setFallbackLocale() public method

Sets the fallback locale.
public setFallbackLocale ( string $locale )
$locale string The fallback locale
Example #1
0
 public function register(Application $app, array $options = array())
 {
     $translator = new Translator('en', new MessageSelector());
     $translator->setFallbackLocale('en');
     $translator->addLoader('array', new ArrayLoader());
     $translator->addLoader('xliff', new XliffFileLoader());
     return $translator;
 }
 public function testTransWithFallbackLocale()
 {
     $translator = new Translator('fr_FR', new MessageSelector());
     $translator->addLoader('array', new ArrayLoader());
     $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
     $translator->addResource('array', array('bar' => 'foobar'), 'en');
     $translator->setFallbackLocale('en');
     $this->assertEquals('foobar', $translator->trans('bar'));
 }
Example #3
0
 public function testAddResourceAfterTrans()
 {
     $translator = new Translator('fr');
     $translator->addLoader('array', new ArrayLoader());
     $translator->setFallbackLocale(array('en'));
     $translator->addResource('array', array('foo' => 'foofoo'), 'en');
     $this->assertEquals('foofoo', $translator->trans('foo'));
     $translator->addResource('array', array('bar' => 'foobar'), 'en');
     $this->assertEquals('foobar', $translator->trans('bar'));
 }
 /**
  * Create a new Symfony translator instance.
  *
  * @param  string  $default
  * @param  string  $fallback
  * @return Symfony\Component\Translation\Translator
  */
 protected function createSymfonyTranslator($default, $fallback)
 {
     $trans = new SymfonyTranslator($default);
     // After creating the translator instance we will set the fallback locale
     // as well as the array loader so that messages can be properly loaded
     // from the application. Then we're ready to get the language lines.
     $trans->setFallbackLocale($fallback);
     $trans->addLoader('array', new ArrayLoader());
     return $trans;
 }
Example #5
0
 public function testSetFallbackLocale()
 {
     $translator = new Translator('en', new MessageSelector());
     $translator->addLoader('array', new ArrayLoader());
     $translator->addResource('array', array('foo' => 'foofoo'), 'en');
     $translator->addResource('array', array('bar' => 'foobar'), 'fr');
     // force catalogue loading
     $translator->trans('bar');
     $translator->setFallbackLocale('fr');
     $this->assertEquals('foobar', $translator->trans('bar'));
 }
 public function register(Application $app)
 {
     $app['translator'] = $app->share(function ($app) {
         $translator = new Translator($app['locale'], $app['translator.message_selector']);
         $translator->setFallbackLocale($app['locale_fallback']);
         $translator->addLoader('array', new ArrayLoader());
         $translator->addLoader('xliff', new XliffFileLoader());
         foreach ($app['translator.domains'] as $domain => $data) {
             foreach ($data as $locale => $messages) {
                 $translator->addResource('array', $messages, $locale, $domain);
             }
         }
         return $translator;
     });
     $app['translator.message_selector'] = $app->share(function () {
         return new MessageSelector();
     });
     $app['translator.domains'] = array();
     $app['locale_fallback'] = 'en';
 }
 public function register(Application $app)
 {
     $app['translator'] = $app->share(function () use($app) {
         $translator = new Translator(isset($app['locale']) ? $app['locale'] : 'en', $app['translator.message_selector']);
         if (isset($app['locale_fallback'])) {
             $translator->setFallbackLocale($app['locale_fallback']);
         }
         $translator->addLoader('array', $app['translator.loader']);
         foreach ($app['translator.domains'] as $domain => $data) {
             foreach ($data as $locale => $messages) {
                 $translator->addResource('array', $messages, $locale, $domain);
             }
         }
         return $translator;
     });
     $app['translator.loader'] = $app->share(function () {
         return new ArrayLoader();
     });
     $app['translator.message_selector'] = $app->share(function () {
         return new MessageSelector();
     });
 }
Example #8
0
 public function register(Application $app)
 {
     $app['translator'] = $app->share(function () use($app) {
         $translator = new Translator(isset($app['locale']) ? $app['locale'] : 'en', $app['translator.message_selector']);
         if (isset($app['locale_fallback'])) {
             $translator->setFallbackLocale($app['locale_fallback']);
         }
         $translator->addLoader('array', $app['translator.loader']);
         foreach ($app['translator.messages'] as $locale => $messages) {
             $translator->addResource('array', $messages, $locale);
         }
         return $translator;
     });
     $app['translator.loader'] = $app->share(function () {
         return new ArrayLoader();
     });
     $app['translator.message_selector'] = $app->share(function () {
         return new MessageSelector();
     });
     if (isset($app['translation.class_path'])) {
         $app['autoloader']->registerNamespace('Symfony\\Component\\Translation', $app['translation.class_path']);
     }
 }
 public function testTransChoiceFallbackWithNoTranslation()
 {
     $translator = new Translator('ru', new MessageSelector());
     $translator->setFallbackLocale('en');
     $translator->addLoader('array', new ArrayLoader());
     // consistent behavior with Translator::trans(), which returns the string
     // unchanged if it can't be found
     $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
 }
