本文整理汇总了PHP中Symfony\Component\Translation\Translator类的典型用法代码示例。如果您正苦于以下问题:PHP Translator类的具体用法?PHP Translator怎么用?PHP Translator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Translator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testTransChoice
/**
* @dataProvider getTransChoiceTests
*/
public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
{
$translator = new Translator('en', new MessageSelector());
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array($id => $translation), $locale, $domain);
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
}
示例2: 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())));
}
示例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: render
public function render($route, $log = null)
{
$template_path = $route->join('/') . '/' . $this->context->template . '.tpl';
$template_base_path = APP_ROOT . '/Views';
$cache = false;
if (isset(\Config\Base::$caching) && \Config\Base::$caching == true) {
$cache = \Config\Base::$caching['twig'] ? APP_ROOT . '/cache' : false;
}
$loader = new \Bacon\Loader($template_base_path, $template_path);
$twig = new \Twig_Environment($loader, ['cache' => $cache, 'auto_reload' => true]);
if (!empty($this->context->template_base_paths)) {
foreach ($this->context->template_base_paths as $path) {
$loader->addPath($path);
}
}
if (!empty($this->context->filters)) {
foreach ($this->context->filters->getArrayCopy() as $name => $function) {
$filter_function = new \Twig_Filter_Function($function, ['is_safe' => ['html']]);
$twig->addFilter($name, $filter_function);
}
}
if (!empty($this->context->locale)) {
$locale = $this->context->locale;
$translator = new Translator($locale, new MessageSelector());
$translator->addLoader('po', new PoFileLoader());
$translator->addResource('po', APP_ROOT . '/locales/' . $locale . '/' . $this->context->locale_file . '.po', $locale);
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\TranslationExtension($translator));
}
echo $twig->render($template_path, $this->context->getArrayCopy());
}
示例5: loader
public function loader($locale = 'es_ES')
{
$Translator = new Translator($locale);
$Translator->addLoader('array', new ArrayLoader());
$data = array();
$part_locale = explode('_', $locale);
$bundles = Service::getBundles();
foreach ($bundles as $bundle) {
$path_i18n = str_replace('\\', '/', $bundle->getPath() . '/i18n/' . $part_locale[0]);
if (is_dir($path_i18n)) {
$finder = new Finder();
$finder->files()->name('*.i18n.php')->in($path_i18n);
// Une todos los esquemas en un solo array
foreach ($finder as $file) {
$_a = (require $file->getRealpath());
$data = array_merge($data, $_a);
}
}
}
$path_i18n = str_replace('\\', '/', Ki_APP . 'src/i18n/' . $part_locale[0]);
if (is_dir($path_i18n)) {
$finder = new Finder();
$finder->files()->name('*.i18n.php')->in($path_i18n);
// Une todos los esquemas en un solo array
foreach ($finder as $file) {
$_a = (require $file->getRealpath());
$data = array_merge($data, $_a);
}
}
$Translator->addResource('array', $data, $locale);
$this->Translator = $Translator;
}
示例6: addResourcesToTranslator
/**
* @return TranslatorInterface
*/
private function addResourcesToTranslator(Translator $translator)
{
foreach ($this->resourceFinder->findInDirectory($this->translationDir) as $resource) {
$translator->addResource($resource['format'], $resource['pathname'], $resource['locale'], $resource['domain']);
}
return $translator;
}
示例7: create
/**
* Create translator
*
* @return Translator
*/
public static function create(ServiceContainer $app)
{
$locale = $app->session->get('user.locale', 'en_US');
$translator = new Translator($locale, new MessageSelector());
$translator->addLoader('mo', new MoFileLoader());
return $translator;
}
示例8: ru
/**
* Plural rules for russian language.
*/
public static function ru($string, $number)
{
$translator = new Translator('ru');
$translated = $translator->trans(':count ' . $string, array(':count' => $number));
$type = $number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 >= 2 && $number % 10 <= 4 && ($number % 100 < 10 || $number % 100 >= 20) ? 1 : 2);
$translated_array = explode("|", $translated);
return $translated_array[$type];
}
示例9: 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);
}
示例10: 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();
}
示例11: register
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;
}
示例12: 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');
}
}
示例13: get
/**
* @param FormView $formView
*
* @return string
* @author Michaël VEROUX
*/
public function get(FormView $formView)
{
$value = $formView->vars['value'];
$display = array();
$expanded = isset($formView->vars['expanded']) && $formView->vars['expanded'];
$multiple = isset($formView->vars['multiple']) && $formView->vars['multiple'];
if ($expanded && $multiple) {
$value = array_filter($value, function ($val) {
return $val;
});
$value = array_keys($value);
}
foreach ($formView->vars['choices'] as $key => $choiceView) {
if ($multiple) {
if ($expanded) {
$compare = $key;
} else {
$compare = $choiceView->value;
}
if (in_array($compare, $value)) {
$display[] = $this->translator->trans($choiceView->label, array(), $formView->vars['translation_domain']);
}
} else {
if ($value == $choiceView->value) {
return $this->translator->trans($choiceView->label, array(), $formView->vars['translation_domain']);
}
}
}
if (!count($display)) {
return $formView->vars['placeholder'];
}
return implode(PHP_EOL, $display);
}
示例14: runJobs
/**
* @param $jobs
* @throws UnauthorizedCommandException
*/
protected function runJobs($jobs)
{
foreach ($jobs as $job) {
/**
* @var \WeavingTheWeb\Bundle\ApiBundle\Entity\Job $job
*/
$command = $job->getValue();
try {
$this->validateCommand($command);
$job->setStatus(JobInterface::STATUS_STARTED);
$this->updateJob($job);
$command = $this->getApplication()->get($command);
$success = $command->run(new ArrayInput(['command' => $command, '--job' => $job->getId()]), $this->output);
if (intval($success) === 0) {
$job->setStatus(JobInterface::STATUS_FINISHED);
} else {
$job->setStatus(JobInterface::STATUS_FAILED);
}
$this->updateJob($job);
} catch (UnauthorizedCommandException $exception) {
$message = $this->translator->trans('job.run.unauthorized', ['{{ command }}' => $command], 'job');
$this->logger->error($exception->getMessage());
$this->output->writeln($message);
$job->setStatus(JobInterface::STATUS_FAILED);
$this->updateJob($job);
continue;
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
continue;
}
}
}
示例15: execute
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getOption('lang')) {
return;
}
$this->translator->setLocale($input->getOption('lang'));
}