Prevođenje PyGObject aplikacija na različite jezike – 5. dio


Nastavljamo seriju PyGObject programiranja s vama i ovdje u ovom 5. dijelu, naučit ćemo kako prevesti naše PyGObject aplikacije na različite jezike. Prijevod vaših aplikacija je važan ako ih namjeravate objaviti za svijet, bit će lakši za krajnje korisnike jer ne razumiju svi engleski.

Kako funkcionira proces prevođenja

Možemo sažeti korake prevođenja bilo kojeg programa pod radnu površinu Linuxa koristeći ove korake:

  1. Ekstrahirajte prevodljive nizove iz Python datoteke.
  2. Spremite nizove u .pot datoteku koja je format koji vam omogućuje kasnije prevođenje na druge jezike.
  3. Počnite prevoditi nizove.
  4. Izvezite nove prevedene nizove u datoteku .po koja će se automatski koristiti kada se promijeni jezik sustava.
  5. Dodajte neke male programske promjene u glavnu Python datoteku i datoteku .desktop.

I to je to! Nakon što poduzmete ove korake, vaša će aplikacija biti spremna za upotrebu za krajnje korisnike iz cijelog svijeta (hoće.. Ipak, morate prevesti svoj program na sve jezike diljem svijeta!), Zvuči jednostavno, zar ne? :-)

Prvo, da uštedite malo vremena, preuzmite projektne datoteke s donje veze i izdvojite datoteku u svoj matični direktorij.

  1. https://copy.com/TjyZAaNgeQ6BB7yn

Otvorite datoteku “setup.py” i primijetite promjene koje smo napravili:

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.

Također otvorite datoteku “myprogram” i pogledajte programske promjene koje smo napravili, sve promjene objašnjene su u komentarima:

#!/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()

Sada.. Počnimo prevoditi naš program. Najprije izradite .pot datoteku (datoteku koja sadrži sve prevodljive nizove u programu) tako da
može započeti prevođenje pomoću sljedeće naredbe:

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

Ovo će stvoriti datoteku “myprogram.pot” unutar mape “po” u glavnoj mapi projekta koja sadrži sljedeći kod:

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 ""

Sada kako bismo započeli s prevođenjem nizova.. Stvorite odvojenu datoteku za svaki jezik na koji želite prevesti svoj program koristeći “ISO-639-1” kodove jezika unutar “po ”, na primjer, ako želite prevesti svoj program na arapski, stvorite datoteku pod nazivom “ar.po ” i kopirajte sadržaj iz “ myprogram.pot ” datoteku.

Ako želite prevesti svoj program na njemački, kreirajte “de.po” datoteku i kopirajte sadržaj iz “myprogram.pot” datoteku u njega.. i tako prvo, morate stvoriti datoteku za svaki jezik na koji želite prevesti svoj program.

Sada ćemo raditi na “ar.po” datoteci, kopirati sadržaj iz “myprogram.pot” datoteke i staviti ga unutar te datoteke i urediti sljedeće :

  1. NEKI OPISNI NASLOV: ovdje možete unijeti naslov svog projekta ako želite.
  2. GODINA NOSITELJA AUTORSKOG PRAVA PAKETA: zamijenite je godinom kada ste izradili projekt.
  3. PAKET: zamijenite ga nazivom paketa.
  4. PRVI AUTOR , GODINA: zamijenite ovo svojim pravim imenom, e-poštom i godinom kada ste preveli datoteku.
  5. VERZIJA PAKETA: zamijenite je verzijom paketa iz debian/kontrolne datoteke.
  6. GODINA-MO-DA HO:MI+ZONE: ne treba objašnjenje, možete ga promijeniti na bilo koji datum.
  7. PUNO IME : također zamijenite svoje ime i e-poštu.
  8. Tim za jezik: zamijenite ga nazivom jezika na koji prevodite, na primjer "arapski" ili "francuski".
  9. Jezik: ovdje morate umetnuti ISO-639-1 kod za jezik na koji prevodite, na primjer “ar”, “fr”, “de”..itd, možete potpuni popis pronađite ovdje.
  10. ZNAKOVI: ovaj korak je važan, zamijenite ovaj niz s “UTF-8 ” (bez navodnika) koji podržava većinu jezika.

Sada počnite prevoditi! Dodajte svoj prijevod za svaki niz nakon navodnika u “msgstr”. Spremite datoteku i izađite. Dobra datoteka prijevoda za
Arapski jezik kao primjer trebao bi izgledati ovako:

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 "اضغط عليّ لفتح الطرفية"

Nema više što učiniti, samo zapakirajte program pomoću sljedeće naredbe:

debuild -us -uc

Sada pokušajte instalirati novostvoreni paket pomoću sljedeće naredbe.

sudo dpkg -i myprogram_1.0_all.deb

I promijenite jezik sustava pomoću programa “Language Support” ili pomoću bilo kojeg drugog programa na arapski (ili jezik na koji ste preveli svoju datoteku):

Nakon odabira, vaš program će biti preveden na arapski jezik.

Ovdje završava naš niz o PyGObject programiranju za Linux desktop, naravno postoje mnoge druge stvari koje možete naučiti iz službene dokumentacije i Python GI API reference.

Što mislite o seriji? Smatrate li ga korisnim? Jeste li uspjeli izraditi svoju prvu aplikaciju prateći ovu seriju? Podijelite s nama svoje mišljenje!