本文整理汇总了PHP中Cake\I18n\I18n::translator方法的典型用法代码示例。如果您正苦于以下问题:PHP I18n::translator方法的具体用法?PHP I18n::translator怎么用?PHP I18n::translator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\I18n\I18n
的用法示例。
在下文中一共展示了I18n::translator方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testParseContextOnSomeMessages
/**
* Test parsing a file with message context on some msgid values.
*
* This behavior is not ideal, but more thorough solutions
* would break compatibility. Perhaps this is something we can
* reconsider in 4.x
*
* @return void
*/
public function testParseContextOnSomeMessages()
{
$parser = new PoFileParser();
$file = APP . 'Locale' . DS . 'en' . DS . 'context.po';
$messages = $parser->parse($file);
I18n::translator('default', 'en_US', function () use($messages) {
$package = new Package('default');
$package->setMessages($messages);
return $package;
});
$this->assertTextEquals('En cours', $messages['Pending']);
$this->assertTextEquals('En resolved', $messages['Resolved']);
}
示例2:
/**
* Returns correct plural form of message identified by $singular and $plural for count $count.
* Allows you to override the current domain for a single message lookup.
* The context is a unique identifier for the translations string that makes it unique
* within the same domain.
*
* @param string $domain Domain.
* @param string $context Context of the text.
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dxn
*/
function __dxn($domain, $context, $singular, $plural, $count, $args = null)
{
if (!$singular) {
return null;
}
$arguments = func_num_args() === 6 ? (array) $args : array_slice(func_get_args(), 5);
return I18n::translator($domain)->translate($plural, ['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments);
}
示例3: testFallbackTranslatorWithFactory
/**
* Tests that it is possible to register a generic translators factory for a domain
* instead of having to create them manually
*
* @return void
*/
public function testFallbackTranslatorWithFactory()
{
I18n::translator('default', 'fr_FR', function () {
$package = new Package('default');
$package->setMessages(['Dog' => 'Le bark']);
return $package;
});
I18n::config('custom', function ($name, $locale) {
$this->assertEquals('custom', $name);
$package = new Package('default');
$package->setMessages(['Cow' => 'Le moo']);
return $package;
});
$translator = I18n::translator('custom', 'fr_FR');
$this->assertEquals('Le moo', $translator->translate('Cow'));
$this->assertEquals('Le bark', $translator->translate('Dog'));
}