本文整理汇总了PHP中Symfony\Component\Translation\Translator::setFallbackLocales方法的典型用法代码示例。如果您正苦于以下问题:PHP Translator::setFallbackLocales方法的具体用法?PHP Translator::setFallbackLocales怎么用?PHP Translator::setFallbackLocales使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Translation\Translator
的用法示例。
在下文中一共展示了Translator::setFallbackLocales方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: configureLocale
/**
* Configure translator locale.
*/
public static function configureLocale()
{
$localeOption = Validation::option('locale');
if (!is_array($localeOption)) {
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
self::$translator->setLocale($localeOption);
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
self::$translator->setFallbackLocales([$localeOption]);
} else {
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
self::$translator->setLocale(array_shift($localeOption));
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
self::$translator->setFallbackLocales($localeOption);
}
}
示例2: register
public function register(Container $app)
{
$app['translator'] = function ($app) {
if (!isset($app['locale'])) {
throw new \LogicException('You must define \'locale\' parameter or register the LocaleServiceProvider to use the TranslationServiceProvider');
}
$translator = new Translator($app['locale'], $app['translator.message_selector'], $app['translator.cache_dir'], $app['debug']);
$translator->setFallbackLocales($app['locale_fallbacks']);
$translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader());
// Register default resources
foreach ($app['translator.resources'] as $resource) {
$translator->addResource($resource[0], $resource[1], $resource[2], $resource[3]);
}
foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
}
}
return $translator;
};
$app['translator.listener'] = function ($app) {
return new TranslatorListener($app['translator'], $app['request_stack']);
};
$app['translator.message_selector'] = function () {
return new MessageSelector();
};
$app['translator.resources'] = $app->protect(function ($app) {
return [];
});
$app['translator.domains'] = [];
$app['locale_fallbacks'] = ['en'];
$app['translator.cache_dir'] = null;
}
示例3: register
public function register()
{
$di = $this->getContainer();
$config = $this->getConfig();
$di->add($config['message_selector_class']);
foreach ($config['loaders'] as $name => $class) {
$di->add($class);
$di->add('translation.loader.' . $name, $class);
}
$di->add('Symfony\\Component\\Translation\\Translator', function () use($di, $config) {
$selector = $di->get($config['message_selector_class']);
$translator = new Translator($config['locale'], $selector);
$translator->setFallbackLocales($config['fallback_locales']);
foreach ($config['loaders'] as $name => $class) {
$translator->addLoader($name, $di->get('translation.loader.' . $name));
}
foreach ($config['resources'] as $locale => $resources) {
foreach ($resources as $config) {
list($loader, $arg, $domain) = $config + ['array', [], 'messages'];
$translator->addResource($loader, $arg, $locale, $domain);
}
}
return $translator;
}, true);
$di->add('Laasti\\SymfonyTranslationProvider\\TranslationArray')->withArgument('Symfony\\Component\\Translation\\Translator');
}
示例4: configure
function configure()
{
$translator = new Translator("fr_FR", new MessageSelector());
$translator->setFallbackLocales(array("fr"));
$translator->addLoader("array", new ArrayLoader());
$translator->addResource("array", array("Hello World!" => "Bonjour"), "fr");
Yasc_App::config()->addOption("translator", $translator);
}
示例5: create
/**
* @return Translator
*/
public function create()
{
$translator = new Translator(null);
$translator->addLoader('yml', new YamlFileLoader());
$translator->setFallbackLocales(['cs']);
$translator = $this->addResourcesToTranslator($translator);
return $translator;
}
示例6: runTest
public function runTest()
{
$translator = new Translator("fr_FR", new MessageSelector());
$translator->addLoader("array", new ArrayLoader());
$translator->addResource("array", array("Hello World!" => "Bonjour", "Symfony is great" => "Symfony est le meuilleur", "Hello %name%" => "Bonjour %name%", "apple.choice" => "Une pomme | %count% pommes"), "fr_FR");
$translator->setFallbackLocales(array("en"));
(new TranslationValidator($translator))->validate();
}
示例7: testTransChoiceFallbackIsLogged
public function testTransChoiceFallbackIsLogged()
{
$logger = $this->getMock('Psr\\Log\\LoggerInterface');
$logger->expects($this->once())->method('debug')->with('Translation use fallback catalogue.');
$translator = new Translator('ar');
$translator->setFallbackLocales(array('en'));
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
$loggableTranslator = new LoggingTranslator($translator, $logger);
$loggableTranslator->transChoice('some_message2', 10, array('%count%' => 10));
}
示例8: register
public function register(Application $app)
{
$config = $this->config;
//Expose some settings
$app['default_locale'] = $config['default_locale'];
$app['fallback_locale'] = $config['fallback_locale'];
$app['locales'] = $config['locales'];
$app['devture_localization.url_generator'] = $app->share(function ($app) {
return new Routing\LocaleAwareUrlGenerator($app, $app['routes'], $app['request_context']);
});
if (!isset($app['url_generator'])) {
//Technically, we don't need either of those.
//But registering as `url_generator` directly will fail if one of them overwrites it later.
//Most projects will include one of them anyway, so this ordering requirement shouldn't be a big deal.
throw new \LogicException('url_generator missing. Register FrameworkBundle (or the UrlGeneratorServiceProvider) before LocalizationBundle.');
}
//Replace the url_generator with our own, to enable transparent locale-aware URL generation
$app->extend('url_generator', function ($current) use($app) {
return $app['devture_localization.url_generator'];
});
$app['devture_localization.locale_listener'] = $app->share(function ($app) use($config) {
return new EventListener\LocaleListener($config['default_locale'], array_keys($config['locales']), $app['devture_localization.translator']);
});
$app['devture_localization.translator'] = $app->share(function ($app) use($config) {
$translator = new Translator('en', $app['devture_localization.translator.message_selector'], $config['cache_path'], $config['auto_reload']);
if (isset($app['fallback_locale'])) {
$translator->setFallbackLocales(array($app['fallback_locale']));
}
$translator->addLoader('json', $app['devture_localization.translator.loader']);
return $translator;
});
//Alias it, so services that rely on $app['translator'] (like devture/form's FormExtension) can work.
//Silex\Provider\TranslationServiceProvider also exports $app['translator'], so this makes us compatible.
$app['translator'] = $app->share(function ($app) {
return $app['devture_localization.translator'];
});
$app['devture_localization.translator.message_selector'] = $app->share(function () {
return new MessageSelector();
});
$app['devture_localization.translator.resource_loader'] = $app->share(function ($app) {
return new Translation\ResourceLoader($app['devture_localization.translator'], 'json');
});
$app['devture_localization.translator.loader'] = $app->share(function () {
return new Translation\JsonFileLoader();
});
$app['devture_localization.twig.translation_extension'] = $app->share(function ($app) {
return new TranslationExtension($app['devture_localization.translator']);
});
$app['devture_localization.twig.locale_helper_extension'] = $app->share(function ($app) use($config) {
return new Twig\LocaleHelperExtension($app, $config['locales']);
});
}
示例9: createTranslator
/**
* @param Request $request
* @return Translator
*/
public static function createTranslator(Request $request)
{
$translator = new Translator($request->getSession()->get('locale'), new MessageSelector());
$translator->setFallbackLocales(['ru']);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['username' => 'Имя пользователя', 'date_of_birth' => 'Дата рождения', 'photo' => 'Фото'], 'ru', 'crud_view');
$translator->addResource('array', ['username' => 'Username', 'date_of_birth' => 'Date of birth', 'photo' => 'Photo'], 'en', 'crud_view');
$translator->addResource('array', ['hello_user' => 'Привет, %username%!'], 'ru', 'auth');
$translator->addResource('array', ['hello_user' => 'Hello, %username%!'], 'en', 'auth');
$translator->addResource('array', ['plural' => '%count% яблоко|%count% яблока|%count% яблок'], 'ru');
$translator->addResource('array', ['plural' => 'One apple|%count% apples'], 'en');
return $translator;
}
示例10: register
public function register(Container $app)
{
$app['translator'] = function ($app) {
if (!isset($app['locale'])) {
throw new \LogicException('You must define \'locale\' parameter or register the LocaleServiceProvider to use the TranslationServiceProvider');
}
$translator = new Translator($app['locale'], $app['translator.message_selector'], $app['translator.cache_dir'], $app['debug']);
$translator->setFallbackLocales($app['locale_fallbacks']);
$translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader());
if (isset($app['validator'])) {
$r = new \ReflectionClass('Symfony\\Component\\Validator\\Validation');
$file = dirname($r->getFilename()) . '/Resources/translations/validators.' . $app['locale'] . '.xlf';
if (file_exists($file)) {
$translator->addResource('xliff', $file, $app['locale'], 'validators');
}
}
if (isset($app['form.factory'])) {
$r = new \ReflectionClass('Symfony\\Component\\Form\\Form');
$file = dirname($r->getFilename()) . '/Resources/translations/validators.' . $app['locale'] . '.xlf';
if (file_exists($file)) {
$translator->addResource('xliff', $file, $app['locale'], 'validators');
}
}
// Register default resources
foreach ($app['translator.resources'] as $resource) {
$translator->addResource($resource[0], $resource[1], $resource[2], $resource[3]);
}
foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
}
}
return $translator;
};
if (isset($app['request_stack'])) {
$app['translator.listener'] = function ($app) {
return new TranslatorListener($app['translator'], $app['request_stack']);
};
}
$app['translator.message_selector'] = function () {
return new MessageSelector();
};
$app['translator.resources'] = $app->protect(function ($app) {
return array();
});
$app['translator.domains'] = array();
$app['locale_fallbacks'] = array('en');
$app['translator.cache_dir'] = null;
}
示例11: register
/**
* {@inheritdoc}
*/
public function register()
{
$this->app->singleton('Flarum\\Locale\\LocaleManager');
$this->app->alias('Flarum\\Locale\\LocaleManager', 'flarum.localeManager');
$this->app->singleton('translator', function () {
$defaultLocale = $this->app->make('flarum.settings')->get('default_locale');
$translator = new Translator($defaultLocale, new MessageSelector());
$translator->setFallbackLocales([$defaultLocale]);
$translator->addLoader('yaml', new YamlFileLoader());
return $translator;
});
$this->app->alias('translator', 'Symfony\\Component\\Translation\\Translator');
$this->app->alias('translator', 'Symfony\\Component\\Translation\\TranslatorInterface');
}
示例12: setUp
protected function setUp(array $install = array(), $language = 'en')
{
$translation = new Translator('en_EN', new MessageSelector());
$translation->setFallbackLocales(array('en'));
$translation->addLoader('yaml', new YamlFileLoader());
$translation->addResource('yaml', __DIR__ . '/../../../../../locales/en.yml', 'en');
$translation->addResource('yaml', __DIR__ . '/../../../../../locales/de.yml', 'de');
$command = new InstallCommand();
$command->setInstalls($install)->setTranslator($translation);
$console = new Application();
$console->add($command);
$command = $console->find('api:install');
$this->command = new CommandTester($command);
$this->command->execute(array('command' => $command->getName(), 'language' => $language));
}
示例13: setUp
protected function setUp($dir = '', array $options = array())
{
$translation = new Translator('en_EN', new MessageSelector());
$translation->setFallbackLocales(array('en'));
$translation->addLoader('yaml', new YamlFileLoader());
$translation->addResource('yaml', __DIR__ . '/../../../../../locales/en.yml', 'en');
$translation->addResource('yaml', __DIR__ . '/../../../../../locales/de.yml', 'de');
$command = new ValidateCommand();
$command->setConfigDir($dir)->setTranslator($translation);
$console = new Application();
$console->add($command);
$command = $console->find('api:validate');
$this->command = new CommandTester($command);
$this->command->execute(array_merge(array('command' => $command->getName()), $options));
}
示例14: __construct
public function __construct()
{
$translator_config = (require APPDIR . 'config/translation.php');
$this->config = $translator_config;
$translator = new TranslationProvider($translator_config['default'], new MessageSelector());
$translator->setFallbackLocales(array($translator_config['default']));
$translator->addLoader('array', new PhpFileLoader());
$this->translator = $translator;
$app = App::instance();
$app_locale = $app->cookies()->get('app_locale');
if ($app_locale) {
if (in_array($app_locale, $this->config['locales'])) {
$this->setLocale($app_locale);
} else {
$this->setLocale($translator_config['default']);
}
} else {
$this->setLocale($translator_config['default']);
}
}
示例15: createInstance
protected static function createInstance()
{
if (config('app.debug') && !config('trans.cache_on_debug')) {
\Cache::forget('po_cache');
}
return \Cache::rememberForever('po_cache', function () {
$basePath = config('trans.translations_path');
$locales = config('trans.supported_locales');
$translator = new SymfonyTranslator(config('app.locale'));
$translator->addLoader('po', new PoFileLoader());
$translator->setFallbackLocales([config('app.locale')]);
foreach ($locales as $locale) {
$path = base_path($basePath . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES');
$file = $path . DIRECTORY_SEPARATOR . 'messages.po';
if (file_exists($file)) {
$translator->addResource('po', $file, $locale);
$translator->getCatalogue($locale);
}
}
return $translator;
});
}