4.1 - Creating Multilingual Python Sripts (english)

Unser Wissens Forum zum Thema GIMP, Funktionen kurz und bündig erklärt
moderierter Bereich

4.1 - Creating Multilingual Python Sripts (english)

Beitragvon Hans » Di 8. Mär 2011, 16:46

Beta Version

If there is something in this document is not clear or wrong, please don't hesitate to contact me by mail (developer@gimp-atelier.org). I will modify this document.


It's frustrating, if Python scripts for The GIMP available in English language only. It's very simple, this extension to implement. Here you will show the way, how to prepare your scripts for the GIMP for international usage.

In principle you could translate a script for the GIMP from each language into another, but it is usual to write code in English language. This gives the possibility that someone from foreign are able to create the translation into local language for your script

Let's start. In Python is library gettext implemented, which will translate text from code into local language. These library will be processed by environment variable LANG, LANGUAGE and / or LC_MESSAGES (LINUX, Windows???). Because I had some trouble about this with WINDOWS so I decide to include library locale too. We have to include libraries sys and os too. Sys delivers system related commands and functions and os operating system related functions. With library os it is also possible to differentiate between LINUX, WINDOWS and MAC.

In future, we will start Python coding for the GIMP every time with following Code lines:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *
import gettext
import os
import sys
import locale

GAT_DIR = os.path.dirname(os.path.abspath(__file__))
GAT_LOC = ''
GAT_LANG = 'en'

s = locale.getdefaultlocale()
s = s[0]
if s.lower <> 'none':
  s = s.split('_')
  GAT_LANG = s[0]

if sys.platform.startswith('linux'):
  GAT_LOC = GAT_DIR + '/locale'
elif sys.platform.startswith('win'):
  GAT_LOC = GAT_DIR + r'\locale'
else: 
  sys.exit('Platform not supported')

# internationalization i18n
trans = gettext.translation("gat_lines", GAT_LOC, [GAT_LANG], fallback=True)
trans.install()

  • #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    These first two lines identify the interpreter program on LINUX and the character set of he script. On LINUX it's usual to use UTF8 since a few years.
  • from gimpfu import *
    This line describe the interface between Python and The GIMP. Without this line a script is not a Python script for the GIMP.
  • import gettext
    This will inclod library gettext into the script. This will create the function _() for translation.
  • import os
    import sys
    import locale

    This three libraries will provide the script with for example to identify the operating system. This is required becaus WINDOWS didn't understand all commands from LINUX and vice versa. The library locale provides functions to evaluate the used language on a PC.
  • GAT_DIR = os.path.dirname(os.path.abspath(__file__))
    gat_locale = ''

    The global variable __file__ contains the script name some times including path and some times not. os.abspath() delivers absolute path, on WINDOWS including Drive. os.dirname() returned the path of a string.

    WINDOWS7 user, except they are using an English system, are wondering that in explorer a path is shown as c:\Programme and if you copy it then you will have C:\Program Files. The most important directories will have an English name. Microsoft has reliable it for the user. Discussions about this on other site.
  • GAT_DIR = os.path.dirname(os.path.abspath(__file__))
    GAT_LOC = ''
    GAT_LANG = 'en'

    GAT_DIR will be initialized with the path of the script.
  • s = locale.getdefaultlocale()
    s = s[0]
    if s.lower <> 'none':
    s = s.split('_')
    GAT_LANG = s[0]

    This code block returned the used users language. Possible values are 'de','fr','nl', and so on
  • if sys.platform.startswith('linux'):
    GAT_LOC = GAT_DIR + '/locale'
    elif sys.platform.startswith('win'):
    GAT_LOC = GAT_DIR + r'\locale'
    else:
    sys.exit('Platform not supported')

    This code block differentiate the operating systems. After script passed this block GAT_LOC should be filled with path of your script plus '/locale' on LINUX or '\locale' on WINDOWS.

Code: Alles auswählen
# internationalization i18n
trans = gettext.translation("gat_lines", GAT_LOC, [GAT_LANG], fallback=True)
trans.install()

This will implement the translation function _(). All strings which will be processed by this function ill be translated now. Option fallback=True is suppressing an error message in case of missing translation file. If translation file is missing, user will see the program as it is defined on the code. Now you should replace strings like 'Brush Size' with _('Brush Size')

