preferred() публичный статический Метод

Determines the preferred locale from a request or array. Optionally negotiates the preferred locale with available locales.
См. также: lithium\g11n\Locale::_preferredAction()
См. также: lithium\g11n\Locale::_preferredConsole()
См. также: lithium\g11n\Locale::lookup()
public static preferred ( object | array $request, array $available = null ) : string
$request object | array An action or console request object or an array of locales.
$available array A list of locales to negotiate the preferred locale with.
Результат string The preferred locale in its canonical form (i.e. `'fr_CA'`).
Пример #1
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;
});
Пример #2
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
Пример #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);
 }
Пример #4
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);
 }