translate() public static method

Usage: Message::translate('Mind the gap.'); Message::translate('house', array('count' => 23)); String::insert()-style placeholders may be used within the message and replacements provided directly within the options argument. Example: Message::translate('I can see {:count} bike.'); Message::translate('This painting is {:color}.', array( 'color' => Message::translate('silver'), ));
See also: lithium\util\String::insert()
public static translate ( string $id, array $options = [] ) : string
$id string The id to use when looking up the translation.
$options array Valid options are: - `'count'`: Used to determine the correct plural form. You can either pass a signed or unsigned integer, the behavior when passing other types is yet undefined. The count is made absolute before being passed to the pluralization function. This has the effect that that with i.e. an English pluralization function passing `-1` results in a singular translation. - `'locale'`: The target locale, defaults to current locale. - `'scope'`: The scope of the message. - `'context'`: The disambiguating context (optional). - `'default'`: Is used as a fall back if `_translated()` returns without a result. - `'noop'`: If `true` no whatsoever lookup takes place.
return string The translation or the value of the `'default'` option if none could be found.
 public function testShortHandsAsymmetry()
 {
     $filters = Message::shortHands();
     $t = $filters['t'];
     $tn = $filters['tn'];
     $expected = Message::translate('house', array('locale' => 'de'));
     $result = $t('house', array('locale' => 'de'));
     $this->assertNotEqual($expected, $result);
     $expected = Message::translate('house', array('locale' => 'de', 'count' => 3));
     $result = $tn('house', 'houses', array('locale' => 'de'));
     $this->assertNotEqual($expected, $result);
 }
示例#2
0
文件: Message.php 项目: EHER/chegamos
 /**
  * Returns an array containing named closures which are aliases for `translate()`.
  * They can be embedded as content filters in the template layer using a filter for
  * `Media::_handle()` or be used in other places where needed.
  *
  * Usage:
  * {{{
  * 	$t('bike');
  * 	$tn('bike', 'bikes', array('count' => 3));
  * }}}
  *
  * Using in a method:
  * {{{
  * 	public function index() {
  * 		extract(Message::aliases());
  * 		$notice = $t('look');
  * 	}
  * }}}
  *
  * @see lithium\net\http\Media::_handle()
  * @return array Named aliases (`'t'` and `'tn'`) for translation functions.
  */
 public static function aliases()
 {
     $t = function ($message, array $options = array()) {
         return Message::translate($message, $options + array('default' => $message));
     };
     $tn = function ($message1, $message2, $count, array $options = array()) {
         return Message::translate($message1, $options + compact('count') + array('default' => $count == 1 ? $message1 : $message2));
     };
     return compact('t', 'tn');
 }
示例#3
0
 public function testCaching()
 {
     $data = array('catalog' => 'Katalog');
     Catalog::write('runtime', 'message', 'de', $data, array('scope' => 'foo'));
     $this->assertFalse(Message::cache());
     $result = Message::translate('catalog', array('locale' => 'de', 'scope' => 'foo'));
     $this->assertEqual('Katalog', $result);
     $cache = Message::cache();
     $this->assertEqual('Katalog', $cache['foo']['de']['catalog']);
     Message::cache(false);
     $this->assertFalse(Message::cache());
     Message::cache(array('foo' => array('de' => array('catalog' => '<Katalog>'))));
     $result = Message::translate('catalog', array('locale' => 'de', 'scope' => 'foo'));
     $this->assertEqual('<Katalog>', $result);
     $options = array('locale' => 'de', 'scope' => 'foo', 'count' => 2);
     $this->assertEqual('<Katalog>', Message::translate('catalog', $options));
     Message::cache(false);
     Message::cache(array('foo' => array('de' => array('catalog' => array('<Katalog>')))));
     $this->assertNull(Message::translate('catalog', $options));
 }