/** * Get translator object. * * @return \Zend\I18n\Translator\Translator */ public function getTranslator() { if (null === $this->translator) { $this->translator = clone $this->getServiceLocator()->get('Translator'); $this->translator->addTranslationFile('ExtendedIni', APPLICATION_PATH . '/languages/native.ini', 'default', 'native'); $this->translator->setLocale('native'); } return $this->translator; }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { if (!$request->hasHeader('Accept-Language')) { return $out($request, $response); } $locale = $request->getHeaderLine('Accept-Language'); $this->translator->setLocale($this->normalizeLocale($locale)); return $out($request, $response); }
public function testTranslatePlurals() { $this->translator->setLocale('en_EN'); $this->translator->addTranslationFile('phparray', $this->testFilesDir . '/translation_en.php', 'default', 'en_EN'); $pl0 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 1); $pl1 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 2); $pl2 = $this->translator->translatePlural('Message 5', 'Message 5 Plural', 10); $this->assertEquals('Message 5 (en) Plural 0', $pl0); $this->assertEquals('Message 5 (en) Plural 1', $pl1); $this->assertEquals('Message 5 (en) Plural 2', $pl2); }
public function init() { $this->app->configs->setDefaultConfig($this->id, new TranslatorConfig(), $this); $this->translator = $translator = new Translator(); /** @var TranslatorConfig $config */ $this->moduleConfig = $config = $this->app->configs->getConfig($this->id); if (!$this->app->getConfig()->developmentMode) { $c = new Filesystem(); $o = new FilesystemOptions(); $o->setCacheDir($this->app->cacheStorage->createStorage($this->id)); $c->setOptions($o); $c->addPlugin(new Serializer()); $translator->setCache($c); } $translator->setLocale($config->defaultLanguage); $folder = $this->app->parseUri($config->translationsDirectory); foreach ($config->contexts as $context => $file) { if (is_int($context)) { $context = $file; $file .= '.mo'; } $translator->addTranslationFilePattern('gettext', $folder, '%s/' . $file, $context); } $this->translator = $translator; }
public function testTranslateNonExistantLocale() { $this->translator->addTranslationFilePattern('phparray', $this->testFilesDir . '/testarray', 'translation-%s.php'); // Test that a locale without translations does not cause warnings $this->translator->setLocale('es_ES'); $this->assertEquals('Message 1', $this->translator->translate('Message 1')); $this->assertEquals('Message 9', $this->translator->translate('Message 9')); $this->translator->setLocale('fr_FR'); $this->assertEquals('Message 1', $this->translator->translate('Message 1')); $this->assertEquals('Message 9', $this->translator->translate('Message 9')); }
public function onBootstrap(MvcEvent $e) { $eventManager = $e->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager); $eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'protectPage'], -100); $t = new I18nTranslator(); $t->setLocale('de_DE'); $translator = new Translator($t); $translator->addTranslationFile('phpArray', 'vendor/zendframework/zend-i18n-resources/languages/de/Zend_Validate.php', 'default', 'de_DE'); AbstractValidator::setDefaultTranslator($translator); }
public function createService(ServiceLocatorInterface $sm) { $configManager = $sm->get('Base\\Manager\\ConfigManager'); $locale = $configManager->need('i18n.locale'); $translator = new Translator(); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/backend.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/base.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/booking.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/calendar.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/frontend.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/service.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/setup.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/square.php'); $translator->addTranslationFilePattern('phparray', getcwd() . '/data/res/i18n/', '%s/user.php'); $translator->setLocale($locale); return new \Zend\Mvc\I18n\Translator($translator); }
/** * @return Translator */ public function getTranslator() { $translator = new Translator(); $translator->setLocale('en-US'); $enLoader = $this->getMock('Zend\\I18n\\Translator\\Loader\\FileLoaderInterface'); $deLoader = $this->getMock('Zend\\I18n\\Translator\\Loader\\FileLoaderInterface'); $domainLoader = $this->getMock('Zend\\I18n\\Translator\\Loader\\FileLoaderInterface'); $enLoader->expects($this->any())->method('load')->willReturn(new TextDomain(['fw' => 'framework'])); $deLoader->expects($this->any())->method('load')->willReturn(new TextDomain(['fw' => 'baukasten'])); $domainLoader->expects($this->any())->method('load')->willReturn(new TextDomain(['fw' => 'fw-alternative'])); $translator->getPluginManager()->setService('test-en', $enLoader); $translator->getPluginManager()->setService('test-de', $deLoader); $translator->getPluginManager()->setService('test-domain', $domainLoader); $translator->addTranslationFile('test-en', null, 'default', 'en-US'); $translator->addTranslationFile('test-de', null, 'default', 'de-DE'); $translator->addTranslationFile('test-domain', null, 'alternative', 'en-US'); return $translator; }
public static function routeProvider() { $translator = new Translator(); $translator->setLocale('en-US'); $enLoader = new TestLoader(); $deLoader = new TestLoader(); $domainLoader = new TestLoader(); $enLoader->textDomain = new TextDomain(array('fw' => 'framework')); $deLoader->textDomain = new TextDomain(array('fw' => 'baukasten')); $domainLoader->textDomain = new TextDomain(array('fw' => 'fw-alternative')); $translator->getPluginManager()->setService('test-en', $enLoader); $translator->getPluginManager()->setService('test-de', $deLoader); $translator->getPluginManager()->setService('test-domain', $domainLoader); $translator->addTranslationFile('test-en', null, 'default', 'en-US'); $translator->addTranslationFile('test-de', null, 'default', 'de-DE'); $translator->addTranslationFile('test-domain', null, 'alternative', 'en-US'); return array('simple-match' => array(new Segment('/:foo'), '/bar', null, array('foo' => 'bar')), 'no-match-without-leading-slash' => array(new Segment(':foo'), '/bar/', null, null), 'no-match-with-trailing-slash' => array(new Segment('/:foo'), '/bar/', null, null), 'offset-skips-beginning' => array(new Segment(':foo'), '/bar', 1, array('foo' => 'bar')), 'offset-enables-partial-matching' => array(new Segment('/:foo'), '/bar/baz', 0, array('foo' => 'bar')), 'match-overrides-default' => array(new Segment('/:foo', array(), array('foo' => 'baz')), '/bar', null, array('foo' => 'bar')), 'constraints-prevent-match' => array(new Segment('/:foo', array('foo' => '\\d+')), '/bar', null, null), 'constraints-allow-match' => array(new Segment('/:foo', array('foo' => '\\d+')), '/123', null, array('foo' => '123')), 'constraints-override-non-standard-delimiter' => array(new Segment('/:foo{-}/bar', array('foo' => '[^/]+')), '/foo-bar/bar', null, array('foo' => 'foo-bar')), 'constraints-with-parantheses-dont-break-parameter-map' => array(new Segment('/:foo/:bar', array('foo' => '(bar)')), '/bar/baz', null, array('foo' => 'bar', 'bar' => 'baz')), 'simple-match-with-optional-parameter' => array(new Segment('/[:foo]', array(), array('foo' => 'bar')), '/', null, array('foo' => 'bar')), 'optional-parameter-is-ignored' => array(new Segment('/:foo[/:bar]'), '/bar', null, array('foo' => 'bar')), 'optional-parameter-is-provided-with-default' => array(new Segment('/:foo[/:bar]', array(), array('bar' => 'baz')), '/bar', null, array('foo' => 'bar', 'bar' => 'baz')), 'optional-parameter-is-consumed' => array(new Segment('/:foo[/:bar]'), '/bar/baz', null, array('foo' => 'bar', 'bar' => 'baz')), 'optional-group-is-discared-with-missing-parameter' => array(new Segment('/:foo[/:bar/:baz]', array(), array('bar' => 'baz')), '/bar', null, array('foo' => 'bar', 'bar' => 'baz')), 'optional-group-within-optional-group-is-ignored' => array(new Segment('/:foo[/:bar[/:baz]]', array(), array('bar' => 'baz', 'baz' => 'bat')), '/bar', null, array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat')), 'non-standard-delimiter-before-parameter' => array(new Segment('/foo-:bar'), '/foo-baz', null, array('bar' => 'baz')), 'non-standard-delimiter-between-parameters' => array(new Segment('/:foo{-}-:bar'), '/bar-baz', null, array('foo' => 'bar', 'bar' => 'baz')), 'non-standard-delimiter-before-optional-parameter' => array(new Segment('/:foo{-/}[-:bar]/:baz'), '/bar-baz/bat', null, array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat')), 'non-standard-delimiter-before-ignored-optional-parameter' => array(new Segment('/:foo{-/}[-:bar]/:baz'), '/bar/bat', null, array('foo' => 'bar', 'baz' => 'bat')), 'parameter-with-dash-in-name' => array(new Segment('/:foo-bar'), '/baz', null, array('foo-bar' => 'baz')), 'url-encoded-parameters-are-decoded' => array(new Segment('/:foo'), '/foo%20bar', null, array('foo' => 'foo bar')), 'urlencode-flaws-corrected' => array(new Segment('/:foo'), "/!\$&'()*,-.:;=@_~+", null, array('foo' => "!\$&'()*,-.:;=@_~+")), 'empty-matches-are-replaced-with-defaults' => array(new Segment('/foo[/:bar]/baz-:baz', array(), array('bar' => 'bar')), '/foo/baz-baz', null, array('bar' => 'bar', 'baz' => 'baz')), 'translate-with-default-locale' => array(new Segment('/{fw}', array(), array()), '/framework', null, array(), array('translator' => $translator)), 'translate-with-specific-locale' => array(new Segment('/{fw}', array(), array()), '/baukasten', null, array(), array('translator' => $translator, 'locale' => 'de-DE')), 'translate-uses-message-id-as-fallback' => array(new Segment('/{fw}', array(), array()), '/fw', null, array(), array('translator' => $translator, 'locale' => 'fr-FR')), 'translate-with-specific-text-domain' => array(new Segment('/{fw}', array(), array()), '/fw-alternative', null, array(), array('translator' => $translator, 'text_domain' => 'alternative'))); }
private function bootstrap($configuration) { // setup service manager $serviceManager = new ServiceManager(new ServiceManagerConfig()); $serviceManager->setService('ApplicationConfig', $configuration); // set translator $translator = new Translator(); $translator->addTranslationFilePattern('gettext', CASASYNC_PLUGIN_DIR . 'vendor/casasoft/casamodules/src/CasasoftStandards/language/', '%s.mo', 'casasoft-standards'); $translator->setLocale(substr(get_bloginfo('language'), 0, 2)); $serviceManager->setService('Translator', $translator); // mvc translator $MVCtranslator = new \Zend\Mvc\I18n\Translator($translator); $MVCtranslator->addTranslationFile('phpArray', CASASYNC_PLUGIN_DIR . 'resources/languages/' . substr(get_bloginfo('language'), 0, 2) . '/Zend_Validate.php', 'default'); \Zend\Validator\AbstractValidator::setDefaultTranslator($MVCtranslator); $this->translator = $translator; // load modules -- which will provide services, configuration, and more $serviceManager->get('ModuleManager')->loadModules(); //renderer $this->renderer = new PhpRenderer(); $pluginManager = $this->renderer->getHelperPluginManager(); //view helper plugins $defaultHelperMapClasses = ['Zend\\Form\\View\\HelperConfig', 'Zend\\I18n\\View\\HelperConfig', 'Zend\\Navigation\\View\\HelperConfig']; foreach ($defaultHelperMapClasses as $configClass) { if (is_string($configClass) && class_exists($configClass)) { $config = new $configClass(); $config->configureServiceManager($pluginManager); } } $this->serviceManager = $serviceManager; $this->queryService = $this->serviceManager->get('casawpQuery'); $this->categoryService = $this->serviceManager->get('CasasoftCategory'); $this->numvalService = $this->serviceManager->get('CasasoftNumval'); }
static function translate($lang = null) { $lang = ''; try { $config = Utility::getConfig(); $lang = isset($config['lang']) ? $config['lang'] : ''; } catch (\Exception $e) { } if ($lang == '') { $lang = 'en_us'; } $type = 'Gettext'; $pattern = $lang . '.mo'; $base_dir = __DIR__ . '/../../../../../language/'; $translator = new Translator(); $translator->setLocale("en"); $translator->addTranslationFilePattern($type, $base_dir, $pattern); return $translator; }
public function testForcedLocale() { $this->translator->setLocale('de_DE'); $this->assertEquals('de_DE', $this->translator->getLocale()); }
public function testGetLocale() { $this->assertEquals('en', $this->extension->getLocale()); $this->translator->setLocale('es'); $this->assertEquals('es', $this->extension->getLocale()); }
/** * Sets the default locale to use when translating messages. * * @param string $locale * * @api * * @return Translator */ public function setLocale($locale) { return parent::setLocale($locale); }