Zoeken op website

PyGObject-applicaties vertalen naar verschillende talen – Deel 5


We vervolgen de PyGObject-programmeerserie met u en hier in dit 5e deel leren we hoe we onze PyGObject-applicaties in verschillende talen kunnen vertalen. Het vertalen van uw applicaties is belangrijk als u deze voor de wereld gaat publiceren. Het zal gebruiksvriendelijker zijn voor eindgebruikers omdat niet iedereen Engels verstaat.

Hoe het vertaalproces werkt

We kunnen de stappen voor het vertalen van elk programma onder de Linux-desktop samenvatten met behulp van deze stappen:

  1. Extraheer de vertaalbare tekenreeksen uit het Python-bestand.
  2. Sla de tekenreeksen op in een .pot-bestand met een formaat waarmee u het later naar andere talen kunt vertalen.
  3. Begin met het vertalen van de snaren.
  4. Exporteer de nieuwe vertaalde tekenreeksen naar een .po-bestand dat automatisch zal worden gebruikt wanneer de systeemtaal wordt gewijzigd.
  5. Voeg enkele kleine programmatische wijzigingen toe aan het Python-hoofdbestand en het .desktop-bestand.

En dat is het! Nadat u deze stappen heeft uitgevoerd, is uw applicatie klaar voor gebruik door eindgebruikers van over de hele wereld (wel.. U moet uw programma echter wel naar alle talen over de hele wereld vertalen!), Klinkt eenvoudig, nietwaar? :-)

Om wat tijd te besparen, downloadt u eerst de projectbestanden via onderstaande link en pakt u het bestand uit in uw thuismap.

  1. https://copy.com/TjyZAaNgeQ6BB7yn

Open het bestand “setup.py” en bekijk de wijzigingen die we hebben aangebracht:

Here we imported the 'setup' module which allows us to install Python scripts to the local system beside performing some other tasks, you can find the documentation here: https://docs.python.org/2/distutils/apiref.html
from distutils.core import setup

Those modules will help us in creating the translation files for the program automatically.
from subprocess import call
from glob import glob
from os.path import splitext, split

DON'T FOTGET TO REPLACE 'myprogram' WITH THE NAME OF YOUR PROGRAM IN EVERY FILE IN THIS PROJECT.

data_files = [ ("lib/myprogram", ["ui.glade"]), # This is going to install the "ui.glade" file under the /usr/lib/myprogram path.
                     ("share/applications", ["myprogram.desktop"]) ] 

This code does everything needed for creating the translation files, first it will look for all the .po files inside the po folder, then it will define the default path for where to install the translation files (.mo) on the local system, then it's going to create the directory on the local system for the translation files of our program and finally it's going to convert all the .po files into .mo files using the "msgfmt" command.
po_files = glob("po/*.po")
for po_file in po_files:
  lang = splitext(split(po_file)[1])[0]
  mo_path = "locale/{}/LC_MESSAGES/myprogram.mo".format(lang)
Make locale directories
  call("mkdir -p locale/{}/LC_MESSAGES/".format(lang), shell=True)
Generate mo files
  call("msgfmt {} -o {}".format(po_file, mo_path), shell=True)
  locales = map(lambda i: ('share/'+i, [i+'/myprogram.mo', ]), glob('locale/*/LC_MESSAGES'))

Here, the installer will automatically add the .mo files to the data files to install them later.
  data_files.extend(locales)

setup(name = "myprogram", # Name of the program.
      version = "1.0", # Version of the program.
      description = "An easy-to-use web interface to create & share pastes easily", # You don't need any help here.
      author = "TecMint", # Nor here.
      author_email = "[email ",# Nor here :D
      url = "http://example.com", # If you have a website for you program.. put it here.
      license='GPLv3', # The license of the program.
      scripts=['myprogram'], # This is the name of the main Python script file, in our case it's "myprogram", it's the file that we added under the "myprogram" folder.

Here you can choose where do you want to install your files on the local system, the "myprogram" file will be automatically installed in its correct place later, so you have only to choose where do you want to install the optional files that you shape with the Python script
      data_files=data_files) # And this is going to install the .desktop file under the /usr/share/applications folder, all the folder are automatically installed under the /usr folder in your root partition, you don't need to add "/usr/ to the path.

Open ook het bestand “mijnprogramma” en bekijk de programmatische wijzigingen die we hebben aangebracht. Alle wijzigingen worden uitgelegd in de opmerkingen:

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

