read() public static méthode

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');
public static read ( mixed $name, string $category, string $locale, array $options = [] ) : array
$name mixed 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.
$category string A (dot-delimeted) category.
$locale string A locale identifier.
$options array Valid options are: - `'scope'`: The scope to use. - `'lossy'`: Whether or not to use the compact and lossy format, defaults to `true`.
Résultat array If available the requested data, else `null`.
	public function testTransliteration() {
		$data = array(
			'transliteration' => array(
				'\$' => 'dollar',
				'&' => 'and'
			)
		);
		Catalog::write('runtime', 'inflection', 'en', $data);

		Inflector::rules(
			'transliteration', Catalog::read('runtime', 'inflection.transliteration', 'en')
		);

		$result = Inflector::slug('this & that');
		$expected = 'this-and-that';
		$this->assertEqual($expected, $result);

		$data = array(
			'transliteration' => array(
				't' => 'd',
				'&' => 'und'
			)
		);
		Catalog::write('runtime', 'inflection', 'de', $data);

		Inflector::rules(
			'transliteration', Catalog::read('runtime', 'inflection.transliteration', 'de')
		);

		$result = Inflector::slug('this & that');
		$expected = 'dhis-und-dhad';
		$this->assertEqual($expected, $result);
	}
Exemple #2
0
 /**
  * Extracts translatable strings from multiple files.
  *
  * @return array Returns the catalog specified. Returns boolean `false` when an error occurs.
  */
 protected function _extract()
 {
     $message[] = 'A `Catalog` class configuration with an adapter that is capable of';
     $message[] = 'handling read requests for the `messageTemplate` category is needed';
     $message[] = 'in order to proceed. This may also be referred to as `extractor`.';
     $this->out($message);
     $this->out();
     $configs = (array) Catalog::config();
     $this->out('Available `Catalog` Configurations:');
     foreach ($configs as $name => $config) {
         $this->out(" - {$name}");
     }
     $this->out();
     $name = $this->in('Please choose a configuration or hit [enter] to add one:', array('choices' => array_keys($configs)));
     if (!$name) {
         $adapter = $this->in('Adapter:', array('default' => 'Code'));
         $path = $this->in('Path:', array('default' => $this->source));
         $scope = $this->in('Scope:', array('default' => $this->scope));
         $name = 'runtime' . uniqid();
         $configs[$name] = compact('adapter', 'path', 'scope');
     }
     Catalog::config($configs);
     try {
         return Catalog::read($name, 'messageTemplate', 'root', array('scope' => $configs[$name]['scope'], 'lossy' => false));
     } catch (Exception $e) {
         return false;
     }
 }
 /**
  * 1. Extract all translated strings from the codebase
  *    If you modify any text in the site, please follow Extract, Create and Compile
  */
 public function extract()
 {
     $command = 'echo Y|del "F:\\Apache\\www\\TBG\\xgcwallet.org\\app\\resources\\tmp\\cache\\templates\\*.*"';
     passthru($command);
     $data = Catalog::read('code', 'messageTemplate', 'root', array('lossy' => false));
     $scope = 'default';
     return Catalog::write('default', 'messageTemplate', 'root', $data, compact('scope'));
 }
 public function testMultipleLocales()
 {
     $data = '/phone en_US/';
     Catalog::write('runtime', 'validation.phone', 'en_US', $data);
     $data = '/phone en_GB/';
     Catalog::write('runtime', 'validation.phone', 'en_GB', $data);
     Validator::add('phone', array('en_US' => Catalog::read('runtime', 'validation.phone', 'en_US'), 'en_GB' => Catalog::read('runtime', 'validation.phone', 'en_GB')));
     $result = Validator::isPhone('phone en_US', 'en_US');
     $this->assertTrue($result);
     $result = Validator::isPhone('phone en_GB', 'en_GB');
     $this->assertTrue($result);
 }
 /**
  * Extracts translatable strings from multiple files.
  *
  * @return array Returns the catalog specified. Returns boolean `false` when an error occurs.
  */
 protected function _extract()
 {
     $message[] = 'A `Catalog` class configuration with an adapter that is capable of';
     $message[] = 'handling read requests for the `messageTemplate` category is needed';
     $message[] = 'in order to proceed. This may also be referred to as `extractor`.';
     $this->out($message);
     $this->out();
     $name = $this->_configuration(array('adapter' => 'Code', 'path' => $this->source, 'scope' => $this->scope));
     $configs = Catalog::config();
     try {
         return Catalog::read($name, 'messageTemplate', 'root', array('scope' => $configs[$name]['scope'], 'lossy' => false));
     } catch (Exception $e) {
         return false;
     }
 }
 /**
  * Tests the plural rule #1 which applies to the following languages
  * grouped by family and sorted alphabetically.
  *
  * Germanic family:
  * - English (en)
  *
  * @return void
  */
 public function testPlurals1()
 {
     $locales = array('en');
     foreach ($locales as $locale) {
         $result = Catalog::read('lithium', 'message.pluralForms', $locale);
         $this->assertEqual(2, $result, "Locale: `{$locale}`\n{:message}");
         $rule = Catalog::read('lithium', 'message.pluralRule', $locale);
         $expected = '10111111111111111111111111111111111111111111111111';
         $expected .= '11111111111111111111111111111111111111111111111111';
         $expected .= '11111111111111111111111111111111111111111111111111';
         $expected .= '11111111111111111111111111111111111111111111111111';
         $result = '';
         for ($n = 0; $n < 200; $n++) {
             $result .= $rule($n);
         }
         $this->assertIdentical($expected, $result, "Locale: `{$locale}`\n{:message}");
     }
 }
