interface LocaleChooserInterface (View source)

Interface to get the list of preferred locales.

This is to facilitate falling back to another locale in case there is no translation present for the desired locale.

We distinguish the current locale (getLocale) and the system default locale. The idea is that there is a default that never changes, which is also used for getDefaultLocalesOrder. In some situations, you want a fixed order of available languages, regardless of the current users preferences.

Methods

setLocalePreference(array $localePreference)

Set the preferences.

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.

string
getLocale()

Get the locale of the current session.

setLocale(string $locale)

Set the locale of the current session.

array
getDefaultLocalesOrder()

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

string
getDefaultLocale()

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

Details

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

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

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

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 getDefaultLocale()

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

Return Value

string defaultLocale