当前位置: 首页>>代码示例>>PHP>>正文


PHP Translator::setFallbackLocales方法代码示例

本文整理汇总了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);
     }
 }
开发者ID:VanillaPackage,项目名称:vanilla-validation,代码行数:18,代码来源:ValidationLocalize.php

示例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;
 }
开发者ID:speedwork,项目名称:provider,代码行数:34,代码来源:TranslationServiceProvider.php

示例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');
 }
开发者ID:laasti,项目名称:symfony-translation-provider,代码行数:26,代码来源:SymfonyTranslationProvider.php

示例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);
}
开发者ID:nebiros,项目名称:yasc,代码行数:8,代码来源:index.php

示例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;
 }
开发者ID:TomasVotruba,项目名称:tomasvotruba.cz,代码行数:11,代码来源:TranslatorFactory.php

示例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();
 }
开发者ID:jeanpasqualini-lesson,项目名称:lesson-translation,代码行数:8,代码来源:ArrayTest.php

示例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));
 }
开发者ID:Ener-Getick,项目名称:symfony,代码行数:11,代码来源:LoggingTranslatorTest.php

示例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']);
     });
 }
开发者ID:devture,项目名称:silex-localization-bundle,代码行数:52,代码来源:ServicesProvider.php

示例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;
 }
开发者ID:GrizliK1988,项目名称:symfony-certification-prepare-project,代码行数:17,代码来源:TranslationFactory.php

示例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;
 }
开发者ID:cordje,项目名称:Silex,代码行数:50,代码来源:TranslationServiceProvider.php

示例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');
 }
开发者ID:johnulist,项目名称:core,代码行数:17,代码来源:LocaleServiceProvider.php

示例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));
 }
开发者ID:dimug,项目名称:api,代码行数:15,代码来源:InstallCommandTest.php

示例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));
 }
开发者ID:dimug,项目名称:api,代码行数:15,代码来源:ValidateCommandTest.php

示例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']);
     }
 }
开发者ID:davinder17s,项目名称:turbo,代码行数:20,代码来源:translator.php

示例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;
     });
 }
开发者ID:gummibeer,项目名称:laravel-translation,代码行数:22,代码来源:Translator.php


注:本文中的Symfony\Component\Translation\Translator::setFallbackLocales方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。