morphi
######
Translatable text for applications and libraries.
What is morphi?
===============
``morphi`` was born out of the need to create a distributable library with internally-localized
text. Although there are several existing packages which deal with translatable text, they all
seem to focus on standalone applications; there seems to be very little available for working
with messages that are distributed along with a packaged library.
Foundations
-----------
``morphi`` is built on ideas gleaned from the following:
* the built-in gettext module
* Babel
Translation
===========
The ``morphi`` module provides utilities for loading ``gettext``-compatible
translators from either the local filesystem or directly from a package. The default
finder will first attempt to locate the messages files in the local filesystem (allowing
messages to be overridden on a particular system), but, if a package name is given,
will then automatically search the package for the messages files. This allows a library
to store default translation messages within the library package itself, and still have
those messages be successfully loaded at runtime.
The ``morphi`` module is primarily built around the
`Babel <http://babel.pocoo.org/en/latest/>`_ package, with
`speaklater <https://github.com/mitsuhiko/speaklater>`_ used for lazy lookups.
Message management
------------------
As the ``morphi`` module is built on ``Babel``, the standard ``distutils`` commands
provided by ``Babel`` are available, and exposed to downstream use. As such, the
standard ``extract_messages``, ``init_catalog``, ``update_catalog``, and ``compile_catalog``
commands are all present and work as described in the `Babel documentation <http://babel.pocoo.org/en/latest/setup.html>`_.
In addition to the standard ``Babel`` ``distutils`` commands, an additional ``compile_json``
command has been added. The ``compile_json`` command will compile the messages into
a JSON file compatible with the
`gettext.js <https://github.com/guillaumepotier/gettext.js>`_ javascript library.
Using translations within a library
-----------------------------------
The easiest way to use the translations is to utilize the ``Manager`` class, which
encapsulates the lookups and ``gettext`` methods, and which provides a way of loading
a new messages file after instantiation (allowing the language to be changed after
initialization).
As an example, let's say you're creating a translation-enabled library named 'mylib'.
The following might be used to initialize and load the translations for use. Details
about the "locales registry" can be found below.
.. code-block:: python
:name: extensions.py
# import the translation library
from morphi.messages import Manager
from morphi.registry import default_registry
# instantiate the translations manager
translation_manager = Manager(package_name='mylib')
# register the manager with the default locales registry
default_registry.subscribe(translation_manager)
# initialize shorter names for the gettext functions
gettext = translation_manager.gettext
lazy_gettext = translation_manager.lazy_gettext
lazy_ngettext = translation_manager.lazy_ngettext
ngettext = translation_manager.ngettext
Note that, in general, this code should be executed only a single time for a given
package. It is recommended that this code be added to an ``extensions.py`` or similar
file, from which the gettext functions can be loaded as singletons.
.. code-block:: python
from mylib.extensions import gettext as _
print(_('My translatable text'))
Format variables
----------------
The gettext functions all permit additional named parameters, to be used in
formatting the translated string. The library currently supports new-style ``.format``
type formatting.
.. code-block:: python
print(_('Hello, {name}!', name='World'))
Locales Registry
----------------
Particularly when being used with package-specific translations, the
``Manager`` will need to be able to be notified when the application's language
settings (particularly the locales) are changed, so that the correct messages
can be loaded and displayed. In order to simplify this notification,
``morphi.registry.Registry`` (with a default singleton registry
named ``default_registry``) can be used. Managers can then be subscribed or
unsubscribed to the registry, which will then notify all managers when
the locale information has changed.
.. code-block:: python
from morphi.registry import default_registry as locales_registry
locales_registry.locales = 'es'
Typically, a manager should be registered with the registry immediately after
it has been instantiated.
Jinja Environment
-----------------
If using Jinja templates, the Jinja environment should be initialized to add the
translation functions.
.. code-block:: python
from morphi.helpers.jinja import configure_jinja_environment
configure_jinja_environment(app.jinja_env, manager)
.. code-block:: jinja
{{ _('Hello, world!') }}
JavaScript translations
-----------------------
As mentioned above, a ``compile_json`` ``distutils`` command is added by the library,
which will compile the messages to a ``messages.js``-compatible JSON file. The library
can be initialized and used as follows
.. code-block:: html
:name: index.html
<script src="{{url_for('mylib.static', filename='gettext.min.js')}}"></script>
<script>
var i18n = window.i18n({});
window._ = function(msgid, domain) {
return i18n.dcnpgettext.apply(
i18n,
[domain, undefined, msgid, undefined, undefined].concat(
Array.prototype.slice.call(arguments, 1)
)
);
};
{% set json_filename = find_mo_filename(package_name='mylib',
extension='json',
localedir='static/i18n') %}
{% if json_filename %}
{# strip off the leading 'static/' portion of the filename #}
{% set json_filename = json_filename[7:] %}
$.getJSON(
'{{ url_for("mylib.static", filename=json_filename) }}'
).then(function (result) {
i18n.loadJSON(result, 'mylib');
});
{% endif %}
</script>
. . .
<p>_('Hello, world!', 'mylib')</p>
Note the presence of the ``find_mo_filename`` function; this function is made available
by calling the ``configure_jinja_environment`` manager method as described above.
Installation
============
``morphi`` can be installed via ``pip``:
.. code:: bash
pip install morphi
To install for development, simply add the ``develop`` tag:
.. code:: bash
pip install morphi[develop]
Development
===========
Testing
-------
Testing currently uses `pytest <https://docs.pytest.org/en/latest/>`_:
.. code:: bash
pytest morphi
Changelog
=========
0.3.2 released 2025-07-10
-------------------------
- fix regression for consumers using setup.py (b7eeb2a_)
.. _b7eeb2a: https://github.com/level12/morphi/commit/b7eeb2a
0.3.1 released 2025-04-11
-------------------------
- support pyproject.toml apps (b32aa95_)
.. _b32aa95: https://github.com/level12/morphi/commit/b32aa95
0.3.0 released 2024-12-18
-------------------------
- fix deprecated pkg_resources usage (de64dba_)
.. _de64dba: https://github.com/level12/morphi/commit/de64dba
0.2.2 released 2023-03-02
-------------------------
- include pytz, since babel no longer does by default (4beab17_)
.. _4beab17: https://github.com/level12/morphi/commit/4beab17
0.2.1 released 2022-10-26
-------------------------
- update package setup and CI, resolve pkg_resources warning (cd0f750_)
.. _cd0f750: https://github.com/level12/morphi/commit/cd0f750
0.2.0 released 2020-05-12
-------------------------
- use pyp for releasing (c4cf37f_)
- support Babel 2.7+ and provide a CI helper method (bc0ef3f_)
.. _c4cf37f: https://github.com/level12/morphi/commit/c4cf37f
.. _bc0ef3f: https://github.com/level12/morphi/commit/bc0ef3f
0.1.2 released 2019-02-11
-------------------------
- Fix errors when using invalid user-supplied resource paths with the resource loader
0.1.1 released 2018-09-20
-------------------------
- Fix pkg_resources support under pyinstaller
0.1.0 released 2018-08-22
-------------------------
- Add initial translations implementation
Raw data
{
"_id": null,
"home_page": "https://github.com/level12/morphi",
"name": "morphi",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Level 12 Developers",
"author_email": "devteam@level12.io",
"download_url": "https://files.pythonhosted.org/packages/76/ea/03783147fa3321b4e7d1aa2950c578f0cb4188b985af21e0a16fb37db37f/morphi-0.3.2.tar.gz",
"platform": null,
"description": "morphi\n######\n\nTranslatable text for applications and libraries.\n\nWhat is morphi?\n===============\n\n``morphi`` was born out of the need to create a distributable library with internally-localized\ntext. Although there are several existing packages which deal with translatable text, they all\nseem to focus on standalone applications; there seems to be very little available for working\nwith messages that are distributed along with a packaged library.\n\n\nFoundations\n-----------\n\n``morphi`` is built on ideas gleaned from the following:\n\n * the built-in gettext module\n * Babel\n\n\nTranslation\n===========\n\nThe ``morphi`` module provides utilities for loading ``gettext``-compatible\ntranslators from either the local filesystem or directly from a package. The default\nfinder will first attempt to locate the messages files in the local filesystem (allowing\nmessages to be overridden on a particular system), but, if a package name is given,\nwill then automatically search the package for the messages files. This allows a library\nto store default translation messages within the library package itself, and still have\nthose messages be successfully loaded at runtime.\n\nThe ``morphi`` module is primarily built around the\n`Babel <http://babel.pocoo.org/en/latest/>`_ package, with\n`speaklater <https://github.com/mitsuhiko/speaklater>`_ used for lazy lookups.\n\n\nMessage management\n------------------\n\nAs the ``morphi`` module is built on ``Babel``, the standard ``distutils`` commands\nprovided by ``Babel`` are available, and exposed to downstream use. As such, the\nstandard ``extract_messages``, ``init_catalog``, ``update_catalog``, and ``compile_catalog``\ncommands are all present and work as described in the `Babel documentation <http://babel.pocoo.org/en/latest/setup.html>`_.\n\nIn addition to the standard ``Babel`` ``distutils`` commands, an additional ``compile_json``\ncommand has been added. The ``compile_json`` command will compile the messages into\na JSON file compatible with the\n`gettext.js <https://github.com/guillaumepotier/gettext.js>`_ javascript library.\n\n\nUsing translations within a library\n-----------------------------------\n\nThe easiest way to use the translations is to utilize the ``Manager`` class, which\nencapsulates the lookups and ``gettext`` methods, and which provides a way of loading\na new messages file after instantiation (allowing the language to be changed after\ninitialization).\n\nAs an example, let's say you're creating a translation-enabled library named 'mylib'.\nThe following might be used to initialize and load the translations for use. Details\nabout the \"locales registry\" can be found below.\n\n.. code-block:: python\n :name: extensions.py\n\n # import the translation library\n from morphi.messages import Manager\n from morphi.registry import default_registry\n\n # instantiate the translations manager\n translation_manager = Manager(package_name='mylib')\n\n # register the manager with the default locales registry\n default_registry.subscribe(translation_manager)\n\n # initialize shorter names for the gettext functions\n gettext = translation_manager.gettext\n lazy_gettext = translation_manager.lazy_gettext\n lazy_ngettext = translation_manager.lazy_ngettext\n ngettext = translation_manager.ngettext\n\n\nNote that, in general, this code should be executed only a single time for a given\npackage. It is recommended that this code be added to an ``extensions.py`` or similar\nfile, from which the gettext functions can be loaded as singletons.\n\n.. code-block:: python\n\n from mylib.extensions import gettext as _\n\n print(_('My translatable text'))\n\n\nFormat variables\n----------------\n\nThe gettext functions all permit additional named parameters, to be used in\nformatting the translated string. The library currently supports new-style ``.format``\ntype formatting.\n\n.. code-block:: python\n\n print(_('Hello, {name}!', name='World'))\n\n\nLocales Registry\n----------------\n\nParticularly when being used with package-specific translations, the\n``Manager`` will need to be able to be notified when the application's language\nsettings (particularly the locales) are changed, so that the correct messages\ncan be loaded and displayed. In order to simplify this notification,\n``morphi.registry.Registry`` (with a default singleton registry\nnamed ``default_registry``) can be used. Managers can then be subscribed or\nunsubscribed to the registry, which will then notify all managers when\nthe locale information has changed.\n\n.. code-block:: python\n\n from morphi.registry import default_registry as locales_registry\n\n locales_registry.locales = 'es'\n\n\nTypically, a manager should be registered with the registry immediately after\nit has been instantiated.\n\n\nJinja Environment\n-----------------\n\nIf using Jinja templates, the Jinja environment should be initialized to add the\ntranslation functions.\n\n.. code-block:: python\n\n from morphi.helpers.jinja import configure_jinja_environment\n\n configure_jinja_environment(app.jinja_env, manager)\n\n.. code-block:: jinja\n\n {{ _('Hello, world!') }}\n\n\nJavaScript translations\n-----------------------\n\nAs mentioned above, a ``compile_json`` ``distutils`` command is added by the library,\nwhich will compile the messages to a ``messages.js``-compatible JSON file. The library\ncan be initialized and used as follows\n\n.. code-block:: html\n :name: index.html\n\n <script src=\"{{url_for('mylib.static', filename='gettext.min.js')}}\"></script>\n <script>\n var i18n = window.i18n({});\n window._ = function(msgid, domain) {\n return i18n.dcnpgettext.apply(\n i18n,\n [domain, undefined, msgid, undefined, undefined].concat(\n Array.prototype.slice.call(arguments, 1)\n )\n );\n };\n {% set json_filename = find_mo_filename(package_name='mylib',\n extension='json',\n localedir='static/i18n') %}\n {% if json_filename %}\n {# strip off the leading 'static/' portion of the filename #}\n {% set json_filename = json_filename[7:] %}\n $.getJSON(\n '{{ url_for(\"mylib.static\", filename=json_filename) }}'\n ).then(function (result) {\n i18n.loadJSON(result, 'mylib');\n });\n {% endif %}\n </script>\n\n . . .\n\n <p>_('Hello, world!', 'mylib')</p>\n\n\nNote the presence of the ``find_mo_filename`` function; this function is made available\nby calling the ``configure_jinja_environment`` manager method as described above.\n\n\nInstallation\n============\n\n``morphi`` can be installed via ``pip``:\n\n.. code:: bash\n\n pip install morphi\n\nTo install for development, simply add the ``develop`` tag:\n\n.. code:: bash\n\n pip install morphi[develop]\n\n\nDevelopment\n===========\n\nTesting\n-------\n\nTesting currently uses `pytest <https://docs.pytest.org/en/latest/>`_:\n\n.. code:: bash\n\n pytest morphi\n\n\n\nChangelog\n=========\n\n0.3.2 released 2025-07-10\n-------------------------\n\n- fix regression for consumers using setup.py (b7eeb2a_)\n\n.. _b7eeb2a: https://github.com/level12/morphi/commit/b7eeb2a\n\n\n0.3.1 released 2025-04-11\n-------------------------\n\n- support pyproject.toml apps (b32aa95_)\n\n.. _b32aa95: https://github.com/level12/morphi/commit/b32aa95\n\n\n0.3.0 released 2024-12-18\n-------------------------\n\n- fix deprecated pkg_resources usage (de64dba_)\n\n.. _de64dba: https://github.com/level12/morphi/commit/de64dba\n\n\n0.2.2 released 2023-03-02\n-------------------------\n\n- include pytz, since babel no longer does by default (4beab17_)\n\n.. _4beab17: https://github.com/level12/morphi/commit/4beab17\n\n\n0.2.1 released 2022-10-26\n-------------------------\n\n- update package setup and CI, resolve pkg_resources warning (cd0f750_)\n\n.. _cd0f750: https://github.com/level12/morphi/commit/cd0f750\n\n\n0.2.0 released 2020-05-12\n-------------------------\n\n- use pyp for releasing (c4cf37f_)\n- support Babel 2.7+ and provide a CI helper method (bc0ef3f_)\n\n.. _c4cf37f: https://github.com/level12/morphi/commit/c4cf37f\n.. _bc0ef3f: https://github.com/level12/morphi/commit/bc0ef3f\n\n\n0.1.2 released 2019-02-11\n-------------------------\n\n- Fix errors when using invalid user-supplied resource paths with the resource loader\n\n\n0.1.1 released 2018-09-20\n-------------------------\n\n- Fix pkg_resources support under pyinstaller\n\n\n0.1.0 released 2018-08-22\n-------------------------\n\n- Add initial translations implementation\n\n\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "i18n services for libraries and applications",
"version": "0.3.2",
"project_urls": {
"Homepage": "https://github.com/level12/morphi"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "13701adea3a3ea81fe5f6ddbd4982bd658194b983b0087abc977aace03c7b5ca",
"md5": "c2b6e049989040247880204d9d320479",
"sha256": "281fe08463eda365fe257c859b2c892eb6b7ecec77255a16cff002ae21ee0b08"
},
"downloads": -1,
"filename": "morphi-0.3.2-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "c2b6e049989040247880204d9d320479",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 17841,
"upload_time": "2025-07-10T12:49:29",
"upload_time_iso_8601": "2025-07-10T12:49:29.048627Z",
"url": "https://files.pythonhosted.org/packages/13/70/1adea3a3ea81fe5f6ddbd4982bd658194b983b0087abc977aace03c7b5ca/morphi-0.3.2-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "76ea03783147fa3321b4e7d1aa2950c578f0cb4188b985af21e0a16fb37db37f",
"md5": "f7a37e0823392c6bb6480449193e8e3a",
"sha256": "bda9d1f12045d694ddf204b1fc44e43caf0ab792a599b818be8188e3b781c08a"
},
"downloads": -1,
"filename": "morphi-0.3.2.tar.gz",
"has_sig": false,
"md5_digest": "f7a37e0823392c6bb6480449193e8e3a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18438,
"upload_time": "2025-07-10T12:49:30",
"upload_time_iso_8601": "2025-07-10T12:49:30.341013Z",
"url": "https://files.pythonhosted.org/packages/76/ea/03783147fa3321b4e7d1aa2950c578f0cb4188b985af21e0a16fb37db37f/morphi-0.3.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-10 12:49:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "level12",
"github_project": "morphi",
"travis_ci": false,
"coveralls": true,
"github_actions": false,
"circle": true,
"tox": true,
"lcname": "morphi"
}