本文整理汇总了PHP中Symfony\Component\Translation\Translator::addLoader方法的典型用法代码示例。如果您正苦于以下问题:PHP Translator::addLoader方法的具体用法?PHP Translator::addLoader怎么用?PHP Translator::addLoader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Translation\Translator
的用法示例。
在下文中一共展示了Translator::addLoader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: __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);
}
示例4: 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());
}
示例5: __construct
/**
* Contructor
*/
public function __construct()
{
if ($this->config()->exists('lang')) {
$this->lang = $this->config()->get('lang');
} else {
$this->lang = 'en';
}
$this->adaptor = new Translator($this->lang);
$this->adaptor->addLoader('array', new ArrayLoader());
}
示例6: register
public function register(Application $app)
{
$app['translator'] = function ($app) {
$translator = new Translator($app['config']['app.locale']);
$translator->addLoader('mo', new MoFileLoader());
$translator->addLoader('po', new PoFileLoader());
$translator->addLoader('array', new ArrayLoader());
return $translator;
};
}
示例7: main
/**
* {@inheritdoc}
*/
public function main(App $app)
{
$app['translator'] = function () {
$translator = new Translator($this->getLocale());
$translator->addLoader('php', new PhpFileLoader());
$translator->addLoader('mo', new MoFileLoader());
$translator->addLoader('po', new PoFileLoader());
$translator->addLoader('array', new ArrayLoader());
$this->loadLocale($this->getLocale(), $translator);
return $translator;
};
require __DIR__ . '/../functions.php';
}
示例8: __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']);
}
示例9: 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;
}
示例10: 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));
}
示例11: 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());
}
示例12: register
/**
* Initialize translator services.
*
* @param Pimple\Container $container
*
* @return Pimple\Container
*/
public function register(Container $container)
{
/**
* This service have to be called once a controller has
* been matched! Never before.
*/
$container['translator.locale'] = function ($c) {
if ($c['session']->get('_locale') != "") {
return $c['session']->get('_locale');
} else {
return $c['request']->getLocale();
}
};
$container['translator'] = function ($c) {
$c['stopwatch']->start('initTranslations');
$translator = new Translator($c['translator.locale'], null, ROADIZ_ROOT . '/cache/translations', (bool) $c['config']['devMode']);
$translator->addLoader('xlf', new XliffFileLoader());
/*
* Chosen language translations
*/
$this->addTranslatorResource($translator, ROADIZ_ROOT . '/src/Roadiz/CMS/Resources/translations', 'xlf', $c['translator.locale']);
$this->addTranslatorResource($translator, ROADIZ_ROOT . '/themes/Install/Resources/translations', 'xlf', $c['translator.locale']);
$classes = [$c['backendTheme']];
$classes = array_merge($classes, $c['frontendThemes']);
foreach ($classes as $theme) {
if (null !== $theme) {
$themeClass = $theme->getClassName();
$this->addTranslatorResource($translator, $themeClass::getResourcesFolder() . '/translations', 'xlf', $c['translator.locale']);
}
}
$c['stopwatch']->stop('initTranslations');
return $translator;
};
return $container;
}
示例13: 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');
}
示例14: 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;
}
示例15: 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())));
}