class Translator (View source)

A class for translating messages

Messages (labels) can be translated in two modes:

  • by original label: untranslated label is used as a key
  • by ID: string identifier is used as a key (eg. user.noaccess)

Correct plural form of translated message is returned when $quantity parameter is provided to a method. Otherwise, or on failure just translated version is returned (eg. when string is translated only to one form).

When all fails, untranslated (original) string or ID is returned (depends on translation method).

Placeholders' resolving is done when needed (see FormatResolver class).

Actual translating is done by injected TranslationProvider instance, so storage format depends on concrete implementation.

Properties

protected Service $localizationService
protected TranslationProviderInterface $translationProvider
protected FormatResolver $formatResolver
protected PluralsReader $pluralsReader

Methods

void
injectLocalizationService(Service $localizationService)

No description

void
injectTranslationProvider(TranslationProviderInterface $translationProvider)

No description

void
injectFormatResolver(FormatResolver $formatResolver)

No description

void
injectPluralsReader(PluralsReader $pluralsReader)

No description

string
translateByOriginalLabel(string $originalLabel, array $arguments = [], mixed $quantity = null, Locale $locale = null, string $sourceName = 'Main', string $packageKey = 'Neos.Flow')

Translates the message given as $originalLabel.

string|null
translateById(string $labelId, array $arguments = [], mixed $quantity = null, Locale $locale = null, string $sourceName = 'Main', string $packageKey = 'Neos.Flow')

Returns translated string found under the $labelId.

string|null
getPluralForm(mixed $quantity, Locale $locale)

Get the plural form to be used.

Details

void injectLocalizationService(Service $localizationService)

No description

Parameters

Service $localizationService

Return Value

void

void injectTranslationProvider(TranslationProviderInterface $translationProvider)

No description

Parameters

TranslationProviderInterface $translationProvider

Return Value

void

void injectFormatResolver(FormatResolver $formatResolver)

No description

Parameters

FormatResolver $formatResolver

Return Value

void

void injectPluralsReader(PluralsReader $pluralsReader)

No description

Parameters

PluralsReader $pluralsReader

Return Value

void

string translateByOriginalLabel(string $originalLabel, array $arguments = [], mixed $quantity = null, Locale $locale = null, string $sourceName = 'Main', string $packageKey = 'Neos.Flow')

Translates the message given as $originalLabel.

Searches for a translation in the source as defined by $sourceName (interpretation depends on concrete translation provider used).

If any arguments are provided in the $arguments array, they will be inserted to the translated string (in place of corresponding placeholders, with format defined by these placeholders).

If $quantity is provided, correct plural form for provided $locale will be chosen and used to choose correct translation variant.

If no $locale is provided, default system locale will be used.

Parameters

string $originalLabel

Untranslated message

array $arguments

An array of values to replace placeholders with

mixed $quantity

A number to find plural form for (float or int), NULL to not use plural forms

Locale $locale

Locale to use (NULL for default one)

string $sourceName

Name of file with translations, base path is $packageKey/Resources/Private/Locale/Translations/

string $packageKey

Key of the package containing the source file

Return Value

string

Translated $originalLabel or $originalLabel itself on failure

Exceptions

IndexOutOfBoundsException
InvalidFormatPlaceholderException

string|null translateById(string $labelId, array $arguments = [], mixed $quantity = null, Locale $locale = null, string $sourceName = 'Main', string $packageKey = 'Neos.Flow')

Returns translated string found under the $labelId.

Searches for a translation in the source as defined by $sourceName (interpretation depends on concrete translation provider used).

If any arguments are provided in the $arguments array, they will be inserted to the translated string (in place of corresponding placeholders, with format defined by these placeholders).

If $quantity is provided, correct plural form for provided $locale will be chosen and used to choose correct translation variant.

Parameters

string $labelId

Key to use for finding translation

array $arguments

An array of values to replace placeholders with

mixed $quantity

A number to find plural form for (float or int), NULL to not use plural forms

Locale $locale

Locale to use (NULL for default one)

string $sourceName

Name of file with translations, base path is $packageKey/Resources/Private/Locale/Translations/

string $packageKey

Key of the package containing the source file

Return Value

string|null

Translated message or NULL on failure

Exceptions

IndexOutOfBoundsException
InvalidFormatPlaceholderException

See also

Translator::translateByOriginalLabel

protected string|null getPluralForm(mixed $quantity, Locale $locale)

Get the plural form to be used.

If $quantity is numeric and non-NULL, the plural form for provided $locale will be chosen according to it.

In all other cases, NULL is returned.

Parameters

mixed $quantity
Locale $locale

Return Value

string|null