Exemple #7
0
 /**
  * Retrieves translations through the `Catalog` class by using `$id` as the lookup
  * key and taking the current or - if specified - the provided locale as well as the
  * scope into account.  Hereupon the correct plural form is determined by passing the
  * value of the `'count'` option to a closure.
  *
  * @see lithium\g11n\Catalog
  * @param string $id The lookup key.
  * @param integer $count Used to determine the correct plural form.
  * @param string $locale The target locale.
  * @param array $options Passed through to `Catalog::read()`. Valid options are:
  *              - `'scope'`: The scope of the message.
  * @return string|void The translation or `null` if none could be found or the plural
  *         form could not be determined.
  * @filter
  */
 protected static function _translated($id, $count, $locale, array $options = array())
 {
     $params = compact('id', 'count', 'locale', 'options');
     $cache =& static::$_cachedPages;
     return static::_filter(__METHOD__, $params, function ($self, $params, $chain) use(&$cache) {
         extract($params);
         if (!isset($cache[$options['scope']][$locale])) {
             $cache[$options['scope']][$locale] = Catalog::read(true, 'message', $locale, $options);
         }
         $page = $cache[$options['scope']][$locale];
         if (!isset($page[$id])) {
             return null;
         }
         if (!is_array($page[$id])) {
             return $page[$id];
         }
         if (!isset($page['pluralRule']) || !is_callable($page['pluralRule'])) {
             return null;
         }
         $key = $page['pluralRule']($count);
         if (isset($page[$id][$key])) {
             return $page[$id][$key];
         }
     });
 }
Exemple #8
0
/**
 * Integration with `View`. Embeds message translation aliases into the `View`
 * class (or other content handler, if specified) when content is rendered. This
 * enables translation functions, i.e. `<?=$t("Translated content"); ?>`.
 */
Media::applyFilter('_handle', function ($self, $params, $chain) {
    $params['handler'] += array('outputFilters' => array());
    $params['handler']['outputFilters'] += Message::aliases();
    return $chain->next($self, $params, $chain);
});
/**
 * Integration with `Validator`. You can load locale dependent rules into the `Validator`
 * by specifying them manually or retrieving them with the `Catalog` class.
 */
foreach (array('phone', 'postalCode', 'ssn') as $name) {
    Validator::add($name, Catalog::read(true, "validation.{$name}", 'en_US'));
}
/**
 * Intercepts dispatching processes in order to set the effective locale by using
 * the locale of the request or if that is not available retrieving a locale preferred
 * by the client.
 */
ActionDispatcher::applyFilter('_callable', function ($self, $params, $chain) {
    $request = $params['request'];
    $controller = $chain->next($self, $params, $chain);
    if (!$request->locale) {
        $request->params['locale'] = Locale::preferred($request);
    }
    Environment::set(Environment::get(), array('locale' => $request->locale));
    return $controller;
});
 * Integration with `View`. Embeds message translation aliases into the `View`
 * class (or other content handler, if specified) when content is rendered. This
 * enables translation functions, i.e. `<?=$t("Translated content"); ?>`.
 */
