class LocaleChooser implements LocaleChooserInterface (View source)

Class to get the list of preferred locales.

Properties

protected $localePreference locale fallback list indexed by source locale.
protected string $locale The current locale to use.
protected string $defaultLocale The default locale of the system used for getDefaultLocalesOrder and as fallback if locale is not set.

Methods

__construct(array $localePreference, string $defaultLocale)

No description

setLocalePreference(array $localePreference)

Set the preferences.

setLocalePreferenceAndDefaultLocale(array $localePreference, string $defaultLocale)

Update the localePreferences and the defaultLocale at once.

setFallbackLocales(string $locale, array $order, bool $replace = false)

Set or update the order of fallback locales for the selected locale.

array
getFallbackLocales(object $document, ClassMetadata $metadata, string|null $forLocale = null)

Gets an ordered list of locales to try as fallback for a locale.

array
getDefaultLocalesOrder()

Get the ordered list of locales for the default locale without any context.

string
getLocale()

Get the locale of the current session.

setLocale(string $locale)

Set the locale of the current session.

string
getDefaultLocale()

Get the default locale of this application. This should never change, regardless of the current session or context.

Details

__construct(array $localePreference, string $defaultLocale)

Parameters

array $localePreference array of arrays with a preferred locale order list for each locale
string $defaultLocale the default locale to be used if locale is not set

setLocalePreference(array $localePreference)

Set the preferences.

For example:

array( 'en' => array('fr', 'de'), 'fr' => array('en'), 'de' => array('en), )

Parameters

array $localePreference array of arrays with a preferred locale order list for each locale

Exceptions

MissingTranslationException if no entry for the default locale is found in $localePreference

setLocalePreferenceAndDefaultLocale(array $localePreference, string $defaultLocale)

Update the localePreferences and the defaultLocale at once.

Update both parameters at once to be able to specify a new defaultLocale that was previously not contained in the localePreferences.

Parameters

array $localePreference array of arrays with a preferred locale order list for each locale
string $defaultLocale the default locale to be used if locale is not set

setFallbackLocales(string $locale, array $order, bool $replace = false)

Set or update the order of fallback locales for the selected locale.

Parameters

string $locale The locale to update the fallback order for.
array $order An order of locales to try as fallback.
bool $replace Whether to append existing locales to the end or replace the whole fallback order.

array getFallbackLocales(object $document, ClassMetadata $metadata, string|null $forLocale = null)

Gets an ordered list of locales to try as fallback for a locale.

Example return value with param $forLocale = 'en': array('fr', 'de')

Parameters

object $document The document object
ClassMetadata $metadata The metadata of the document class
string|null $forLocale Locale for which you want the fallback order, e.g. the current request locale. If null, the default locale is to be used.

Return Value

array $preferredLocales

Exceptions

MissingTranslationException

array getDefaultLocalesOrder()

Get the ordered list of locales for the default locale without any context.

This list has to include the default locale as first element.

Return Value

array preferred locale order for the default locale

string getLocale()

Get the locale of the current session.

Return Value

string locale

setLocale(string $locale)

Set the locale of the current session.

Parameters

string $locale

string getDefaultLocale()

Get the default locale of this application. This should never change, regardless of the current session or context.

Return Value

string defaultLocale