After script is finished and saved open a terminal (LINUX xterm, konsole, on WINDOWS start cmd). Go into directory, where your script is stored.

LINUX
start Python script pygettext.py It's located normally in /usr/bin and could be started without path. pygettext --help displays a short help on screen.


WINDOWS
The script pygettext.py is located in your Python installation (e.g. C:\Program Files\Python) in sub folder Tools\i18n. To start pygettext.py you have to type in the complete path.


With command pygettext.py -o yourScript.pot yourScript.py all strings will be extracted if this is given to function _() and written to new file yourScript.pot. The .pot file has a defined structure. Fits you have a header. You could edit it with a normal editor like vi, emacs, notepad++ or something else but the best option is to use poedit. If you are edit with poedit, please read following text up to chapter poedit only.
Code: Alles auswählen
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2011-03-05 22:19+CET\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@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"

Header will be followed by the translation section. Following example contains two entries
Code: Alles auswählen
#: gat_lines.py:27 gat_lines.py:51
msgid "GIMP Atelier Tools: Scan Lines"
msgstr ""

#: gat_lines.py:52
msgid "creates scan lines"
msgstr ""

The line with the sharp sign is not essential. It's pointing do the line numbers in script, where the String was found. Its followed by line msgstr: and the string which should be translated. The line after this contains msgstr:. Behind this is the place for your translation. After your translation for the whole file is done, save your file as yourScript.po Now go to the command lien window.

With command msgfmt.py -o yourScript.mo yourScript.po all entries will be written in new .mo file. WINDOWS user have to type in the whole path to the msgfmt.py script. (msgfmt.py is in the same folder than pygettext.py).

poedit
Open program poedit (on LINUX fastest way is from command line, WINDOWS user open it by menu entry). Open new Catalog from .pot file. Following screen appears

Bild

Typ in your project data. Pathes and keywords I left unchanged. then it looks like this

Bild

If you close this windows, then a save dialogue for .po file appears. Save the file with same name as your Python script, but with extension .po Following screen appears now

Bild

This window has three sections: On 1 you will find by pygettext.py extracted strings. Activate line which contains string you like to translate. Text from your script will appear in section 2 too. In section 3 type in your translation.

After you are finished, save the file. If your poedit is configured in right way, it creates .mo file automatically.

Move your file to the right folder. If you are following this script, then subfolder locale/.../LC_MESSAGES of your script file is the right one. Replace ... with your language code. To make it simple I'm saving .pot and .po files also in LC_MESSAGESfolder.

Conclusion
  • Create the Python Script and prepare it for translation
  • set _() on all strings which should be translated.
  • create .pot file with pygettext.py
  • open poedit and read new catalog from .pot file
  • type in project data
  • translate all strings
  • save catalog, which creates .mo file automatically
  • move .mo file to the right folder

Tool for LINUX only

I have attached a script with name buildgimppackage which makes it easy to create a package for upload. Unzip attachment to /usr/bin and give execute attribute to it chmod 755 /usr/bin/buildgimppackage. You must have root access for this actions.

If you have translated a Python script acordin g this how to, go to folder of your script. Following example is showing my script gat_lines.py Type in
Code: Alles auswählen
buildgimppackage /tmp/gat_lines.zip gat_lines


The resulot shoul look like this:
Code: Alles auswählen
[hans@chris plug-ins]$ buildgimppackage /tmp/gat_lines.zip gat_lines
updating: locale/de/LC_MESSAGES/gat_lines.mo (deflated 40%)
updating: locale/de/LC_MESSAGES/gat_lines.pot (deflated 50%)
updating: locale/de/LC_MESSAGES/gat_lines.po (deflated 52%)
updating: gat_lines.py (deflated 50%)


done




If file gat_lines.py is not located in current folder, then it looks like this
Code: Alles auswählen
[hans@chris ~]$ buildgimppackage /tmp/gat_lines.zip gat_lines


***** wrong directory /home/hans *****





Sources:

poedit: http://www.poedit.net/download.php
Python from Peter Kaiser, Johannes Ernesti, Chapter 15.3, Galileo Computing, ISBN 978-3-8362-1110-9
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.
Bild
Benutzeravatar
Hans
 
Beiträge: 2883
Registriert: So 27. Jun 2010, 12:56
Wohnort: Angeles City, PH

Zurück zu Wilber's Lexikon

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast

cron