## Replace your name and email.
My Name <[email >

## Here you must add the license of the file, replace "MyProgram" with your program name.
License:
   MyProgram is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
#
   MyProgram is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
#
   You should have received a copy of the GNU General Public License
   along with MyProgram.  If not, see <http://www.gnu.org/licenses/>.

from gi.repository import Gtk 
import os, gettext, locale

## This is the programmatic change that you need to add to the Python file, just replace "myprogram" with the name of your program. The "locale" and "gettext" modules will take care about the rest of the operation.
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain('myprogram', '/usr/share/locale')
gettext.textdomain('myprogram')
_ = gettext.gettext
gettext.install("myprogram", "/usr/share/locale")

class Handler: 
  
  def openterminal(self, button): 
    ## When the user clicks on the first button, the terminal will be opened.
    os.system("x-terminal-emulator ")
  
  def closeprogram(self, button):
    Gtk.main_quit()
    
Nothing new here.. We just imported the 'ui.glade' file. 
builder = Gtk.Builder() 
builder.add_from_file("/usr/lib/myprogram/ui.glade") 
builder.connect_signals(Handler()) 

label = builder.get_object("label1")
Here's another small change, instead of setting the text to ("Welcome to my Test program!") we must add a "_" char before it in order to allow the responsible scripts about the translation process to recognize that it's a translatable string.
label.set_text(_("Welcome to my Test program !"))

button = builder.get_object("button2")
And here's the same thing.. You must do this for all the texts in your program, elsewhere, they won't be translated.
button.set_label(_("Click on me to open the Terminal"))


window = builder.get_object("window1") 
window.connect("delete-event", Gtk.main_quit)
window.show_all() 
Gtk.main()

Nu.. Laten we beginnen met het vertalen van ons programma. Maak eerst het bestand .pot (een bestand dat alle vertaalbare tekenreeksen in het programma bevat), zodat u
kan beginnen met vertalen met behulp van de volgende opdracht:

cd myprogram
xgettext --language=Python --keyword=_ -o po/myprogram.pot myprogram

Hiermee wordt het bestand “mijnprogramma.pot” aangemaakt in de map “po” in de hoofdprojectmap, die de volgende code bevat:

SOME DESCRIPTIVE TITLE.
Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
This file is distributed under the same license as the PACKAGE package.
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-12-29 21:28+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email >\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: myprogram:48
msgid "Welcome to my Test program !"
msgstr ""

#: myprogram:52
msgid "Click on me to open the Terminal"
msgstr ""

Om nu te beginnen met het vertalen van de tekenreeksen. Maak een afzonderlijk bestand voor elke taal waarnaar u uw programma wilt vertalen, met behulp van de “ISO-639-1” taalcodes in de “po ” map, als u bijvoorbeeld uw programma naar Arabisch wilt vertalen, maakt u een bestand met de naam “ar.po ” en kopieert u de inhoud van de map “ mijnprogramma.pot ”-bestand ernaar.

Als u uw programma naar Duits wilt vertalen, maak dan een “de.po ”-bestand en kopieer de inhoud van “mijnprogramma.pot ” bestand ernaar toe. En dus moet u een bestand maken voor elke taal waarnaar u uw programma wilt vertalen.

Nu gaan we werken aan het bestand “ar.po ”, kopiëren de inhoud van het bestand “mijnprogramma.pot ” en plaatsen het in dat bestand en bewerken het volgende :

  1. ENKELE BESCHRIJVENDE TITEL: u kunt hier de titel van uw project invoeren als u dat wilt.
  2. JAAR VAN DE AUTEURSRECHTHOUDER VAN HET PAKKET: vervang dit door het jaar waarin u het project heeft gemaakt.
  3. PAKKET: vervang het door de naam van het pakket.
  4. EERSTE AUTEUR , JAAR: vervang dit door uw echte naam, e-mailadres en het jaar waarin u het bestand heeft vertaald.
  5. PAKKETVERSIE: vervang het door de pakketversie uit het debian/control-bestand.
  6. JAAR-MA-DA HO:MI+ZONE: heeft geen uitleg nodig, je kunt het wijzigen in elke gewenste datum.
  7. VOLLEDIGE NAAM : vervang dit ook door uw naam en e-mailadres.
  8. Taalteam: vervang dit door de naam van de taal waarnaar u vertaalt, bijvoorbeeld 'Arabisch' of 'Frans'.
  9. Taal: hier moet u de ISO-639-1-code invoeren voor de taal waarnaar u vertaalt, bijvoorbeeld "ar", "fr", "de"..etc, dat kan vind hier een volledige lijst.
  10. CHARSET: deze stap is belangrijk, vervang deze string door “UTF-8 ” (zonder de aanhalingstekens) die de meeste talen ondersteunt.

Begin nu met vertalen! Voeg uw vertaling toe voor elke tekenreeks na de aanhalingstekens in “msgstr ”. Sla het bestand op en sluit af. Een goed vertaalbestand voor de
De Arabische taal als voorbeeld zou er als volgt uit moeten zien:

My Program
Copyright (C) 2014
This file is distributed under the same license as the myprogram package.
Hanny Helal <[email <, 2014.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-12-29 21:28+0200\n"
"PO-Revision-Date: 2014-12-29 22:28+0200\n"
"Last-Translator: M.Hanny Sabbagh <hannysabbagh<@hotmail.com<\n"
"Language-Team: Arabic <[email <\n"
"Language: ar\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: myprogram:48
msgid "Welcome to my Test program !"
msgstr "أهلًا بك إلى برنامجي الاختباري!"

#: myprogram:52
msgid "Click on me to open the Terminal"
msgstr "اضغط عليّ لفتح الطرفية"

U hoeft niets meer te doen, u hoeft alleen maar het programma te verpakken met de volgende opdracht:

debuild -us -uc

Probeer nu het nieuw gemaakte pakket te installeren met behulp van de volgende opdracht.

sudo dpkg -i myprogram_1.0_all.deb

En verander de systeemtaal met behulp van het programma “Taalondersteuning” of gebruik een ander programma naar Arabisch (of de taal waarnaar u uw bestand heeft vertaald):

Na selectie wordt uw programma vertaald naar de Arabische taal.

Hier eindigt onze serie over PyGObject-programmeren voor de Linux-desktop. Natuurlijk zijn er nog veel meer dingen die je kunt leren uit de officiële documentatie en de Python GI API-referentie.

Wat vind je van de serie? Vind je het nuttig? Heb je je eerste applicatie kunnen maken door deze serie te volgen? Deel ons uw mening!