Media::applyFilter('_handle', function ($self, $params, $chain) {
    $params['handler'] += array('outputFilters' => array());
    $params['handler']['outputFilters'] += Message::aliases();
    return $chain->next($self, $params, $chain);
});
/**
 * Integration with `Validator`. You can load locale dependent rules into the `Validator`
 * by specifying them manually or retrieving them with the `Catalog` class.
 */
Validator::add('phone', Catalog::read('validation.phone', 'en_US'));
Validator::add('postalCode', Catalog::read('validation.postalCode', 'en_US'));
Validator::add('ssn', Catalog::read('validation.ssn', 'en_US'));
/**
 * Intercepts dispatching processes in order to set the effective locale by using
 * the locale of the request or if that is not available retrieving a locale preferred
 * by the client.
 */
ActionDispatcher::applyFilter('_callable', function ($self, $params, $chain) {
    $request = $params['request'];
    $controller = $chain->next($self, $params, $chain);
    if (!$request->locale) {
        $request->params['locale'] = Locale::preferred($request);
    }
    Environment::set(Environment::get(), array('locale' => $request->locale));
    return $controller;
});
ConsoleDispatcher::applyFilter('_callable', function ($self, $params, $chain) {
 public function testNlNl()
 {
     Validator::add(Catalog::read('validation', 'nl_NL'));
     $this->assertTrue(Validator::isSsn('123456789'));
     $this->assertFalse(Validator::isSsn('12345678'));
 }
 public function testOutputLosslessFormat()
 {
     $data = array('house' => array('id' => 'house', 'ids' => array('singular' => 'house'), 'translated' => 'Haus', 'flags' => array(), 'comments' => array(), 'occurrences' => array()));
     Catalog::write('message', 'de', $data, array('name' => 'runtime'));
     $result = Catalog::read('message', 'de', array('lossy' => false));
     $expected = array('house' => array('id' => 'house', 'ids' => array('singular' => 'house'), 'translated' => 'Haus', 'flags' => array(), 'comments' => array(), 'occurrences' => array()));
     $this->assertEqual($expected, $result);
 }
Exemple #12
0
 * and `li3_cldr` projects.
 *
 * Further enables support for multibyte strings through the `Multibyte` class by
 * overwriting rules (currently just `lengthBetween`).
 *
 * @link https://github.com/UnionOfRAD/li3_lldr
 * @link https://github.com/UnionOfRAD/li3_cldr
 * @see lithium\g11n\Catalog
 * @see lithium\g11n\Multibyte
 * @see lithium\util\Validator
 */
foreach (array('phone', 'postalCode', 'ssn') as $name) {
    $regex = Validator::rules($name);
    Validator::add($name, function ($value, $format, $options) use($name, $regex) {
        if ($format !== 'any') {
            $regex = Catalog::read(true, "validation.{$name}", $format);
        }
        if (!$regex) {
            $message = "Cannot find regular expression for validation rule `{$name}` ";
            $message .= "using format/locale `{$format}`.";
            throw new RuntimeException($message);
        }
        return preg_match($regex, $value);
    });
}
Validator::add('lengthBetween', function ($value, $format, $options) {
    $length = Multibyte::strlen($value);
    $options += array('min' => 1, 'max' => 255);
    return $length >= $options['min'] && $length <= $options['max'];
});
/**
 /**
  * Retrieves translations through the `Catalog` class by using `$id` as the lookup
  * key and taking the current or - if specified - the provided locale as well as the
  * scope into account.  Hereupon the correct plural form is determined by passing the
  * value of the `'count'` option to a closure.
  *
  * @param string $id The lookup key.
  * @param integer $count Used to determine the correct plural form.
  * @param string $locale The target locale.
  * @param array $options Passed through to `Catalog::read()`. Valid options are:
  *              - `'scope'`: The scope of the message.
  * @return string|void The translation or `null` if none could be found or the plural
  *         form could not be determined.
  * @see lithium\g11n\Catalog
  * @filter
  * @todo Message pages need caching.
  */
 protected static function _translated($id, $count, $locale, $options = array())
 {
     $params = compact('id', 'count', 'locale', 'options');
     return static::_filter(__METHOD__, $params, function ($self, $params, $chain) {
         extract($params);
         $page = Catalog::read('message', $locale, $options);
         if (!isset($page[$id])) {
             return null;
         }
         $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];
         }
     });
 }