Μετάφραση εφαρμογών PyGObject σε διαφορετικές γλώσσες – Μέρος 5


Συνεχίζουμε τη σειρά προγραμματισμού PyGObject μαζί σας και εδώ σε αυτό το 5ο μέρος, θα μάθουμε πώς να μεταφράσουμε τις εφαρμογές μας PyGObject σε διαφορετικές γλώσσες. Η μετάφραση των εφαρμογών σας είναι σημαντική εάν πρόκειται να τη δημοσιεύσετε για τον κόσμο, θα είναι πιο φιλική προς τον χρήστη για τους τελικούς χρήστες, επειδή δεν καταλαβαίνουν όλοι Αγγλικά.

Πώς λειτουργεί η διαδικασία μετάφρασης

Μπορούμε να συνοψίσουμε τα βήματα της μετάφρασης οποιουδήποτε προγράμματος στην επιφάνεια εργασίας Linux χρησιμοποιώντας αυτά τα βήματα:

  1. Εξαγωγή των μεταφράσιμων συμβολοσειρών από το αρχείο Python.
  2. Αποθηκεύστε τις συμβολοσειρές σε ένα αρχείο .pot που είναι μορφή που σας επιτρέπει να τα μεταφράσετε αργότερα σε άλλες γλώσσες.
  3. Ξεκινήστε τη μετάφραση των χορδών.
  4. Εξάγετε τις νέες μεταφρασμένες συμβολοσειρές σε ένα αρχείο .po το οποίο θα χρησιμοποιείται αυτόματα όταν αλλάξει η γλώσσα του συστήματος.
  5. Προσθέστε μερικές μικρές προγραμματικές αλλαγές στο κύριο αρχείο Python και στο αρχείο .desktop.

Και αυτό είναι! Αφού κάνετε αυτά τα βήματα, η εφαρμογή σας θα είναι έτοιμη για χρήση για τελικούς χρήστες από όλο τον κόσμο (θα πρέπει να μεταφράσετε το πρόγραμμά σας σε όλες τις γλώσσες σε όλο τον κόσμο, ωστόσο!), Ακούγεται εύκολο, έτσι δεν είναι; :-)

Αρχικά, για να εξοικονομήσετε χρόνο, κατεβάστε τα αρχεία του έργου από τον παρακάτω σύνδεσμο και εξαγάγετε το αρχείο στον αρχικό σας κατάλογο.

  1. https://copy.com/TjyZAaNgeQ6BB7yn

Ανοίξτε το αρχείο "setup.py " και παρατηρήστε τις αλλαγές που κάναμε:

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.

Ανοίξτε επίσης το αρχείο "myprogram " και δείτε τις αλλαγές μέσω προγραμματισμού που κάναμε, όλες οι αλλαγές εξηγούνται στα σχόλια:

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

Τώρα.. Ας ξεκινήσουμε τη μετάφραση του προγράμματός μας. Πρώτα δημιουργήστε το αρχείο .pot (ένα αρχείο που περιέχει όλες τις μεταφράσιμες συμβολοσειρές του προγράμματος) ώστε να
μπορεί να ξεκινήσει τη μετάφραση χρησιμοποιώντας την ακόλουθη εντολή:

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

Αυτό θα δημιουργήσει το αρχείο "myprogram.pot " μέσα στο φάκελο "po " στον κύριο φάκελο του έργου που περιέχει τον ακόλουθο κώδικα:

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

Τώρα για να ξεκινήσετε τη μετάφραση των συμβολοσειρών.. Δημιουργήστε ένα ξεχωριστό αρχείο για κάθε γλώσσα στην οποία θέλετε να μεταφράσετε το πρόγραμμά σας χρησιμοποιώντας τους κώδικες γλωσσών "ISO-639-1" μέσα στο "po ", για παράδειγμα, εάν θέλετε να μεταφράσετε το πρόγραμμά σας σε Αραβικά, δημιουργήστε ένα αρχείο με το όνομα "ar.po " και αντιγράψτε τα περιεχόμενα από το " myprogram.pot ” αρχείο σε αυτό.

Εάν θέλετε να μεταφράσετε το πρόγραμμά σας στα γερμανικά, δημιουργήστε ένα αρχείο "de.po " και αντιγράψτε τα περιεχόμενα από το "myprogram.pot " αρχείο σε αυτό.. και έτσι ένα, πρέπει να δημιουργήσετε ένα αρχείο για κάθε γλώσσα στην οποία θέλετε να μεταφράσετε το πρόγραμμά σας.

