UserFrosting API

Translator

Translator Class.

Translate message ids to a message in a specified language.

Tags
author

Louis Charette

author

Alexander Weissman (https://alexanderweissman.com)

Table of Contents

$twig Twig_Environment
$defaultPluralKey string
$dictionary DictionaryInterface
__construct() Create the translator. mixed
getDictionary() Returned the associated dictionary. DictionaryInterface
getLocale() Returns the associated locale for the specified dictionary. LocaleInterface
translate() Translate the given message id into the currently configured language, substituting any placeholders that appear in the translated string. string
getMessageFromKey() Get the message from key. string
getPluralKey() Return the plural key from a translation array. string
getPluralValue() Return the plural value, aka the nummber to display, from the placeholder values. int|null
getPluralMessageKey() Return the correct plural message form to use. int|null
parsePlaceHolders() Parse Placeholder. string
getPluralForm() Determine which plural form we should use. int
getPluralRuleNumber() Return the correct rule number to use. int

Properties

$defaultPluralKey

protected string $defaultPluralKey = 'plural'

Methods

translate()

Translate the given message id into the currently configured language, substituting any placeholders that appear in the translated string.

public translate( $messageKey : string [, $placeholders : array|int = [] ] ) : string

Return the $messageKey if not match is found

Parameters
$messageKey : string

The id of the message id to translate. can use dot notation for array

$placeholders : array|int = []

An optional hash of placeholder names => placeholder values to substitute (default : [])

Return values
string

The translated message.

getMessageFromKey()

Get the message from key.

protected getMessageFromKey( $messageKey : string , &$placeholders : array|int ) : string

Go throught all registered language keys avaiable and find the correct one to use, using the placeholders to select the correct plural form.

Parameters
$messageKey : string

The key to find the message for

$placeholders : array|int

Passed by reference, since plural placeholder will be added for later processing

Return values
string

The message string

getPluralKey()

Return the plural key from a translation array.

protected getPluralKey( $messageArray : array ) : string

If no plural key is defined in the @PLURAL instruction of the message array, we fallback to the default one.

Parameters
$messageArray : array
Return values
string

getPluralValue()

Return the plural value, aka the nummber to display, from the placeholder values.

protected getPluralValue( $placeholders : array|int , $pluralKey : string ) : int|null
Parameters
$placeholders : array|int

Placeholder

$pluralKey : string

The plural key, for key => value match

Return values
int|null

The number, null if not found

getPluralMessageKey()

Return the correct plural message form to use.

protected getPluralMessageKey( $messageArray : array , $pluralValue : int ) : int|null

When multiple plural form are available for a message, this method will return the correct oen to use based on the numeric value.

Parameters
$messageArray : array

The array with all the form inside ($pluralRule => $message)

$pluralValue : int

The numeric value used to select the correct message

Return values
int|null

Returns which key from $messageArray to use

parsePlaceHolders()

Parse Placeholder.

protected parsePlaceHolders( $message : string , $placeholders : array ) : string

Replace placeholders in the message with their values from the passed argument.

Parameters
$message : string

The message to replace placeholders in

$placeholders : array

An optional hash of placeholder (names => placeholder) values to substitute (default : [])

Return values
string

The message with replaced placeholders

getPluralForm()

Determine which plural form we should use.

public getPluralForm( $number : int|float [, $forceRule : mixed = false ] ) : int

For some languages this is not as simple as for English.

Parameters
$number : int|float

The number we want to get the plural case for. Float numbers are floored.

$forceRule : mixed = false

False to use the plural rule of the language package or an integer to force a certain plural rule

Tags
see
https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals
Return values
int

The plural-case we need to use for the number plural-rule combination

getPluralRuleNumber()

Return the correct rule number to use.

protected getPluralRuleNumber( $forceRule : int|bool ) : int
Parameters
$forceRule : int|bool

Force to use a particular rule. Otherwise, use the language defined one

Return values
int

Search results