l18n


Namel18n JSON
Version 2021.3 PyPI version JSON
download
home_pagehttps://github.com/tkhyn/l18n
SummaryInternationalization for pytz timezones and territories
upload_time2021-11-12 09:32:36
maintainer
docs_urlNone
authorThomas Khyn
requires_python
license
keywords pytz translation i18n
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            l18n
====

|copyright| 2014-2021 Thomas Khyn

Locale 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, ``l18n`` is a contraction of ``i18n`` and ``l10n``,
namely 'internationalisation' and 'localization'. It basically provides
lazy translations for names used for localization purposes (e.g. places and
timezones).

I started writing ``l18n`` when I was looking for translations for the pytz_
library. 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 the CLDR_ (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 use ``l18n``, you first need to install it. It works well with ``pip``::

   pip install l18n

Then, in your code::

   >>> import l18n

``l18n`` exposes several read-only dictionary-like objects:

l18n.tz_cities

   is a mapping between all the timezones listed in ``pytz.all_timezones``
   and human-friendly **lazy** versions of the translated name of the city
   in the current language (see `Selecting the language`_ below). 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_fullnames

   is a mapping between all the timezones listed in ``pytz.all_timezones``
   and **lazy** versions 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 French

   It is interesting to note that for 3-components timezone names where the
   local state or territory appears in the city name, ``l18n`` cleverly 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.territories

   is a mapping between the territory codes as defined in the CLDR_ and their
   localized names, lazily defined. For example::

      >>> str(l18n.territories['CZ'])
      'Czech Republic'  # or 'République Tchèque' in French


.. note::

   The 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.


.. note::

   The 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-mentioned ``tz_cities``, ``tz_fullnames`` and ``territories`` are not
simple dictionaries and provide additional features.

Sorting
.......

When iterating over an ``L18NMap``, the items, keys or values are *yielded* in
alphabetical order **in 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.

Subsets
.......

It is possible to generate a new ``L18NMap`` from an existing one by using the
``subset`` method and passing an iterable of ``keys`` that 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 for
``pytz.common_timezones``::

   >>> common_cities = l18n.tz_cities.subset(pytz.common_timezones.keys())


Selecting the language
----------------------

By default, when importing ``l18n``, the current default locale is used (via
``locale.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)


Utilities
---------

``l18n`` also 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 language

``l18n.utils.get_country_code_from_tz(timezone)``

   returns the country code from a given (untranslated) timezone


Versionning
-----------

``l18n``'s main version number matches ``pytz``'s version number. ``l18n``
2014.10.X will be fully compatible with ``pytz`` 2014.10 whatever the value of
X. Indeed, the primary aim is to keep ``l18n``'s translation files consistent
with ``pytz``'s timezone names.

Before ``l18n`` 2016.6, the ``pytz`` version was pinned against the ``l18n``
version. Now, ``l18n`` YEAR.MONTH can now be used with any subsequent ``pytz``
version. However, note that there may be missing translations if the 2 versions
are too different from each other. In that case, open an issue_ to request a
new version of ``l18n`` to be published.


.. _`want to add yours?`:

Want to add a language?
-----------------------

Great idea !! Have a look at CONTRIBUTE.rst_.


Roadmap
-------

- Add supported languages
- Add currencies and other stuff


.. |copyright| unicode:: 0xA9

.. _pytz: https://pypi.python.org/pypi/pytz/
.. _CLDR: http://cldr.unicode.org/
.. _CONTRIBUTE.rst: https://github.com/tkhyn/l18n/src/tip/CONTRIBUTE.rst
.. _issue: https://github.com/tkhyn/l18n/issues/new



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tkhyn/l18n",
    "name": "l18n",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "pytz,translation,i18n",
    "author": "Thomas Khyn",
    "author_email": "thomas@ksytek.com",
    "download_url": "https://files.pythonhosted.org/packages/ff/9c/13d732e16e8fbdef7e68f4f339f3b86618430d4003c7ffe82e15bf925d7c/l18n-2021.3.tar.gz",
    "platform": "",
    "description": "l18n\n====\n\n|copyright| 2014-2021 Thomas Khyn\n\nLocale internationalization package. Translations for places, timezones ...\n\nTested with the latest minor versions of Python 2 and 3.\n\nSupported languages: English, French, German, Czech, Chinese\n(`want to add yours?`_)\n\n\nWhat is l18n?\n-------------\n\nAs you may have noticed, ``l18n`` is a contraction of ``i18n`` and ``l10n``,\nnamely 'internationalisation' and 'localization'. It basically provides\nlazy translations for names used for localization purposes (e.g. places and\ntimezones).\n\nI started writing ``l18n`` when I was looking for translations for the pytz_\nlibrary. Indeed, on a multi-lingual site where users can select the timezone\nthey are in, it's much better if they can select in their language, as in some\ncases, the differences with the english name can be significant, hence the\nplace to look for it when it's sorted in alphabetical order.\n\nAnd as I am lazy, I thought of a way to - almost - automatically fetch the\ntranslations from the CLDR_ (Unicode's Common Locale Data Repository) database.\n\nIntegrating function to link timezone to country codes, there was no reason not\nto try and provide translations also for the latter. In the near future, I -\nor contributors - may also add currencies or measurement units fetched from\nthe CLDR database ...\n\n\nHow does it work?\n-----------------\n\nTo use ``l18n``, you first need to install it. It works well with ``pip``::\n\n   pip install l18n\n\nThen, in your code::\n\n   >>> import l18n\n\n``l18n`` exposes several read-only dictionary-like objects:\n\nl18n.tz_cities\n\n   is a mapping between all the timezones listed in ``pytz.all_timezones``\n   and human-friendly **lazy** versions of the translated name of the city\n   in the current language (see `Selecting the language`_ below). For example,\n   if the language is English::\n\n      >>> l18n.tz_cities['Pacific/Easter']\n      L18NLazyString <Easter Island>\n      >>> str(l18n.tz_cities['Pacific/Easter'])\n      'Easter Island'\n\n   In French, it would give::\n\n      >>> str(l18n.tz_cities['Pacific/Easter'])\n      '\u00cele de P\u00e2ques'\n\nl18n.tz_fullnames\n\n   is a mapping between all the timezones listed in ``pytz.all_timezones``\n   and **lazy** versions of the timezones' full names in the current language.\n   For example::\n\n      >>> str(l18n.tz_fullnames['Pacific/Easter'])\n      'Pacific/Easter Island'  # or 'Pacifique/\u00cele de P\u00e2ques' in French\n\n   It is interesting to note that for 3-components timezone names where the\n   local state or territory appears in the city name, ``l18n`` cleverly strips\n   this information so that it is not repeated::\n\n      >>> str(l18n.tz_fullnames['America/North_Dakota/New_Salem'])\n      'America/North Dakota/New Salem'\n\n   indeed::\n\n      >>> str(l18n.tz_cities['America/North_Dakota/New_salem'])\n      'New Salem, North Dakota'\n\nl18n.territories\n\n   is a mapping between the territory codes as defined in the CLDR_ and their\n   localized names, lazily defined. For example::\n\n      >>> str(l18n.territories['CZ'])\n      'Czech Republic'  # or 'R\u00e9publique Tch\u00e8que' in French\n\n\n.. note::\n\n   The values are translated each time they are evaluated, there is no caching.\n   This means that the same L18NLazyString / L18NLazyStringsList instance can\n   be used and produce 2 different outputs if you change the language between\n   the evaluations.\n\n\n.. note::\n\n   The values in the above mentionned dictionaries can be overriden by your\n   own translations. The dictionaries are not read-only and values can be\n   added or removed at your convenience.\n\n\nLazy mappings special features (v.2016.6.3 onwards)\n---------------------------------------------------\n\nThe fore-mentioned ``tz_cities``, ``tz_fullnames`` and ``territories`` are not\nsimple dictionaries and provide additional features.\n\nSorting\n.......\n\nWhen iterating over an ``L18NMap``, the items, keys or values are *yielded* in\nalphabetical order **in the currently selected language**. For performance, the\nresults are cached by language, so the sort is only performed once per language.\nNote that the values are still lazy objects that are evaluated only when\nrendered into a string.\n\nSubsets\n.......\n\nIt is possible to generate a new ``L18NMap`` from an existing one by using the\n``subset`` method and passing an iterable of ``keys`` that need to be kept in\nthe new mapping. Any cached sort is also used to generate the new cache, so\nthat there is nothing to re-calculate in the new subset.\n\nFor example, one can generate a map of translations for\n``pytz.common_timezones``::\n\n   >>> common_cities = l18n.tz_cities.subset(pytz.common_timezones.keys())\n\n\nSelecting the language\n----------------------\n\nBy default, when importing ``l18n``, the current default locale is used (via\n``locale.getdefaultlocale()``). If it is not the one you want or if you need to\nchange it, it is rather easy::\n\n   >>> l18n.set_language('en')\n   >>> str(l18n.tz_cities['Pacific/Easter'])\n   'Easter Island'\n   >>> l18n.set_language('fr')\n   >>> str(l18n.tz_cities['Pacific/Easter'])\n   '\u00cele de P\u00e2ques'\n\nAnd in case you want to disable translation and use raw default strings::\n\n   >>> l18n.set_language(None)\n\n\nUtilities\n---------\n\n``l18n`` also exposes a few functions that may be helpful in some cases:\n\n``l18n.utils.get_country_tzs(country_code)``\n\n   returns a list of locations for the given country code, sorted in\n   alphabetical order in the currently selected language\n\n``l18n.utils.get_country_code_from_tz(timezone)``\n\n   returns the country code from a given (untranslated) timezone\n\n\nVersionning\n-----------\n\n``l18n``'s main version number matches ``pytz``'s version number. ``l18n``\n2014.10.X will be fully compatible with ``pytz`` 2014.10 whatever the value of\nX. Indeed, the primary aim is to keep ``l18n``'s translation files consistent\nwith ``pytz``'s timezone names.\n\nBefore ``l18n`` 2016.6, the ``pytz`` version was pinned against the ``l18n``\nversion. Now, ``l18n`` YEAR.MONTH can now be used with any subsequent ``pytz``\nversion. However, note that there may be missing translations if the 2 versions\nare too different from each other. In that case, open an issue_ to request a\nnew version of ``l18n`` to be published.\n\n\n.. _`want to add yours?`:\n\nWant to add a language?\n-----------------------\n\nGreat idea !! Have a look at CONTRIBUTE.rst_.\n\n\nRoadmap\n-------\n\n- Add supported languages\n- Add currencies and other stuff\n\n\n.. |copyright| unicode:: 0xA9\n\n.. _pytz: https://pypi.python.org/pypi/pytz/\n.. _CLDR: http://cldr.unicode.org/\n.. _CONTRIBUTE.rst: https://github.com/tkhyn/l18n/src/tip/CONTRIBUTE.rst\n.. _issue: https://github.com/tkhyn/l18n/issues/new\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Internationalization for pytz timezones and territories",
    "version": "2021.3",
    "project_urls": {
        "Homepage": "https://github.com/tkhyn/l18n"
    },
    "split_keywords": [
        "pytz",
        "translation",
        "i18n"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6ce7dfa82d0bb2b314950e457a755463b429090127ffc0ab9d8d14ef4563ad44",
                "md5": "46eea68cc807474d27083ec0c47c7a86",
                "sha256": "78495d1df95b6f7dcc694d1ba8994df709c463a1cbac1bf016e1b9a5ce7280b9"
            },
            "downloads": -1,
            "filename": "l18n-2021.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "46eea68cc807474d27083ec0c47c7a86",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 51534,
            "upload_time": "2021-11-12T09:32:34",
            "upload_time_iso_8601": "2021-11-12T09:32:34.296382Z",
            "url": "https://files.pythonhosted.org/packages/6c/e7/dfa82d0bb2b314950e457a755463b429090127ffc0ab9d8d14ef4563ad44/l18n-2021.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ff9c13d732e16e8fbdef7e68f4f339f3b86618430d4003c7ffe82e15bf925d7c",
                "md5": "71c22f57c532e58175fac6a540d6d922",
                "sha256": "1956e890d673d17135cc20913253c154f6bc1c00266c22b7d503cc1a5a42d848"
            },
            "downloads": -1,
            "filename": "l18n-2021.3.tar.gz",
            "has_sig": false,
            "md5_digest": "71c22f57c532e58175fac6a540d6d922",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 50712,
            "upload_time": "2021-11-12T09:32:36",
            "upload_time_iso_8601": "2021-11-12T09:32:36.255672Z",
            "url": "https://files.pythonhosted.org/packages/ff/9c/13d732e16e8fbdef7e68f4f339f3b86618430d4003c7ffe82e15bf925d7c/l18n-2021.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-11-12 09:32:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tkhyn",
    "github_project": "l18n",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "l18n"
}
        
Elapsed time: 0.16736s