Τώρα, θα δουλέψουμε στο αρχείο "ar.po", θα αντιγράψουμε τα περιεχόμενα από το αρχείο "myprogram.pot " και θα το βάλουμε μέσα σε αυτό το αρχείο και θα επεξεργαστούμε τα ακόλουθα :

  1. ΜΕΡΙΚΟΣ ΠΕΡΙΓΡΑΦΙΚΟΣ ΤΙΤΛΟΣ: μπορείτε να εισαγάγετε τον τίτλο του έργου σας εδώ αν θέλετε.
  2. ΕΤΟΣ ΚΑΤΟΧΟΣ ΠΝΕΥΜΑΤΙΚΩΝ ΔΙΚΑΙΩΜΑΤΩΝ ΤΟΥ ΠΑΚΕΤΟΣ: αντικαταστήστε το με το έτος που δημιουργήσατε το έργο.
  3. ΠΑΚΕΤΟ: αντικαταστήστε το με το όνομα του πακέτου.
  4. ΠΡΩΤΟΣ ΣΥΓΓΡΑΦΕΑΣ , ΕΤΟΣ: αντικαταστήστε το με το πραγματικό σας όνομα, τη διεύθυνση ηλεκτρονικού ταχυδρομείου και το έτος που μεταφράσατε το αρχείο.
  5. ΕΚΔΟΣΗ ΠΑΚΕΤΟ: αντικαταστήστε την με την έκδοση πακέτου από το αρχείο debian/control.
  6. YEAR-MO-DA HO:MI+ZONE: δεν χρειάζεται εξήγηση, μπορείτε να την αλλάξετε σε οποιαδήποτε ημερομηνία θέλετε.
  7. ΠΛΗΡΕΣ ΟΝΟΜΑ : αντικαταστήστε επίσης το όνομά σας και το email σας.
  8. Γλώσσα-Ομάδα: αντικαταστήστε το με το όνομα της γλώσσας στην οποία μεταφράζετε, για παράδειγμα "Αραβικά" ή "Γαλλικά".
  9. Γλώσσα: εδώ, πρέπει να εισαγάγετε τον κωδικό ISO-639-1 για τη γλώσσα στην οποία μεταφράζετε, για παράδειγμα “ar ”, “fr ”, “de ”..κ.λπ., μπορείτε βρείτε μια πλήρη λίστα εδώ.
  10. CHARSET: αυτό το βήμα είναι σημαντικό, αντικαταστήστε αυτήν τη συμβολοσειρά με "UTF-8" (χωρίς τα εισαγωγικά) που υποστηρίζει τις περισσότερες γλώσσες.

Τώρα ξεκινήστε τη μετάφραση! Προσθέστε τη μετάφρασή σας για κάθε συμβολοσειρά μετά τα εισαγωγικά στο "msgstr ". Αποθηκεύστε το αρχείο και βγείτε. Ένα καλό αρχείο μετάφρασης για το
Η αραβική γλώσσα ως παράδειγμα θα πρέπει να μοιάζει με αυτό:

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

Δεν υπάρχει τίποτα άλλο να κάνετε, απλώς συσκευάστε το πρόγραμμα χρησιμοποιώντας την ακόλουθη εντολή:

debuild -us -uc

Τώρα δοκιμάστε να εγκαταστήσετε το νέο πακέτο που δημιουργήθηκε χρησιμοποιώντας την ακόλουθη εντολή.

sudo dpkg -i myprogram_1.0_all.deb

Και αλλάξτε τη γλώσσα συστήματος χρησιμοποιώντας το πρόγραμμα "Language Support " ή χρησιμοποιώντας οποιοδήποτε άλλο πρόγραμμα σε Αραβικά (ή τη γλώσσα στην οποία μεταφράσατε το αρχείο σας):

Μετά την επιλογή, το πρόγραμμά σας θα μεταφραστεί στην αραβική γλώσσα.

Εδώ τελειώνει η σειρά μας σχετικά με τον προγραμματισμό PyGObject για την επιφάνεια εργασίας Linux, φυσικά υπάρχουν πολλά άλλα πράγματα που μπορείτε να μάθετε από την επίσημη τεκμηρίωση και την αναφορά Python GI API..

Τι γνώμη έχετε για τη σειρά; Το βρίσκετε χρήσιμο; Καταφέρατε να δημιουργήσετε την πρώτη σας εφαρμογή ακολουθώντας αυτή τη σειρά; Μοιραστείτε μας τις σκέψεις σας!