Example #10
0
    /**
     * @expectedException \InvalidArgumentException
     */
    public function testTransChoiceFallbackWithNoTranslation()
    {
        $translator = new Translator('ru', new MessageSelector());
        $translator->setFallbackLocale('en');
        $translator->addLoader('array', new ArrayLoader());

        $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
    }
 /**
  * Gets the translator required for checking the DoubleLocale tests (en_UK etc)
  */
 private function getNonRedirectingHostMapRouter($config = 'routing.yml')
 {
     $container = new Container();
     $container->set('routing.loader', new YamlFileLoader(new FileLocator(__DIR__ . '/Fixture')));
     $translator = new Translator('en_UK', new MessageSelector());
     $translator->setFallbackLocale('en');
     $translator->addLoader('yml', new TranslationLoader());
     $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.en_UK.yml'), 'en_UK', 'routes');
     $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.en_US.yml'), 'en_US', 'routes');
     $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.nl.yml'), 'nl', 'routes');
     $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.en.yml'), 'en', 'routes');
     $container->set('i18n_loader', new I18nLoader(new DefaultRouteExclusionStrategy(), new DefaultPatternGenerationStrategy('custom', $translator, array('en_UK', 'en_US', 'nl_NL', 'nl_BE'), sys_get_temp_dir(), 'routes', 'en_UK')));
     $router = new I18nRouter($container, $config);
     $router->setRedirectToHost(false);
     $router->setI18nLoaderId('i18n_loader');
     $router->setDefaultLocale('en_UK');
     $router->setHostMap(array('en_UK' => 'uk.test', 'en_US' => 'us.test', 'nl_NL' => 'nl.test', 'nl_BE' => 'be.test'));
     return $router;
 }
 private function getRouter($config = 'routing.yml', $translator = null)
 {
     $container = new Container();
     $container->set('routing.loader', new YamlFileLoader(new FileLocator(__DIR__ . '/Fixture')));
     if (null === $translator) {
         $translator = new Translator('en', new MessageSelector());
         $translator->setFallbackLocale('en');
         $translator->addLoader('yml', new TranslationLoader());
         $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.de.yml'), 'de', 'routes');
         $translator->addResource('yml', file_get_contents(__DIR__ . '/Fixture/routes.en.yml'), 'en', 'routes');
     }
     $container->set('i18n_loader', new I18nLoader($translator, array('en', 'de', 'fr'), 'en', 'routes', 'custom', sys_get_temp_dir()));
     $router = new I18nRouter($container, $config);
     $router->setI18nLoaderId('i18n_loader');
     $router->setDefaultLocale('en');
     return $router;
 }
Example #13
0
 protected function setupForms()
 {
     // Set up the CSRF provider
     $csrfProvider = new DefaultCsrfProvider(CSRF_SECRET);
     // Set up the Translation component
     $translator = new Translator('en');
     $translator->setFallbackLocale(array('en'));
     $translator->setLocale('en');
     $translator->addLoader('xlf', new XliffFileLoader());
     $translator->addResource('xlf', VENDOR_FORM_DIR . '/Resources/translations/validators.en.xlf', 'en', 'validators');
     $translator->addResource('xlf', VENDOR_VALIDATOR_DIR . '/Resources/translations/validators.en.xlf', 'en', 'validators');
     // Set up the Validator component
     $validator = Validation::createValidatorBuilder()->setTranslator($translator)->setTranslationDomain('validators')->getValidator();
     $formEngine = new TwigRendererEngine(array(DEFAULT_FORM_THEME));
     $twig = $this->view->getInstance();
     $formEngine->setEnvironment($twig);
     $twig->addExtension(new TranslationExtension($translator));
     $twig->addExtension(new FormExtension(new TwigRenderer($formEngine, $csrfProvider)));
     // Set up the Form component
     $this->form = Forms::createFormFactoryBuilder()->addExtension(new CsrfExtension($csrfProvider))->addExtension(new ValidatorExtension($validator))->getFormFactory();
 }