本文整理汇总了PHP中Zend\I18n\Translator\Translator类的典型用法代码示例。如果您正苦于以下问题:PHP Translator类的具体用法?PHP Translator怎么用?PHP Translator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Translator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: init
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;
}
示例2: __invoke
/**
* Translate a plural message.
*
* @param string $singular
* @param string $plural
* @param integer $number
* @param string $textDomain
* @param string $locale
* @return string
* @throws Exception\RuntimeException
*/
public function __invoke($singular, $plural, $number, $textDomain = 'default', $locale = null)
{
if ($this->translator === null) {
throw new Exception\RuntimeException('Translator has not been set');
}
return $this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
}
示例3: setUp
public function setUp()
{
$this->testFilesDir = __DIR__ . '/../_files';
$this->translator = new Translator();
$this->translator->addTranslationFile('phparray', $this->testFilesDir . '/tokens.en.php', 'route', 'en');
$this->translator->addTranslationFile('phparray', $this->testFilesDir . '/tokens.de.php', 'route', 'de');
$this->fooRoute = ['type' => 'Segment', 'options' => ['route' => '/:locale'], 'child_routes' => ['index' => ['type' => 'Segment', 'options' => ['route' => '/{homepage}']]]];
}
示例4: __invoke
/**
* @param $str
* @param pt_BR $locale
* @return string
*/
public function __invoke($str, $locale = 'pt_BR', $typeTranslate = 'PhpArray', $pathFileTranslate = null)
{
$translator = new Translator();
if (!empty($pathFileTranslate)) {
$translator->addTranslationFile($typeTranslate, $pathFileTranslate, 'default', $locale);
}
return $translator->translate($str, $locale);
}
示例5: getTranslator
/**
* 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;
}
示例6: onBootstrap
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);
}
示例7: createTranslatorAdapter
/**
* {@inheritDoc}
*/
public function createTranslatorAdapter($locale)
{
$cache = new ZendCacheDriver('cache/expensive');
$t = new Translator();
$t->setCache($cache);
$adapter = new TranslatorAdapter($t);
$adapter->setLocale($locale);
if (isset($this->translationLoaderRepository)) {
foreach ($this->translationLoaderRepository->getTranslationLoaders() as $key => $loader) {
$loader->loadTranslations($adapter);
}
}
return $adapter;
}
示例8: setUp
public function setUp()
{
$command = new GenerateCharsetCommand(Translator::factory([]));
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);
}
示例9: createService
/**
* @param ServiceLocatorInterface $serviceLocator
* @return MvcTranslator
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Assume that if a user has registered a service for the
// TranslatorInterface, it must be valid
if ($serviceLocator->has('Zend\\I18n\\Translator\\TranslatorInterface')) {
return new MvcTranslator($serviceLocator->get('Zend\\I18n\\Translator\\TranslatorInterface'));
}
// If ext/intl is not loaded, return a dummy translator
if (!extension_loaded('intl')) {
return new MvcTranslator(new DummyTranslator());
}
// Load a translator from configuration, if possible
if ($serviceLocator->has('Config')) {
$config = $serviceLocator->get('Config');
// 'translator' => false
if (array_key_exists('translator', $config) && $config['translator'] === false) {
return new MvcTranslator(new DummyTranslator());
}
// 'translator' => array( ... translator options ... )
if (array_key_exists('translator', $config) && (is_array($config['translator']) && !empty($config['translator']) || $config['translator'] instanceof Traversable)) {
$i18nTranslator = Translator::factory($config['translator']);
$serviceLocator->setService('Zend\\I18n\\Translator\\TranslatorInterface', $i18nTranslator);
return new MvcTranslator($i18nTranslator);
}
}
// For BC purposes (pre-2.3.0), use the I18n Translator
return new MvcTranslator(new Translator());
}
示例10: setUp
public function setUp()
{
$this->factory = new RendererFactory();
$this->container = $this->prophesize(ContainerInterface::class);
$this->container->get('translator')->willReturn(Translator::factory([]));
$this->container->get('config')->willReturn(['navigation' => [], 'recaptcha' => []]);
$this->container->get(RouteAssembler::class)->willReturn($this->prophesize(RouteAssembler::class)->reveal());
}
示例11: setUp
public function setUp()
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$command = new GenerateShortcodeCommand($this->urlShortener->reveal(), Translator::factory([]), ['schema' => 'http', 'hostname' => 'foo.com']);
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);
}
示例12: __invoke
/**
* Create a Translator instance.
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return Translator
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
// Configure the translator
$config = $container->get('config');
$trConfig = isset($config['translator']) ? $config['translator'] : [];
$translator = Translator::factory($trConfig);
return $translator;
}
示例13: translatePlural
public function translatePlural($singular, $plural, $number, $textDomain, $locale)
{
if ($this->translator === null) {
$this->translator = $this->getApplicationServiceLocator()->get('translator');
}
return $this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
}
示例14: testLanguage
public function testLanguage()
{
$this->assertEquals('en', $this->translator->getLocale());
$this->middleware->__invoke($this->request, new Response(), function ($req, $resp) {
return $resp;
});
$this->assertEquals('es', $this->translator->getLocale());
}
示例15: addErrorMessage
public function addErrorMessage($message)
{
if ($this->translator) {
$namespace = explode('\\', get_class($this))[0];
$message = $this->translator->translate($message, $namespace);
}
$this->errorMessages[] = $message;
}