negotiate_locale(preferred,
available,
sep=' _ ' ,
aliases={ ' ar ' : ' ar_SY ' , ' bg ' : ' bg_BG ' , ' bs ' : ' bs_BA ' , ' ca ' : ' ca_ES ' , ' ... )
|
|
Find the best match between available and requested locale strings.
>>> negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
'de_DE'
>>> negotiate_locale(['de_DE', 'en_US'], ['en', 'de'])
'de'
Case is ignored by the algorithm, the result uses the case of the preferred
locale identifier:
>>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at'])
'de_DE'
>>> negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at'])
'de_DE'
By default, some web browsers unfortunately do not include the territory
in the locale identifier for many locales, and some don't even allow the
user to easily add the territory. So while you may prefer using qualified
locale identifiers in your web-application, they would not normally match
the language-only locale sent by such browsers. To workaround that, this
function uses a default mapping of commonly used langauge-only locale
identifiers to identifiers including the territory:
>>> negotiate_locale(['ja', 'en_US'], ['ja_JP', 'en_US'])
'ja_JP'
Some browsers even use an incorrect or outdated language code, such as "no"
for Norwegian, where the correct locale identifier would actually be "nb_NO"
(Bokmål) or "nn_NO" (Nynorsk). The aliases are intended to take care of
such cases, too:
>>> negotiate_locale(['no', 'sv'], ['nb_NO', 'sv_SE'])
'nb_NO'
You can override this default mapping by passing a different aliases
dictionary to this function, or you can bypass the behavior althogher by
setting the aliases parameter to None .
- Parameters:
preferred - the list of locale strings preferred by the user
available - the list of locale strings available
sep - character that separates the different parts of the locale
strings
aliases - a dictionary of aliases for locale identifiers
- Returns:
str
- the locale identifier for the best match, or
None if no match
was found
|