IronPython - Luke31/i18n-cs GitHub Wiki
(IronPython 2.7)
Example project: IronPythonPackageLib and IronPythonCsharp
##Intro
This tutorial is based on IronPython Python 2.7 - Internationalizing your programs and modules
Tutorial for translation using Poedit (Warning Python 3!): Translate Your Python 3 Program with the gettext Module Unicode mess
A complete guide to i18n in Python with Github example: handroll
##Python-part First we start with the Python-Part. In the example project IronPythonPackageLib there are two packages for demonstration: sample and package.
Given state: The core-module in the sample-package calls a function in the side-module of the package-package. All the strings are given in the source-code.
Desired state: Translations per package
Following steps have been taken in the example-project to achieve this translation:
-
Mark all strings which you'd like to translate with
_('...Text...')
For formatted strings:
Hint: For translated strings: use keys or at least indexes for translaters to change position of replaced strings!
numb1 = 1 numb2 = 2 #Former format string: print('This is a formatted string: %2d %d' % (numb1, numb2)) print(_('This is a formatted string: {n1:2d} {n2:d}').format(n1=numb1, n2=numb2)) print(_('This is a formatted string: {0:2d} {1:d}').format(numb1, numb2)) print(_('This is a formatted string: %(n1)2d %(n2)d') % {'n1':numb1, 'n2':numb2})
-
Run pygettext.py (Similar to GNU xgettext) from C:\Python27\Tools\i18n on your IronPython-packages containing your .py-files
Hint: IronPython does not contain a Tools\i18n folder! Use the one from a regular Python 2.7 instance instead (in the example IronPythonPackageLib already contains a copy of pygettext.py for convenience)
pygettext.py -d sample sample/*.py pygettext.py -d package package/*.py
This will get you a sample.pot and package.pot template file containing all the marked strings per package.
-
Move the generated sample.pot template file to IronPythonPackageLib/locale/sample.pot
-
Copy it and save it to IronPythonPackageLib/locale/ja/LC_MESSAGES/sample.po (Yes .po, not .pot - make sure the file is saved in Unicode). Now you may open it and translate the strings: msgid is the original string, msgstr is the translation. Don't forget to set "Language: ja\n"
Hint: You may use the Tool Poedit to translate your strings:
-
Convert the .po-file to a .mo-binary-file using msgfmt.py in C:\Python27\Tools\i18n
Hint: If you've used Poedit, the tool has already done this for you :)
msgfmt.py sample
-
Repeat steps 3-5 for package.pot
-
To localize per package (Domain per Package) we'll introduce the ()-function per package-namespace. Add the following code in the _init_.py-file of the sample-packge:
current_locale, encoding = locale.getdefaultlocale() _ = gettext.translation('sample', 'locale', [current_locale], fallback = True).ugettext #unicode gettext
-
Do the same for the __init__.py-file of the package-packge (Write package instead of sample)
-
Python-part finished!
-
Addition maintenance: If you're source file has changd and you need to add the new translations, just edit the .po and add the new strings
OR: Even easier is the process with Poedit:
- Generate the new .pot file as usual
- Open your existing .po-translation with Poedit and select Catalogue -> Update from POT-file
- Select the new generated .pot file
- All new strings have been added to your existing translation
- Translate the new strings to Japanese
Hint: For all files containing UTF-8 characters, put this at top of file:
# -*- coding: utf-8 -*-
Addition: If you'd like to change languages on the fly use this snippet (Only as an info, not in example, see: Changing languages on the fly):
import gettext
langEn = gettext.translation('IronPython', languages=['en'])
langJa = gettext.translation('IronPython', languages=['ja'])
# start by using language1
langEn.install()
##C-Sharp-Part The C#-part is not described in this tutorial, see the IronPythonCsharp-Project for how to execute the IronPython-Assembly from C#.
##Python to C-Sharp The make_python_dll.bat-Script in IronPythonPackageLib/tool is used to perform the following tasks:
- Package the standard python libraries to an assembly (stdipy.dll and stdipyencod.dll)
- Package the custom python packages sample and package to sample.dll and package.dll
- Add custom resource informations to the dlls using ResHacker.exe
- Copy all the assemblies and the locale-folder to IronPythonCsharp/bin/Release
You may start the application using IronPythonCsharp/bin/Release/IronPythonCsharp.exe
##FAQ:
-
Why are not keys used in msgid?
See Combining keys and full text when working with gettext and .po files
-
What options do we have to get different translations per package?
See How do I use multiple .mo files simultaneously for gettext translation?
-
Why should we use keywords for formatting instead of % or {}?
The order of the inserted strings may change depending on language, See: "You have a {color} {car}" and "Vous avez une {car} {color}" How to use % and .format() See: PyFormat