/** * Reads data. * * 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(true, 'message', 'zh'); * Catalog::read('default', 'message', 'zh'); * Catalog::read('default', 'validation.postalCode', 'en_US'); * }}} * * @param mixed $name Provide a single configuration name as a string or multiple ones as * an array which will be used to read from. Pass `true` to use all configurations. * @param string $category A (dot-delimeted) category. * @param string $locale A locale identifier. * @param array $options Valid options are: * - `'scope'`: The scope to use. * - `'lossy'`: Whether or not to use the compact and lossy format, defaults to `true`. * @return array If available the requested data, else `null`. */ public static function read($name, $category, $locale, array $options = array()) { $defaults = array('scope' => null, 'lossy' => true); $options += $defaults; $category = strtok($category, '.'); $id = strtok('.'); $names = $name === true ? array_keys(static::$_configurations) : (array) $name; $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; } } } if ($options['lossy']) { array_walk($results, function (&$value) { $value = $value['translated']; }); } if ($id) { return isset($results[$id]) ? $results[$id] : null; } return $results ?: null; }
/** * Tests cascading of locales. * * @return void */ public function testCascade() { $expected = array('root'); $this->assertEqual($expected, Locale::cascade('root')); $expected = array('en', 'root'); $this->assertEqual($expected, Locale::cascade('en')); $expected = array('en_US', 'en', 'root'); $this->assertEqual($expected, Locale::cascade('en_US')); $expected = array('zh_HK_REVISED', 'zh_HK', 'zh', 'root'); $this->assertEqual($expected, Locale::cascade('zh_HK_REVISED')); $expected = array('zh_Hans_HK', 'zh_Hans', 'zh', 'root'); $this->assertEqual($expected, Locale::cascade('zh_Hans_HK')); $expected = array('zh_Hans_HK_REVISED', 'zh_Hans_HK', 'zh_Hans', 'zh', 'root'); $this->assertEqual($expected, Locale::cascade('zh_Hans_HK_REVISED')); }