Using gettext to provide different language-version of a script

Tony Nelson *firstname*nlsnews at georgea*lastname*.com
Wed Nov 23 02:23:41 CET 2005


In article <1132661256.664435.311520 at g14g2000cwa.googlegroups.com>,
 "Thomas W" <thomas.weholt at gmail.com> wrote:

> 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 at ADDRESS>, 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 at ADDRESS>\n"
> "Language-Team: LANGUAGE <LL at li.org>\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*nlsnews at georgea*lastname*.com
      '                                  <http://www.georgeanelson.com/>



More information about the Python-list mailing list