本文整理汇总了PHP中Symfony\Component\Translation\Translator::setLocale方法的典型用法代码示例。如果您正苦于以下问题:PHP Translator::setLocale方法的具体用法?PHP Translator::setLocale怎么用?PHP Translator::setLocale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Translation\Translator
的用法示例。
在下文中一共展示了Translator::setLocale方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getOption('lang')) {
return;
}
$this->translator->setLocale($input->getOption('lang'));
}
示例2: __construct
public function __construct($language)
{
$this->translator = new Translator(null);
$this->translator->addLoader('array', new ArrayLoader());
$this->translator->addResource('array', array('Hello World' => 'Olá mundo'), 'pt');
$this->translator->addResource('array', array('Hello World' => 'Hola Mundo'), 'es');
$this->translator->setLocale($language);
}
示例3: 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);
}
}
示例4: testSetGetLocale
public function testSetGetLocale()
{
$translator = new Translator('en', new MessageSelector());
$this->assertEquals('en', $translator->getLocale());
$translator->setLocale('fr');
$this->assertEquals('fr', $translator->getLocale());
}
示例5: testGetCatalogue
public function testGetCatalogue()
{
$translator = new Translator('en');
$this->assertEquals(new MessageCatalogue('en'), $translator->getCatalogue());
$translator->setLocale('fr');
$this->assertEquals(new MessageCatalogue('fr'), $translator->getCatalogue('fr'));
}
示例6: setLocale
public function setLocale($locale)
{
if (null === $locale) {
return;
}
$this->app['locale'] = $locale;
parent::setLocale($locale);
}
示例7: execute
/**
* Validate the security.yml and the configuration.yml.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$language = $input->getArgument('language');
if (in_array($language, array('de', 'en')) == true) {
$this->translator->setLocale($language);
}
if (is_dir($this->configDir) == true) {
if ($this->validateSecurity($output) == true) {
$output->writeln($this->translator->trans('validation.success', array('%file%' => 'security')));
}
if ($this->validateConfiguration($output) == true) {
$output->writeln($this->translator->trans('validation.success', array('%file%' => 'configuration')));
}
} else {
$output->writeln($this->translator->trans('validation.error.dir'));
}
}
示例8: execute
/**
* Validate the security.yml and the configuration.yml.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$language = $input->getArgument('language');
if (in_array($language, array('de', 'en')) == true) {
$this->translator->setLocale($language);
}
foreach ($this->installs as $install) {
if ($this->createDir($install['target']) == true) {
if (isset($install['source']) == true) {
if ($this->copyFiles($output, $install['source'], $install['target'], $install['files']) == false) {
return;
}
}
} else {
$output->writeln($this->translator->trans('install.dir.create', array('%dir%' => $install['target'])));
return;
}
}
$output->writeln($this->translator->trans('install.success'));
}
示例9: testRefreshCacheWhenResourcesChange
public function testRefreshCacheWhenResourcesChange()
{
// prime the cache
$loader = $this->getMock('Symfony\\Component\\Translation\\Loader\\LoaderInterface');
$loader->method('load')->will($this->returnValue($this->getCatalogue('fr', array('foo' => 'foo A'))));
$translator = new Translator('fr', null, $this->tmpDir, true);
$translator->setLocale('fr');
$translator->addLoader('loader', $loader);
$translator->addResource('loader', 'foo', 'fr');
$this->assertEquals('foo A', $translator->trans('foo'));
// add a new resource to refresh the cache
$loader = $this->getMock('Symfony\\Component\\Translation\\Loader\\LoaderInterface');
$loader->method('load')->will($this->returnValue($this->getCatalogue('fr', array('foo' => 'foo B'))));
$translator = new Translator('fr', null, $this->tmpDir, true);
$translator->setLocale('fr');
$translator->addLoader('loader', $loader);
$translator->addResource('loader', 'bar', 'fr');
$this->assertEquals('foo B', $translator->trans('foo'));
}
示例10: testSetValidLocale
/**
* @dataProvider getValidLocalesTests
*/
public function testSetValidLocale($locale)
{
$translator = new Translator($locale, new MessageSelector());
$translator->setLocale($locale);
$this->assertEquals($locale, $translator->getLocale());
}
示例11: setLocale
public function setLocale($locale)
{
$this->translator->setLocale($locale);
}
示例12: setLocale
/**
* Megpróbálja beállítani a paraméterben megadott locale-t. Ha nincsen, megpróbálja betölteni a HTTP_ACCEPT_LANGUAGE
* szerver változóból a locale-t, de ha az nem támogatott, akkor legvégül a default nyelvet tölti be.
*
* Igaz a visszatérési érték, ha sikerült beállítani a paraméterben megadott locale-t. Minden más esetben hamis.
*
* @param string $locale
* @return bool
*/
public function setLocale($locale)
{
if ($this->isSupportedLocale($locale)) {
//Ha létezik ilyen támogatott locale, akkor beállítjuk
$validLocale = $locale;
$success = true;
} else {
//Ha nem létezik megpróbáljuk beállítani a HTTP_ACCEPT_LANGUAGE változóból
$httpLocale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($this->isSupportedLocale($httpLocale)) {
$validLocale = $httpLocale;
} else {
$validLocale = $this->configuration["fallbackLocales"][0];
}
$success = false;
}
$this->translator->setLocale($validLocale);
// Adott stratégiák esetén be kell állítani a cookie-kat is
if ($this->configuration["strategy"] == Translator::STRATEGY_ONLY_COOKIE || $this->configuration["strategy"] == Translator::STRATEGY_COOKIE_AND_URL) {
$this->setLocaleToCookie($validLocale);
}
return $success;
}
示例13: setLocale
public function setLocale($locale)
{
$this->app['locale'] = $locale;
parent::setLocale($locale);
}
示例14: __construct
* @Assert\NotBlank(message = "user.name.not_blank")
*/
private $name;
/**
* @Assert\Email
* @Assert\NotBlank
*/
private $email;
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
/**
* @Assert\IsTrue(message = "The user should have a Google Mail account")
*/
public function isGmailUser()
{
return false !== strpos($this->email, '@gmail.com');
}
}
$trans = new Translator('es');
$trans->setLocale("zh");
$trans->addLoader('xliff', new XliffFileLoader());
$trans->addResource('xliff', __DIR__ . '/Resources/translations/validators.en.xlf', 'en');
$trans->addResource('xliff', __DIR__ . '/Resources/translations/validators.es.xlf', 'es');
$trans->addResource('xliff', __DIR__ . '/Resources/translations/validators.zh.xlf', 'zh');
$validator = Validation::createValidatorBuilder()->setTranslator($trans)->enableAnnotationMapping()->getValidator();
$user = new User('', 'john@example.com');
$violations = $validator->validate($user);
var_dump($violations->get(0)->getMessage());
示例15: setupForms
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();
}