Permalink
Browse files

Refactoring `Catalog`.

Forcing uniform format for in and ouput.

The conversions for the `lossy` and `lossless` have been moved into the
`Catalog` class' `read` and `write` methods.

Removing support for querying multiple locales.

Support for multiple locales is seldomly needed and adds up to the
complexity and speed of the methods.

Removing explitcly defined `Catalog` categories.

To allow for easier extension and adding of categories checks if a
certain category is supported have been moved into the `read` and
`write` methods of the adapters.

Renaming categories for templates and pages.

By default all categories are treated as socalled _pages_. In order for
being able to treat all categories in a uniform way. Adapters only have
to return full pages and don't need to care about it's components and
support for them.

Selecting of _page_ ids.

Parts of _pages_ can be selected by appending the id to select to the
category: `validation.phone`.

Renaming and making `Base::mergeMessageItem()` more generic.

Renaming `Base::formatMessageItem()`.

Upating lossless format to be as flexible as possible.
  • Loading branch information...
1 parent e3319d0 commit 177f12d0d655296be08c33ecad93953fb0981310 @davidpersson davidpersson committed with gwoo Jan 6, 2010
@@ -148,8 +148,8 @@
* Globalization runtime data. You can add globalized data during runtime utilizing a
* configuration set up to use the _memory_ adapter.
*/
-// $data = array('root' => function($n) { return $n != 1 ? 1 : 0; });
-// Catalog::write('message.plural', $data, array('name' => 'runtime'));
+// $data = function($n) { return $n != 1 ? 1 : 0; };
+// Catalog::write('message.plural', 'root', $data, array('name' => 'runtime'));
/**
* Enabling globalization integration. Classes in the framework are designed with
@@ -159,12 +159,8 @@
// use lithium\util\Validator;
// use lithium\util\Inflector;
//
-// Validator::add('postalCode',
-// Catalog::read('validation.postalCode', array('en_US'))
-// );
-// Inflector::rules('transliterations',
-// Catalog::read('inflection.transliteration', array('en'))
-// );
+// Validator::add('phone', Catalog::read('validation.phone', 'en_US'));
+// Inflector::rules('transliterations', Catalog::read('inflection.transliterations', 'en'));
/**
* Your custom code goes here.
@@ -41,7 +41,7 @@ public function run() {
$this->err('Yielded no items.');
return 1;
}
- $count = count($data['root']);
+ $count = count($data);
$this->out("Yielded {$count} items.");
$this->nl();
@@ -95,10 +95,11 @@ protected function _extract() {
$configs[$name] = compact('adapter', 'path', 'scope');
}
Catalog::config($configs);
- $scope = $configs[$name]['scope'];
- return Catalog::read('message.template', 'root', compact('name', 'scope'));
- }
+ return Catalog::read('messageTemplate', 'root', compact('name') + array(
+ 'scope' => $configs[$name]['scope'],
+ 'lossy' => false
+ ));
}
/**
@@ -153,7 +154,7 @@ protected function _writeTemplate($data) {
$this->out('Aborting upon user request.');
$this->stop(1);
}
- return Catalog::write('message.template', $data, compact('name', 'scope'));
+ return Catalog::write('messageTemplate', 'root', $data, compact('name', 'scope'));
}
}
@@ -32,8 +32,6 @@ class Catalog extends \lithium\core\Adaptable {
protected static $_configurations = null;
- protected static $_adapters = 'adapter.g11n.catalog';
-
public static function config($config = null) {
$default = array('scope' => null);
@@ -44,58 +42,57 @@ public static function config($config = null) {
}
/**
- * Reads data. Data can be obtained for one or multiple configurations and locales.
+ * Reads data.
*
- * The results for list-like categories are aggregated by querying all requested
- * configurations for the requested locale and then repeating this process for all locales down
- * the locale cascade. This allows for sparse data which is complemented by data from other
- * sources or for more generic locales. Aggregation can be controlled by either specifying the
- * configurations or a scope to use.
+ * Results are aggregated by querying all requested configurations for the requested
+ * locale then repeating this process for all locales down the locale cascade. This
+ * allows for sparse data which is complemented by data from other sources or for more
+ * generic locales. Aggregation can be controlled by either specifying the configurations
+ * or a scope to use.
*
* Usage:
* {{{
- * Catalog::read('message.page', array('zh', 'en'));
+ * Catalog::read('message', 'zh');
* Catalog::read('validation.postalCode', 'en_US');
* }}}
*
- * @param string $category Dot-delimeted category.
- * @param string|array $locales One or multiple locales.
+ * @param string $category A (dot-delimeted) category.
+ * @param string $locale A locale identifier.
* @param array $options Valid options are:
* - `'name'`: One or multiple configuration names.
* - `'scope'`: The scope to use.
+ * - `'lossy'`: Whether or not to use the compact and lossy format, defaults to `true`.
* @return array|void If available the requested data, else `null`.
- * @see lithium\g11n\catalog\adapter\Base::$_categories.
*/
- public static function read($category, $locales, $options = array()) {
- $defaults = array('name' => null, 'scope' => null);
+ public static function read($category, $locale, $options = array()) {
+ $defaults = array('name' => null, 'scope' => null, 'lossy' => true);
$options += $defaults;
+ $category = strtok($category, '.');
+ $id = strtok('.');
+
$names = (array) $options['name'] ?: static::$_configurations->keys();
- $results = null;
-
- foreach ((array) $locales as $locale) {
- foreach (Locale::cascade($locale) as $cascaded) {
- foreach ($names as $name) {
- $adapter = static::adapter($name);
-
- if (!$adapter->isSupported($category, __FUNCTION__)) {
- continue;
- }
- if (!$result = $adapter->read($category, $cascaded, $options['scope'])) {
- continue;
- }
- if (!is_array($result)) {
- $results[$locale] = $result;
- break 2;
- }
- if (!isset($results[$locale])) {
- $results[$locale] = array();
- }
- $results[$locale] += $result;
+ $results = array();
+
+ foreach (Locale::cascade($locale) as $cascaded) {
+ foreach ($names as $name) {
+ $adapter = static::adapter($name);
+
+ if ($result = $adapter->read($category, $cascaded, $options['scope'])) {
+ $results += $result;
}
}
}
- return $results;
+ if ($options['lossy']) {
+ array_walk($results, function(&$value) {
+ $value = $value['translated'];
+ });
+ }
+
+ if ($id) {
+ return isset($results[$id]) ? $results[$id] : null;
+ }
+ return $results ?: null;
}
/**
@@ -104,44 +101,52 @@ public static function read($category, $locales, $options = array()) {
* Usage:
* {{{
* $data = array(
- * 'pl' => array(
- * 'color' => 'Kolor'
- * )
- * 'ja' => array(
- * 'color' => '色'
- * ));
- * Catalog::write('message.page', $data, array('name' => 'runtime'));
+ * 'color' => '色'
+ * );
+ * Catalog::write('message', 'ja', $data, array('name' => 'runtime'));
* }}}
*
- * @param string $category Dot-delimeted category.
- * @param array $data Data keyed by locale.
+ * @param string $category A (dot-delimeted) category.
+ * @param string $locale A locale identifier.
+ * @param array $data
* @param array $options Valid options are:
* - `'name'`: One or multiple configuration names.
* - `'scope'`: The scope to use.
* @return boolean Success.
- * @see lithium\g11n\catalog\adapter\Base::$_categories.
*/
- public static function write($category, $data, $options = array()) {
+ public static function write($category, $locale, $data, $options = array()) {
$defaults = array('name' => null, 'scope' => null);
$options += $defaults;
+ $category = strtok($category, '.');
+ $id = strtok('.');
+
+ if ($id) {
+ $data = array($id => $data);
+ }
+
+ array_walk($data, function(&$value, $key) {
+ if (!is_array($value) || !isset($value['translated'])) {
+ $value = array(
+ 'id' => $key,
+ 'ids' => array('singular' => $key),
+ 'translated' => $value
+ );
+ }
+ });
+
$names = (array) $options['name'] ?: static::$_configurations->keys();
foreach ($names as $name) {
$adapter = static::adapter($name);
-
- if (!$adapter->isSupported($category, __FUNCTION__)) {
- continue;
- }
- foreach ($data as $locale => $item) {
- if (!$adapter->write($category, $locale, $options['scope'], $item)) {
- return false;
- }
- }
- return true;
+ return $adapter->write($category, $locale, $options['scope'], $data);
}
return false;
}
+
+ public static function adapter($name) {
+ return static::_adapter('adapter.g11n.catalog', $name);
+ }
}
?>
@@ -97,14 +97,17 @@ protected static function _translated($id, $options = array()) {
);
extract($options + $defaults);
- $page = Catalog::read('message.page', $locale, compact('scope'));
- $plural = Catalog::read('message.plural', $locale);
+ $page = Catalog::read('message', $locale, compact('scope'));
- if (empty($page[$locale][$id]['translated']) || !isset($plural[$locale])) {
+ if (!isset($page[$id])) {
return null;
}
- $translated = $page[$locale][$id]['translated'];
- $key = $plural[$locale]($count);
+ $translated = (array) $page[$id];
+
+ if (!isset($page['plural']) || !is_callable($page['plural'])) {
+ return null;
+ }
+ $key = $page['plural']($count);
if (isset($translated[$key])) {
return $translated[$key];
Oops, something went wrong.

0 comments on commit 177f12d

Please sign in to comment.