本文整理汇总了PHP中Symfony\Component\Translation\Translator::addResource方法的典型用法代码示例。如果您正苦于以下问题:PHP Translator::addResource方法的具体用法?PHP Translator::addResource怎么用?PHP Translator::addResource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Translation\Translator
的用法示例。
在下文中一共展示了Translator::addResource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testDefaultTranslationDomain
public function testDefaultTranslationDomain()
{
$templates = array('index' => '
{%- extends "base" %}
{%- trans_default_domain "foo" %}
{%- block content %}
{%- trans %}foo{% endtrans %}
{%- trans from "custom" %}foo{% endtrans %}
{{- "foo"|trans }}
{{- "foo"|trans({}, "custom") }}
{{- "foo"|transchoice(1) }}
{{- "foo"|transchoice(1, {}, "custom") }}
{% endblock %}
', 'base' => '
{%- block content "" %}
');
$translator = new Translator('en', new MessageSelector());
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
$translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
$template = $this->getTemplate($templates, $translator);
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
}
示例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: execute
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$i18nPath = dirname(dirname(dirname(dirname(dirname(__DIR__))))) . DIRECTORY_SEPARATOR . 'i18n.php';
foreach (require $i18nPath as $lang => $messages) {
$this->translator->addResource('array', $messages, $lang, 'output');
}
}
示例4: __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);
}
示例5: loadTranslator
/**
* @param ContainerInterface $container
* @return Translator
*/
function loadTranslator(ContainerInterface $container)
{
$translator = new Translator('ru');
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', VENDOR_PATH . 'symfony/form/Resources/translations/validators.ru.xlf', 'ru', 'validators');
$translator->addResource('xlf', VENDOR_PATH . 'symfony/validator/Resources/translations/validators.ru.xlf', 'ru', 'validators');
$container->set('translator', $translator);
return $translator;
}
示例6: setUp
public function setUp()
{
$this->translator = new Translator("en_US");
$this->translator->addLoader('array', new ArrayLoader());
$this->translator->addResource('array', array('Hello %name%' => 'Ciao %name%'), 'it', 'dom');
$loader = new \Twig_Loader_String();
$this->twig = new \Twig_Environment($loader);
$this->twig->addExtension(new TranslationExtension($this->translator));
$this->twig->addExtension(new SafeTransExtension());
}
示例7: getTranslator
/**
* Returns a Translator instance.
*
* @param string $locale
*
* @return Translator
*/
public function getTranslator($locale)
{
$translator = new Translator($locale, new MessageSelector());
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('sonata_template_box_media_gallery_block' => 'This is the default message'), 'en', 'SonataCoreBundle');
$translator->addResource('array', array('sonata_template_box_media_gallery_block' => 'Ceci est le message par défaut'), 'fr', 'SonataCoreBundle');
$translator->addResource('array', array('sonata_core_template_box_file_found_in' => 'This file can be found in'), 'en', 'SonataCoreBundle');
$translator->addResource('array', array('sonata_core_template_box_file_found_in' => "Ce fichier peut être trouvé à l'emplacement"), 'fr', 'SonataCoreBundle');
return $translator;
}
示例8: createCollector
private function createCollector()
{
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('foo' => 'foo (en)'), 'en');
$translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
$translator->addResource('array', array('bar_ru' => 'bar (ru)'), 'ru');
$collector = new DataCollectorTranslator($translator);
return $collector;
}
示例9: setUp
protected function setUp()
{
$translator = new Translator('en', new MessageSelector());
$keywords = new SymfonyTranslationKeywords($translator);
$parser = new Parser(new Lexer($keywords));
$this->loader = new GherkinFileLoader($parser);
$translator->addLoader('xliff', new XliffFileLoader());
$translator->addResource('xliff', __DIR__ . '/../../../../i18n/en.xliff', 'gherkin');
$translator->addResource('xliff', __DIR__ . '/../../../../i18n/ru.xliff', 'gherkin');
$this->featuresPath = realpath(__DIR__ . '/../Fixtures/features');
}
示例10: build
/**
* Builds a translator
* @return \Symfony\Component\Translation\TranslatorInterface
*/
public function build()
{
$fixer = new VendorDirectoryFixer();
// Set up the Translation component
$translator = new Translator($this->locale);
$pos = strpos($this->locale, '_');
$file = 'validators.' . ($pos ? substr($this->locale, 0, $pos) : $this->locale) . '.xlf';
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', $fixer->getLocation('form', self::FORM_TRANSLATIONS_DIR . $file), $this->locale, self::TRANSLATION_DOMAIN);
$translator->addResource('xlf', $fixer->getLocation('validator', self::VALIDATOR_TRANSLATIONS_DIR . $file), $this->locale, self::TRANSLATION_DOMAIN);
return $translator;
}
示例11: testDomains
/**
* Test missing messages in different domains
*/
public function testDomains()
{
$this->translator->addResource('array', ['m1' => 'Hello world', 'm2' => 'Testing message'], 'en_US', 'test');
$this->translator->addResource('array', ['m1' => 'Ahoj světe', 'm2' => 'Testovací zpráva'], 'cs_CZ', 'test2');
$missing = $this->validator->validate($this->translator, ['cs_CZ', 'en_US']);
$this->assertEquals(2, count($missing['cs_CZ']));
$this->assertContains('test.m1', $missing['cs_CZ']);
$this->assertContains('test.m2', $missing['cs_CZ']);
$this->assertEquals(2, count($missing['en_US']));
$this->assertContains('test2.m1', $missing['en_US']);
$this->assertContains('test2.m2', $missing['en_US']);
}
示例12: 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;
}
示例13: __construct
/**
* Block constructor.
* @throws InvalidArgumentException
*/
private function __construct()
{
self::$translator = new Translator('en', new MessageSelector());
self::$translator->addLoader('php', new PhpFileLoader());
foreach (glob(__DIR__ . '/../../locales/messages.*.php') as $file) {
if (preg_match('/\\.([\\w-]+)\\.php$/', basename($file), $fileLocaleMatch)) {
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
self::$translator->addResource('php', $file, $fileLocaleMatch[1]);
}
}
self::configureLocale();
// Reconfigure locale if it was changed.
EventListener::$global->on('rentalhost.validation::option.set.locale', [self::class, 'configureLocale']);
}
示例14: 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));
}
示例15: 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));
}