Permalink
Browse files

Merge branch '0.3-g11n' into 0.3

  • Loading branch information...
gwoo committed Dec 9, 2009
2 parents b7b33e2 + e1ae0a5 commit 3d06acf1612b99cc4f42bdd892c413e9425d88d2
View
@@ -131,16 +131,24 @@
// use lithium\g11n\Catalog;
//
// Catalog::config(array(
-// 'runtime' => array('adapter' => 'Memory'),
-// 'app' => array('adapter' => 'Gettext', 'path' => LITHIUM_APP_PATH . '/resources/po'),
-// 'lithium' => array('adapter' => 'Gettext', 'path' => LITHIUM_LIBRARY_PATH . '/lithium/resources/po')
+// 'runtime' => array(
+// 'adapter' => 'Memory'
+// ),
+// 'app' => array(
+// 'adapter' => 'Gettext',
+// 'path' => LITHIUM_APP_PATH . '/resources/g11n'
+// ),
+// 'lithium' => array(
+// 'adapter' => 'Gettext',
+// 'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources'
+// )
// ));
/**
* Globalization runtime data. You can add globalized data during runtime utilizing a
* configuration set up to use the _memory_ adapter.
*/
-// $data = array('en' => function($n) { return $n != 1 ? 1 : 0; });
+// $data = array('root' => function($n) { return $n != 1 ? 1 : 0; });
// Catalog::write('message.plural', $data, array('name' => 'runtime'));
/**
View
@@ -19,11 +19,13 @@
use \lithium\http\Router;
use \lithium\core\Environment;
use \lithium\action\Dispatcher;
+use \lithium\g11n\Message;
+use \lithium\util\String;
/**
* Loads application routes before the request is dispatched. Change this to `include_once` if
* more than one request cycle is executed per HTTP request.
- *
+ *
* @see lithium\http\Router
*/
Dispatcher::applyFilter('run', function($self, $params, $chain) {
@@ -34,7 +36,7 @@
/**
* Intercepts the `Dispatcher` as it finds a controller object, and passes the `'request'` parameter
* to the `Environment` class to detect which environment the application is running in.
- *
+ *
* @see lithium\action\Request
* @see lithium\core\Environment
*/
@@ -43,4 +45,33 @@
return $chain->next($self, $params, $chain);
});
+/**
+ * Implements logic for handling cases where `Message::translate()` returns without a result.
+ * The message specified for the `'default'` option will be used as a fall back. By
+ * default the value for the options is the message passed to the method.
+ */
+Message::applyFilter('translate', function($self, $params, $chain) {
+ $params['options'] += array('default' => $params['id']);
+ return $chain->next($self, $params, $chain) ?: $params['options']['default'];
+});
+
+/**
+ * Placeholders in translated messages. Adds support for `String::insert()`-style placeholders
+ * to translated messages. Placeholders may be used within the message and replacements provided
+ * directly within the `options` argument.
+ *
+ * Usage:
+ * {{{
+ * Message::translate('Your {:color} paintings are looking just great.', array(
+ * 'color' => 'silver',
+ * 'locale' => 'fr'
+ * ));
+ * }}}
+ *
+ * @see lithium\util\String::insert()
+ */
+Message::applyFilter('translate', function($self, $params, $chain) {
+ return String::insert($chain->next($self, $params, $chain), $params['options']);
+});
+
?>
@@ -17,7 +17,7 @@
class G11n extends \lithium\console\Command {
/**
- * The main method of the commad.
+ * The main method of the command.
*
* @return void
*/
@@ -11,55 +11,52 @@
use \Exception;
use \DateTime;
use \lithium\g11n\Catalog;
-use \lithium\util\String;
/**
* The `Extract` class is a command for extracting messages from files.
*/
class Extract extends \lithium\console\Command {
+ public $source;
+
+ public $destination;
+
+ public $scope;
+
+ public function _init() {
+ parent::_init();
+ $this->source = $this->source ?: LITHIUM_APP_PATH;
+ $this->destination = $this->destination ?: LITHIUM_APP_PATH . '/resources/g11n';
+ }
+
/**
- * The main method of the commad.
+ * The main method of the command.
*
* @return void
*/
public function run() {
- $sourcePath = LITHIUM_APP_PATH;
- $destinationPath = LITHIUM_APP_PATH . '/resources/po/';
+ $this->header('Message Extraction');
- $this->out('Extracting messages from source code.');
- $this->hr();
- $timeStart = microtime(true);
-
- $data = $this->_extract($sourcePath);
+ if (!$data = $this->_extract()) {
+ $this->err('Yielded no items.');
+ return 1;
+ }
+ $count = count($data['root']);
+ $this->out("Yielded {$count} items.");
$this->nl();
- $this->out(String::insert('Yielded {:countItems} items taking {:duration} seconds.', array(
- 'countItems' => count($data['root']),
- 'duration' => round(microtime(true) - $timeStart, 4)
- )));
- $this->nl();
- $this->out('Additional data.');
- $this->hr();
+ $this->header('Message Template Creation');
$meta = $this->_meta();
-
$this->nl();
- $this->out('Messages template.');
- $this->hr();
- $message = 'Would you like to save the template now? ';
- $message .= '(An existing template will be overwritten)';
-
- if ($this->in($message, array('choices' => array('y', 'n'), 'default' => 'n')) != 'y') {
- $this->stop(1, 'Aborting upon user request.');
+ if (!$this->_writeTemplate($data, $meta)) {
+ $this->err('Failed to write template.');
+ return 1;
}
$this->nl();
- $this->_writeTemplate($data, $meta);
-
- $this->nl();
- $this->out('Done.');
+ return 0;
}
/**
@@ -68,11 +65,41 @@ public function run() {
* @param array $files Absolute paths to files
* @return array
*/
- protected function _extract($path) {
- Catalog::config(array(
- 'extract' => array('adapter' => 'Code', 'path' => $path)
+ protected function _extract() {
+ $message[] = 'A `Catalog` class configuration with an adapter that is capable of';
+ $message[] = 'handling read requests for the `message.template` category is needed';
+ $message[] = 'in order to proceed.';
+ $this->out($message);
+ $this->nl();
+
+ $configs = (array)Catalog::config()->to('array');
+
+ $this->out('Available `Catalog` Configurations:');
+ foreach ($configs as $name => $config) {
+ $this->out(" - {$name}");
+ }
+ $this->nl();
+
+ $name = $this->in('Please choose a configuration or hit [enter] to add one:', array(
+ 'choices' => array_keys($configs)
));
- return Catalog::read('message.template', 'root', array('name' => 'extract'));
+
+ if (!$name) {
+ $adapter = $this->in('Adapter:', array(
+ 'default' => 'Gettext'
+ ));
+ $path = $this->in('Path:', array(
+ 'default' => $this->destination
+ ));
+ $scope = $this->in('Scope:', array(
+ 'default' => $this->scope
+ ));
+ $name = 'runtime' . uniqid();
+ $configs[$name] = compact('adapter', 'path', 'scope');
+ }
+ Catalog::config($configs);
+ $scope = $configs[$name]['scope'];
+ return Catalog::read('message.template', 'root', compact('name', 'scope'));
}
/**
@@ -81,6 +108,11 @@ protected function _extract($path) {
* @return array
*/
protected function _meta() {
+ $message[] = 'Please provide some data which is used when creating the';
+ $message[] = 'template.';
+ $this->out($message);
+ $this->nl();
+
$now = new DateTime();
return array(
'package' => $this->in('Package name:', array('default' => 'app')),
@@ -100,13 +132,52 @@ protected function _meta() {
* @return void
*/
protected function _writeTemplate($data, $meta) {
- $configs = Catalog::config()->to('array');
- $name = $this->in('Please choose a config:', array(
- 'choices' => array_keys($configs),
- 'default' => 'extract'
+ $message[] = 'In order to proceed you need to choose a `Catalog` configuration';
+ $message[] = 'which is used for writing the template. The adapter for the configuration';
+ $message[] = 'should be capable of handling write requests for the `message.template`';
+ $message[] = 'category.';
+ $this->out($message);
+ $this->nl();
+
+ $configs = (array)Catalog::config()->to('array');
+
+ $this->out('Available `Catalog` Configurations:');
+ foreach ($configs as $name => $config) {
+ $this->out(" - {$name}");
+ }
+ $this->nl();
+
+ $name = $this->in('Please choose a configuration or hit [enter] to add one:', array(
+ 'choices' => array_keys($configs)
));
- Catalog::write('message.template', 'root', compact('name'));
+ if (!$name) {
+ $adapter = $this->in('Adapter:', array(
+ 'default' => 'Gettext'
+ ));
+ $path = $this->in('Path:', array(
+ 'default' => $this->destination
+ ));
+ $scope = $this->in('Scope:', array(
+ 'default' => $this->scope
+ ));
+ $name = 'runtime' . uniqid();
+ $configs[$name] = compact('adapter', 'path', 'scope');
+ Catalog::config($configs);
+ }
+ $scope = $configs[$name]['scope'] ?: $this->in('Scope:', array('default' => $this->scope));
+
+ $message = array();
+ $message[] = 'The template is now ready to be saved.';
+ $message[] = 'Please note that an existing template will be overwritten.';
+ $this->out($message);
+ $this->nl();
+
+ if ($this->in('Save?', array('choices' => array('y', 'n'), 'default' => 'n')) != 'y') {
+ $this->out('Aborting upon user request.');
+ $this->stop(1);
+ }
+ return Catalog::write('message.template', $data, compact('name', 'scope'));
}
}
@@ -27,25 +27,18 @@
*
* The class is able to aggregate data from different sources which allows to complement sparse
* data. Not all categories must be supported by an individual adapter.
- *
- * @todo Extend \lithium\core\Adaptable.
*/
-class Catalog extends \lithium\core\StaticObject {
+class Catalog extends \lithium\core\Adaptable {
protected static $_configurations = null;
- public static function __init() {
- static::$_configurations = new Collection();
- }
-
public static function config($config = null) {
- $default = array('adapter' => null, 'scope' => null);
+ $default = array('scope' => null);
if ($config) {
- $items = array_map(function($i) use ($default) { return $i + $default; }, $config);
- static::$_configurations = new Collection(compact('items'));
+ $config = array_map(function($i) use ($default) { return $i + $default; }, $config);
}
- return static::$_configurations;
+ return parent::config($config);
}
/**
@@ -148,28 +141,8 @@ public static function write($category, $data, $options = array()) {
return false;
}
- public static function clear() {
- static::__init();
- }
-
- public static function _adapter($name = null) {
- if (empty($name)) {
- $names = static::$_configurations->keys();
- if (empty($names)) {
- return;
- }
- $name = end($names);
- }
- if (!isset(static::$_configurations[$name])) {
- return;
- }
- if (is_string(static::$_configurations[$name]['adapter'])) {
- $config = static::$_configurations[$name];
- $class = Libraries::locate('adapter.g11n.catalog', $config['adapter']);
- $conf = array('adapter' => new $class($config)) + static::$_configurations[$name];
- static::$_configurations[$name] = $conf;
- }
- return static::$_configurations[$name]['adapter'];
+ public static function adapter($name) {
+ return static::_adapter('adapter.g11n.catalog', $name);
}
}
Oops, something went wrong.

0 comments on commit 3d06acf

Please sign in to comment.