php Symfony-Component-Translation-Translator类(方法)实例源码

下面列出了php Symfony-Component-Translation-Translator 类(方法)源码代码实例,从而了解它的用法。

作者:netixpr    项目:symfon   
/**
  * @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));
 }

作者:makhloufi-louni    项目:tuto_symfon   
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())));
    }

作者:laast    项目:symfony-translation-provide   
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');
 }

作者:brainswar    项目:baco   
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());
 }

作者:kodazz    项目:framewor   
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;
 }

作者:TomasVotrub    项目:tomasvotruba.c   
/**
  * @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;
 }

作者:oda    项目:moleng   
/**
  * 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;
 }

作者:SolidWe    项目:Hagakur   
/**
  * 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];
 }

作者:nebiro    项目:yas   
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);
}

作者:jeanpasqualini-lesso    项目:lesson-translatio   
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();
 }

作者:ubic    项目:glu   
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;
 }

作者:Athorci    项目:athorrent-fronten   
public function extendTranslator(Translator $translator, Application $app)
 {
     $translator->addLoader('yaml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
     foreach ($app['locales'] as $locale) {
         $translator->addResource('yaml', LOCALES . '/' . $locale . '.yml', $locale);
     }
     return $translator;
 }

作者:clavier-souri    项目:SonataAdminBundl   
public function setUp()
 {
     date_default_timezone_set('Europe/London');
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $this->pool = new Pool($container, '', '');
     $this->pool->setAdminServiceIds(array('sonata_admin_foo_service'));
     $this->pool->setAdminClasses(array('fooClass' => array('sonata_admin_foo_service')));
     $this->logger = $this->getMock('Psr\\Log\\LoggerInterface');
     $this->twigExtension = new SonataAdminExtension($this->pool, $this->logger);
     $loader = new StubFilesystemLoader(array(__DIR__ . '/../../../Resources/views/CRUD'));
     $this->environment = new \Twig_Environment($loader, array('strict_variables' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0));
     $this->environment->addExtension($this->twigExtension);
     // translation extension
     $translator = new Translator('en', new MessageSelector());
     $translator->addLoader('xlf', new XliffFileLoader());
     $translator->addResource('xlf', __DIR__ . '/../../../Resources/translations/SonataAdminBundle.en.xliff', 'en', 'SonataAdminBundle');
     $this->environment->addExtension(new TranslationExtension($translator));
     // routing extension
     $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__ . '/../../../Resources/config/routing')));
     $routeCollection = $xmlFileLoader->load('sonata_admin.xml');
     $xmlFileLoader = new XmlFileLoader(new FileLocator(array(__DIR__ . '/../../Fixtures/Resources/config/routing')));
     $testRouteCollection = $xmlFileLoader->load('routing.xml');
     $routeCollection->addCollection($testRouteCollection);
     $requestContext = new RequestContext();
     $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
     $this->environment->addExtension(new RoutingExtension($urlGenerator));
     $this->environment->addExtension(new \Twig_Extensions_Extension_Text());
     $this->twigExtension->initRuntime($this->environment);
     // initialize object
     $this->object = new \stdClass();
     // initialize admin
     $this->admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $this->admin->expects($this->any())->method('getCode')->will($this->returnValue('xyz'));
     $this->admin->expects($this->any())->method('id')->with($this->equalTo($this->object))->will($this->returnValue(12345));
     $this->admin->expects($this->any())->method('getNormalizedIdentifier')->with($this->equalTo($this->object))->will($this->returnValue(12345));
     $this->admin->expects($this->any())->method('trans')->will($this->returnCallback(function ($id) {
         return $id;
     }));
     $this->adminBar = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $this->adminBar->expects($this->any())->method('isGranted')->will($this->returnValue(true));
     $this->adminBar->expects($this->any())->method('getNormalizedIdentifier')->with($this->equalTo($this->object))->will($this->returnValue(12345));
     // for php5.3 BC
     $admin = $this->admin;
     $adminBar = $this->adminBar;
     $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use($admin, $adminBar) {
         if ($id == 'sonata_admin_foo_service') {
             return $admin;
         } elseif ($id == 'sonata_admin_bar_service') {
             return $adminBar;
         }
         return;
     }));
     // initialize field description
     $this->fieldDescription = $this->getMock('Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface');
     $this->fieldDescription->expects($this->any())->method('getName')->will($this->returnValue('fd_name'));
     $this->fieldDescription->expects($this->any())->method('getAdmin')->will($this->returnValue($this->admin));
     $this->fieldDescription->expects($this->any())->method('getLabel')->will($this->returnValue('Data'));
 }

作者:eunico    项目:meetu   
public function testSetTranslator()
 {
     $t = new Translator('fr');
     $t->addLoader('array', new ArrayLoader());
     CarbonInterval::setTranslator($t);
     $t = CarbonInterval::getTranslator();
     $this->assertNotNull($t);
     $this->assertSame('fr', $t->getLocale());
 }

作者:hostne    项目:entity-translation-bundl   
/**
  * @expectedException \RuntimeException
  */
 public function testNonEnumLoad()
 {
     $yml_loader = new YamlFileLoader();
     $loader = new EnumLoader($yml_loader);
     $translator = new Translator("en");
     $translator->addLoader("enum", $loader);
     $translator->addResource("enum", __DIR__ . "/../Mock/Resources/translations/enum.en.yml", "en", "phpunit");
     $translator->trans(0, [], "phpunit");
 }

作者:oda    项目:dand   
/**
 * Create translator
 *
 * @return Translator
 */
function translator($locale = 'en_US')
{
    static $translator = null;
    if ($translator === null) {
        $translator = new Translator($locale, new MessageSelector());
        $translator->addLoader('mo', new MoFileLoader());
    }
    return $translator;
}

作者:miknat    项目:grac   
public function getFormattedErrors(Translator $translator)
 {
     $formattedErrors = array();
     foreach ($this->errors as $error) {
         /** @var $error ConstraintViolation */
         $formattedErrors[$error->getPropertyPath()][] = $translator->trans($error->getMessage(), array(), 'validators');
     }
     return $formattedErrors;
 }

作者:hml    项目:dat   
/**
  * @dataProvider providerLocales
  *
  * @param string $locale
  */
 public function testSetTranslator($locale)
 {
     $t = new Translator($locale);
     $t->addLoader('array', new ArrayLoader());
     Date::setTranslator($t);
     $t = Date::getTranslator();
     $this->assertNotNull($t);
     $this->assertSame($locale, $t->getLocale());
 }

作者:GrizliK198    项目:symfony-certification-prepare-projec   
/**
 * @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;
}

作者:OverByTher    项目:Beha   
/**
  * {@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');
     }
 }


问题


面经


文章

微信
公众号

扫码关注公众号