Pyramid and Python 3 internationalization

What is the current state of internationalization support for Pyramid using Python 3?

Currently it seems that the packages used by Pyramid for this, lingua and babel, are not compatible with Python 3.

There is https://bitbucket.org/felixschwarz/babel-py3k but no official release.

Also for lingua I could only find shadows of something called lingua3k but links to it were broken all over the place and also no official releases.

How can I get up and running with Pyramid i18n and Python 3?

+3


source to share


3 answers


I was unable to find information on the status (current or future) of the i18n support packages for the pyramid in python 3 (babel and lingua).

To solve this inconvenience, I did 4 things:

  • Create a second virtual environment with python2.7
  • Add a specific i18n setup file named setup_i18n.py
  • Write a simple wrapper script called translate.py that setup.py will call to translate.
  • Add an entry point to setup.py to point to the translation script.

More details:

  • I created a very meager 2nd virtual and very meager env with python2.7 inside my project. Just call it env_python_2.7 and place it in the root of my project.

  • Then, in the directory where setup.py lives, I created another setup file, setup_i18n.py. This file only makes requirements for what is needed to support i18n. The setup_i18n.py file looks like this:


from setuptools import setup, find_packages

requires = [
    'Babel',
    'lingua',
]

setup(name='my_project',
      packages=find_packages(),
      install_requires=requires,

      message_extractors={'.': [
          ('**.py', 'lingua_python', None),
          ('**.pt', 'lingua_xml', None),
          ]},
      )

      



  • Then I created a script called translate.py and placed it in the same script directory where initializedb.py is initialized when you create a project from a template. The script is pretty generic and could be built for more specific purposes, but looks like this:

import argparse
import subprocess
import sys

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

description = """\
A script to facilitate the translation of strings in the app.
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('ini_file', help='Ini file to setup environment for this script')
parser.add_argument('-a', '--add', metavar='locale',
                    help='Add locale name of new language for translation (eg: en, de)')

def main(argv=sys.argv):
    args = parser.parse_args()

    setup_logging(args.ini_file)
    settings = get_appsettings(args.ini_file)

    # Python 2.7 is needed for translation strings at this time, because the
    # available translation string tools are not compatible with Python 3
    # at this time
    python27 = settings['my_project.python27_dir'] + '/bin/python'
    setup_file = settings['my_project.i18n_setup_file']

    if args.add:
        subprocess.call([python27, setup_file, 'init_catalog', '-l', args.add])

    subprocess.call([python27, setup_file, 'extract_messages'])
    subprocess.call([python27, setup_file, 'update_catalog'])
    subprocess.call([python27, setup_file, 'compile_catalog'])

if __name__ == '__main__':
    main()

      

  • Finally, I changed the setup.py entry points to indicate the translation of the script. My entry points look like this:

entry_points="""\
[paste.app_factory]
main = my_project:main
[console_scripts]
my_project_db = my_project.script.initializedb:main
my_project_translate = my_project.script.translate:main
""",

      

Now I can connect to my application by calling 'python3 setup.py my_project_translate'

+3


source


Py3 Status 6/28/2013

This is an updated answer to two answers posted in March 2013 for anyone else who stumbles upon this by accident.

Work on Babel for Python 3 has not been officially published. The maintainers have an open ticket that does not have any permission here: http://babel.edgewall.org/ticket/209

However, a group of people raised the torch and started an unofficial BitBucket repository to host their work on Babel3: https://bitbucket.org/babel3_developers/babel3

The pyramid also uses a lingua. Currently, if you try to install easy_install / pip install lingua, it will fail. The only reason for this is that xlwt is not officially ported.

If you want to install lingua, you have to manually install xlwt.



Install unofficial Babel3

Right now, there is no official release for Babel in Python 3. There is also nothing published in pypi (ie easy_install babel / babel3 or pip install babel / babel3 doesn't work as expected). However, you can use the unofficial release.

  • To get the unofficial release, you first need to install mercury (source control tool). You need this to check the source code from BitBucket. You can get it here: https://www.mercurial-scm.org/ for Windows users. For Linux / Unix, use your distribution tools to get binaries or compile from source.
  • On the command line, change to the directory where you want to temporarily save the Babel3 source installation files; then run: hg clone https://bitbucket.org/babel3_developers/babel3 (Windows users may need to either enter the full path to hg.exe or make sure they added the hg.exe directory to their path)
  • This will create a directory on your local machine containing the setup.py file. Change to the new directory and run the file: python setup.py install

Installing Lingua

Pyramid i18n relies on another Python module named lingua. It is having problems with another module dependency named xlwt. It has also been unofficially fixed here: https://github.com/tonyroberts/xlwt

  • Get the source for the corrected version of xlwt here: https://github.com/tonyroberts/xlwt (click "Download zip" at bottom right).
  • Extract content to a temporary location
  • Go to command line inside the resulting directory
  • Running: installing python setup.py
  • Now install lingua via: pip install lingua -or- easy_install lingua
+5


source


To get a Python application your way, you usually follow two different steps:

  • Retrieving the message catalog
  • Translating strings

The first part is handled by Babel and lingua, the second part is gettext (or any wrapper around it, like translationstring .

Short answer . Download Poedit to work in a directory and use the regular Pyramid library for translation.

Long answer

So, if Babel and lingua don't work for you, you can create your own message directory yourself. Then, with the existing file .pot

, you can create your own files .po

for each language. If you are using Poedit , it can automatically compile your files .po

to files .mo

. This, in turn, can be handled without any dependency using the Python gettext standard library .

So here's what you do: use Poedit to create a message directory ( File->New Catalog

). Apply all settings carefully. Poedit can even fetch strings, maybe you don't need any manual work here, just take a look at the source sources and source keywords in the directory properties.

Then you translate all your (extracted or manually added) strings. Pyramid expects the following directory structure

myproject/
    locale/
        en/
            LC_MESSAGES/
                myproject.po
                myproject.mo
        myproject.pot

      

File pot

is your directory. The folder is en

intended for translation into English. For any other language placed in the locale here: de

for German, es

for Spanish, etc. You can create the file .po

empty and update Poedit from the file .pot

.

When you're done translating, use the Pyramid documentation for these files . You don't need Babel if you can retrieve your posts in any other way.

As a side note: everything you use for the translation process wraps Pythons gettext, so if you run into any problems you might even go down to this library. This will work anyway, as long as you have a valid directory.

+1


source







All Articles