universal python library for internalization and translation

Question:

I need to internationalize and translate python application. I look forward for some dictionary collection resides in additional resource files that could be switched runtime and used smoothly inside python code.

I’ve searched stackoverflow.com for similar tools but find only platform-specific libraries, e.g. for pylons, for django and so on.

Is there any general ready for use library?

Asked By: ayvango

||

Answers:

Babel provides such tools:

A collection of tools for internationalizing Python applications

Babel is composed of two major parts:

  • tools to build and work with gettext message catalogs
  • a Python interface to the CLDR (Common Locale Data Repository), providing access to various locale display names, localized number and date formatting, etc.

So it helps you translate strings and provide localized float, currency, dates and other information based on a current locale:

>>> locale = Locale('es')
>>> month_names = locale.months['format']['wide'].items()
>>> month_names.sort()
>>> for idx, name in month_names:
...     print name
enero
febrero
marzo
abril
mayo
junio
julio
agosto
septiembre
octubre
noviembre
diciembre
>>> format_decimal(1.2345, locale='en_US')
u'1.234'
>>> format_decimal(1.2345, locale='sv_SE')
u'1,234'
>>> format_decimal(12345, locale='de_DE')
u'12.345'

It builds on top of the default gettext library to do translations, giving you tools to extract messages from a variety of source files (python, templates, etc), including a plugin-system for other packages to provide additional extractors.

Answered By: Martijn Pieters

Python’s standard gettext module provides this. See the Python docs here.

The gettext module provides internationalization (I18N) and
localization (L10N) services for your Python modules and applications.
It supports both the GNU gettext message catalog API and a higher
level, class-based API that may be more appropriate for Python files.
The interface described below allows you to write your module and
application messages in one natural language, and provide a catalog of
translated messages for running under different natural languages.

A simple example:

import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print _('This is a translatable string.')
Answered By: Kyle

I know this is old question, but you can use this cool library to internationalize your python application.

https://github.com/sectasy0/pyi18n

Answered By: sectasy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.