Package: l18n
© 2014-2021 Thomas KhynLocale internationalization package. Translations for places, timezones …Tested with the latest minor versions of Python 2 and 3.Supported languages: English, French, German, Czech, Chinese
(want to add yours?)What is l18n?As you may have noticed,l18nis a contraction ofi18nandl10n,
namely ‘internationalisation’ and ‘localization’. It basically provides
lazy translations for names used for localization purposes (e.g. places and
timezones).I started writingl18nwhen I was looking for translations for thepytzlibrary. Indeed, on a multi-lingual site where users can select the timezone
they are in, it’s much better if they can select in their language, as in some
cases, the differences with the english name can be significant, hence the
place to look for it when it’s sorted in alphabetical order.And as I am lazy, I thought of a way to - almost - automatically fetch the
translations from theCLDR(Unicode’s Common Locale Data Repository) database.Integrating function to link timezone to country codes, there was no reason not
to try and provide translations also for the latter. In the near future, I -
or contributors - may also add currencies or measurement units fetched from
the CLDR database …How does it work?To usel18n, you first need to install it. It works well withpip:pip install l18nThen, in your code:>>> import l18nl18nexposes several read-only dictionary-like objects:l18n.tz_citiesis a mapping between all the timezones listed inpytz.all_timezonesand human-friendlylazyversions of the translated name of the city
in the current language (seeSelecting the languagebelow). For example,
if the language is English:>>> l18n.tz_cities['Pacific/Easter']
L18NLazyString <Easter Island>
>>> str(l18n.tz_cities['Pacific/Easter'])
'Easter Island'In French, it would give:>>> str(l18n.tz_cities['Pacific/Easter'])
'Île de Pâques'l18n.tz_fullnamesis a mapping between all the timezones listed inpytz.all_timezonesandlazyversions of the timezones’ full names in the current language.
For example:>>> str(l18n.tz_fullnames['Pacific/Easter'])
'Pacific/Easter Island'  # or 'Pacifique/Île de Pâques' in FrenchIt is interesting to note that for 3-components timezone names where the
local state or territory appears in the city name,l18ncleverly strips
this information so that it is not repeated:>>> str(l18n.tz_fullnames['America/North_Dakota/New_Salem'])
'America/North Dakota/New Salem'indeed:>>> str(l18n.tz_cities['America/North_Dakota/New_salem'])
'New Salem, North Dakota'l18n.territoriesis a mapping between the territory codes as defined in theCLDRand their
localized names, lazily defined. For example:>>> str(l18n.territories['CZ'])
'Czech Republic'  # or 'République Tchèque' in FrenchNoteThe values are translated each time they are evaluated, there is no caching.
This means that the same L18NLazyString / L18NLazyStringsList instance can
be used and produce 2 different outputs if you change the language between
the evaluations.NoteThe values in the above mentionned dictionaries can be overriden by your
own translations. The dictionaries are not read-only and values can be
added or removed at your convenience.Lazy mappings special features (v.2016.6.3 onwards)The fore-mentionedtz_cities,tz_fullnamesandterritoriesare not
simple dictionaries and provide additional features.SortingWhen iterating over anL18NMap, the items, keys or values areyieldedin
alphabetical orderin the currently selected language. For performance, the
results are cached by language, so the sort is only performed once per language.
Note that the values are still lazy objects that are evaluated only when
rendered into a string.SubsetsIt is possible to generate a newL18NMapfrom an existing one by using thesubsetmethod and passing an iterable ofkeysthat need to be kept in
the new mapping. Any cached sort is also used to generate the new cache, so
that there is nothing to re-calculate in the new subset.For example, one can generate a map of translations forpytz.common_timezones:>>> common_cities = l18n.tz_cities.subset(pytz.common_timezones.keys())Selecting the languageBy default, when importingl18n, the current default locale is used (vialocale.getdefaultlocale()). If it is not the one you want or if you need to
change it, it is rather easy:>>> l18n.set_language('en')
>>> str(l18n.tz_cities['Pacific/Easter'])
'Easter Island'
>>> l18n.set_language('fr')
>>> str(l18n.tz_cities['Pacific/Easter'])
'Île de Pâques'And in case you want to disable translation and use raw default strings:>>> l18n.set_language(None)Utilitiesl18nalso exposes a few functions that may be helpful in some cases:l18n.utils.get_country_tzs(country_code)returns a list of locations for the given country code, sorted in
alphabetical order in the currently selected languagel18n.utils.get_country_code_from_tz(timezone)returns the country code from a given (untranslated) timezoneVersionningl18n’s main version number matchespytz’s version number.l18n2014.10.X will be fully compatible withpytz2014.10 whatever the value of
X. Indeed, the primary aim is to keepl18n’s translation files consistent
withpytz’s timezone names.Beforel18n2016.6, thepytzversion was pinned against thel18nversion. Now,l18nYEAR.MONTH can now be used with any subsequentpytzversion. However, note that there may be missing translations if the 2 versions
are too different from each other. In that case, open anissueto request a
new version ofl18nto be published.Want to add a language?Great idea !! Have a look atCONTRIBUTE.rst.RoadmapAdd supported languagesAdd currencies and other stuff