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?
Advertisement
Answer
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.