In order to avoid unnecessary overhead all methods throughout the framework accepting a locale require it to be well-formed according to the structure laid out below. For assuring the correct format use Locale::canonicalize() once on the locale. However the methods within this class will also work with not-so-well-formed locales. They accept both underscores and hyphens as separators between and don't care about the case of the individual tags. The identifier used by Lithium is based in its structure upon Unicode's language identifier and is compliant to BCP 47. language[_Script][_TERRITORY][_VARIANT] - language The spoken language, here represented by an ISO 639-1 code, where not available ISO 639-3 and ISO 639-5 codes are allowed too) tag. The tag should be lower-cased and is required. - Script The tag should have its first character capitalized, all others lower-cased. The tag is optional. - TERRITORY A geographical area, here represented by an ISO 3166-1 code. Should be all upper-cased and is optional. - VARIANT Should be all upper-cased and is optional.
Inheritance: extends lithium\core\StaticObject
 /**
  * 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;
 }
Example #2
0
});
/**
 * 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;
});
ConsoleDispatcher::applyFilter('_callable', function ($self, $params, $chain) {
    $request = $params['request'];
    $command = $chain->next($self, $params, $chain);
    if (!$request->locale) {
        $request->params['locale'] = Locale::preferred($request);
    }
    Environment::set(Environment::get(), array('locale' => $request->locale));
    return $command;
});
Example #3
0
 public function testPreferredAvailableNegotiation()
 {
     $expected = 'nl_BE';
     $result = Locale::preferred(array('nl_NL', 'nl_BE', 'nl', 'en_US', 'en'), array('en', 'en_US', 'nl_BE'));
     $this->assertEqual($expected, $result);
     $expected = 'da';
     $result = Locale::preferred(array('da', 'en_GB', 'en'), array('da', 'en_GB', 'en'));
     $this->assertEqual($expected, $result);
     $expected = 'da';
     $result = Locale::preferred(array('da', 'en_GB', 'en'), array('en', 'en_GB', 'da'));
     $this->assertEqual($expected, $result);
     $expected = 'en_GB';
     $result = Locale::preferred(array('da', 'en_GB', 'en'), array('en_GB', 'en'));
     $this->assertEqual($expected, $result);
     $expected = 'da';
     $result = Locale::preferred(array('da_DK', 'en_GB', 'en'), array('da', 'en_GB', 'en'));
     $this->assertEqual($expected, $result);
     $expected = 'zh';
     $result = Locale::preferred(array('zh_Hans_REVISED', 'zh_Hans_HK', 'zh', 'en'), array('zh_Hans_HK_REVISED', 'zh_Hans_HK', 'zh', 'en'));
     $this->assertEqual($expected, $result);
 }
Example #4
0
Environment::set('production', compact('locale', 'locales'));
Environment::set('development', compact('locale', 'locales'));
Environment::set('test', array('locale' => 'en', 'locales' => array('en' => 'English')));
/**
 * Effective/Request Locale
 *
 * 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.
 *
 * @see lithium\g11n\Message
 * @see lithium\core\Environment
 */
$setLocale = function ($self, $params, $chain) {
    if (!$params['request']->locale()) {
        $params['request']->locale(Locale::preferred($params['request']));
    }
    Environment::set(true, array('locale' => $params['request']->locale()));
    return $chain->next($self, $params, $chain);
};
ActionDispatcher::applyFilter('_callable', $setLocale);
ConsoleDispatcher::applyFilter('_callable', $setLocale);
/**
 * Resources
 *
 * Globalization (g11n) catalog configuration.  The catalog allows for obtaining and
 * writing globalized data. Each configuration can be adjusted through the following settings:
 *
 *   - `'adapter'` _string_: The name of a supported adapter. The builtin adapters are `Memory` (a
 *     simple adapter good for runtime data and testing), `Php`, `Gettext`, `Cldr` (for
 *     interfacing with Unicode's common locale data repository) and `Code` (used mainly for
Example #5
0
 public function testRespondsToMagic()
 {
     $this->assertTrue(Locale::respondsTo('language'));
     $this->assertTrue(Locale::respondsTo('territory'));
     $this->assertFalse(Locale::respondsTo('foobar'));
 }
Example #6
0
 protected function _readValidation($path, $locale)
 {
     if (!($territory = Locale::territory($locale))) {
         return null;
     }
     $data = array();
     $file = "{$path}/supplemental/postalCodeData.xml";
     $query = "/supplementalData/postalCodeData";
     $query .= "/postCodeRegex[@territoryId=\"{$territory}\"]";
     $nodes = $this->_parseXml($file, $query);
     $regex = (string) current($nodes);
     return $this->_merge($data, array('id' => 'postalCode', 'translated' => "/^{$regex}\$/"));
 }
Example #7
0
 /**
  * An Accept-Language coming from a Chrome user which contains an invalid
  * item `es-419` causing `preferred` to fail with an exception while it
  * should ignored.
  *
  * @see https://github.com/UnionOfRAD/lithium/issues/386
  */
 public function testPreferredMalformedSpanish()
 {
     $available = array('fr', 'de');
     $chrome = 'es-419,es;q=0.8';
     $request = new ActionRequest(array('env' => array('HTTP_ACCEPT_LANGUAGE' => $chrome)));
     $result = Locale::preferred($request, $available);
     $this->assertNull($result);
 }
Example #8
0
	<div class="nav">
		<nav>
			<?php 
echo $this->_view->render(array('element' => 'nav'), compact('object'), array('library' => 'li3_docs'));
?>
		</nav>
	</div>

	<div class="article">
		<article>
			<?php 
echo $this->content;
?>
		</article>
	</div>
	<script type="text/javascript" charset="utf-8">
		$(document).ready(function () {
			RadCli.setup({
				setupGitCopy: false,
				commandBase: 'http://lithify.me/<?php 
echo Locale::language(Environment::get('locale'));
?>
/cmd'
			});
			$('#header').css({ borderTop: '40px solid black' });
		});
	</script>
</body>
</html>
 /**
  * Tests formatting of locale.
  *
  * @return void
  */
 public function testCanonicalize()
 {
     $this->assertEqual('en_US', Locale::canonicalize('en-US'));
     $this->assertEqual('en_US_POSIX', Locale::canonicalize('en_US-posiX'));
     $this->assertEqual('kpe_GN', Locale::canonicalize('kpe_gn'));
     $this->assertEqual('zh_Hans_HK_REVISED', Locale::canonicalize('ZH-HANS-HK_REVISED'));
 }