lithium\g11n\Message::translate()

public static method

Translates a message according to the current or provided locale and into its correct plural form.

Usage:

Message::translate('Mind the gap.');
Message::translate('house', ['count' => 23]);

Text::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}.', [
	'color' => Message::translate('silver'),
]);

Parameters

  • string $id

    The id to use when looking up the translation.

  • array $options

    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.

Returns

string

The translation or the value of the 'default' option if none could be found.

Source

	public static function translate($id, array $options = []) {
		$defaults = [
			'count' => 1,
			'locale' => Environment::get('locale'),
			'scope' => null,
			'context' => null,
			'default' => null,
			'noop' => false
		];
		$options += $defaults;

		if ($options['noop']) {
			$result = null;
		} else {
			$result = static::_translated($id, abs($options['count']), $options['locale'], [
				'scope' => $options['scope'],
				'context' => $options['context']
			]);
		}

		if ($result = $result ?: $options['default']) {
			return strpos($result, '{:') !== false ? Text::insert($result, $options) : $result;
		}
	}