Using gettext to provide different language-version of a script


T

Thomas W

I'm trying to wrap my head around the docs at python.org related to the
gettext-module, but I'm having some problem getting it to work. Is
there any really simple, step-by-step on how to use this module
available?

This is my script so far :

import gettext
gettext.install('test2', '.', unicode=1)
lang1 = gettext.translation('test2', languages=['no'])
print _('writing a log to file')

in the folder where the test2.py-script lives I've created a
folder-structure like

../locales/NO/LC_MESSAGES/messages.mo

the messages.mo-file I've created using the scripts in the
\Tools\i18l\-folder by running :

python pygettext.py test2.py

and renaming the generated messages.pot-file to messages.po, and
editing it to look like :

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <[email protected]>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2005-11-22 13:02+W. Europe Standard Time\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: ENCODING\n"
"Generated-By: pygettext.py 1.5\n"


#: test2.py:5
msgid "writing a log to file"
msgstr "skriver logg til fil"

and then run python msgfmt.py messages.po and moving it to

../locales/NO/LC_MESSAGES/messages.mo

When I run

python test2.py

it gives me this error :

Traceback (most recent call last):
File "C:\Program
Files\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 307, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Program
Files\Python24\Lib\site-packages\pythonwin\pywin\debugger\__init__.py",
line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Program
Files\Python24\Lib\site-packages\pythonwin\pywin\debugger\debugger.py",
line 631, in run
exec cmd in globals, locals
File "C:\Program Files\Python24\Tools\i18n\test2.py", line 4, in ?
lang1 = gettext.translation('test2', languages=['no'])
File "C:\Program Files\Python24\lib\gettext.py", line 456, in
translation
raise IOError(ENOENT, 'No translation file found for domain',
domain)
IOError: [Errno 2] No translation file found for domain: 'test2'

Hmmm ... any hints?

Thomas
 
Ad

Advertisements

T

Tony Nelson

"Thomas W said:
I'm trying to wrap my head around the docs at python.org related to the
gettext-module, but I'm having some problem getting it to work. Is
there any really simple, step-by-step on how to use this module
available?

This is my script so far :

import gettext
gettext.install('test2', '.', unicode=1)
lang1 = gettext.translation('test2', languages=['no'])
print _('writing a log to file')

I do:

# assume the script is named "myscript.py":
us = os.path.splitext(os.path.basename(sys.argv[0]))[0]
usdir = os.path.dirname(sys.argv[0])

import gettext
gettext.install(us, usdir)

in the folder where the test2.py-script lives I've created a
folder-structure like

./locales/NO/LC_MESSAGES/messages.mo

Mine looks like:

./en_PL/LC_MESSAGES/myscript.mo

the messages.mo-file I've created using the scripts in the
\Tools\i18l\-folder by running :

python pygettext.py test2.py

and renaming the generated messages.pot-file to messages.po, and
editing it to look like :

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <[email protected]>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2005-11-22 13:02+W. Europe Standard Time\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: ENCODING\n"
"Generated-By: pygettext.py 1.5\n"
...

You need to set the "Content-Type: charset" and
"Content-Transfer-Encoding:". I use:

"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
________________________________________________________________________
TonyN.:' *firstname*[email protected]*lastname*.com
' <http://www.georgeanelson.com/>
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top