Paghubad sa mga Aplikasyon sa PyGObject ngadto sa Lainlaing Pinulongan – Bahin 5


Gipadayon namo ang serye sa programming sa PyGObject uban kanimo ug dinhi niining 5th nga bahin, makakat-on kami unsaon paghubad sa among mga aplikasyon sa PyGObject ngadto sa lain-laing mga pinulongan. Ang paghubad sa imong mga aplikasyon hinungdanon kung imong imantala kini alang sa kalibutan, kini mahimong labi ka user friendly alang sa mga end-user tungod kay dili tanan nakasabut sa English.

Giunsa Paglihok ang Proseso sa Paghubad

Mahimo natong i-summarize ang mga lakang sa paghubad sa bisan unsang programa ubos sa desktop sa Linux gamit kining mga lakang:

  1. Kuhaa ang mahubad nga mga string gikan sa Python file.
  2. I-save ang mga string sa usa ka .pot file nga mao ang format nga nagtugot kanimo sa paghubad niini sa ulahi ngadto sa ubang mga pinulongan.
  3. Sugdi ang paghubad sa mga kuwerdas.
  4. Export ang bag-ong gihubad nga mga kuwerdas ngadto sa .po nga payl nga awtomatik nga gamiton kon ang sistema sa pinulongan mausab.
  5. Idugang ang pipila ka gagmay nga mga pagbag-o sa programa sa nag-unang Python file ug ang .desktop file.

Ug mao kana! Human sa pagbuhat niini nga mga lakang ang imong aplikasyon mahimong andam na alang sa paggamit alang sa mga end-user gikan sa tibuok kalibutan (mao ba.. Kinahanglan nimo nga hubaron ang imong programa sa tanan nga mga pinulongan sa tibuok kalibutan, bisan pa!), Morag sayon dili ba? :-)

Una, aron makadaginot og panahon, i-download ang mga file sa proyekto gikan sa ubos nga link ug kuhaa ang file sa imong home directory.

  1. https://copy.com/TjyZAaNgeQ6BB7yn

Ablihi ang \setup.py” nga file ug tan-awa ang mga kausaban nga among gibuhat:

# 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.

Ablihi usab ang \myprogram nga file ug tan-awa ang mga pagbag-o sa programa nga among gibuhat, ang tanan nga mga pagbag-o gipasabut sa mga komento:

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

Karon.. Atong sugdan ang paghubad sa atong programa. Paghimo una sa .pot nga payl (usa ka payl nga naglangkob sa tanang mahubad nga mga kuwerdas sa programa) aron ikaw
makasugod sa paghubad gamit ang mosunod nga sugo:

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

Kini maghimo sa \myprogram.pot” nga payl sulod sa \po” nga folder sa main project folder nga adunay mosunod nga code:

# 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 >, 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 >\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 ""

Karon aron masugdan ang paghubad sa mga kuwerdas.. Paghimo ug bulag nga file para sa matag pinulongan nga gusto nimong hubaron sa imong programa sa paggamit sa \ISO-639-1” nga mga code sa pinulongan sulod sa \po” folder, pananglitan, kon gusto nimong hubaron ang imong programa ngadto sa Arabic, paghimo ug file nga gitawag og \ar.po” ug kopyaha ang mga sulod gikan sa \myprogram.pot” nga file ngadto niini.

Kung gusto nimong hubaron ang imong programa sa German, paghimo og \de.po” nga payl ug kopyaha ang mga sulod gikan sa \myprogram.pot” nga file niini.. ug ang usa, kinahanglang maghimo ka ug file para sa matag pinulongan nga gusto nimong hubaron sa imong programa.

Karon, atong buhaton ang \ar.po” nga payl, kopyaha ang mga sulod gikan sa \myprogram.pot” nga payl ug ibutang kini sulod sa maong file ug i-edit ang mosunod:

  1. PILA NGA DESCRIPTIVE TITLE: mahimo nimong isulod ang titulo sa imong proyekto dinhi kung gusto nimo.
  2. YEAR THE PACKAGE'S COPYRIGHT HOLDER: ilisan kini sa tuig nga imong gibuhat ang proyekto.
  3. PACKAGE: ilisan kini sa ngalan sa package.
  4. FIRST AUTHOR <[email >, YEAR: ilisan kini sa imong tinuod nga ngalan, Email ug ang tuig nga imong gihubad ang file.
  5. PACKAGE VERSION: ilisan kini sa package version gikan sa debian/control file.
  6. YEAR-MO-DA HO:MI+ZONE: wala magkinahanglan og katin-awan, mahimo nimo kining usbon sa bisan unsang petsa nga imong gusto.
  7. FULL NAME <[email >: ilisan usab kini sa imong ngalan ug Email.
  8. Team-Language: ilisan kini sa ngalan sa pinulongan nga imong gihubad, pananglitan \Arabic o \French.
  9. Language: dinhi, kinahanglan nimong isulod ang ISO-639-1 code para sa pinulongan nga imong gihubad, pananglitan \ar, \fr, \de ..ug uban pa, makit-an nimo ang kompleto nga lista dinhi.
  10. CHARSET: kini nga lakang importante, ilisan kini nga string sa \UTF-8” (walay mga kinutlo) nga nagsuporta sa kadaghanan sa mga pinulongan.

Karon sugdi ang paghubad! Idugang ang imong hubad alang sa matag hilo human sa mga kinutlo sa \msgstr”. I-save ang file ug exit. Usa ka maayong translation file para sa
Ang pinulongang Arabiko isip usa ka pananglitan kinahanglan nga tan-awon sama niini:

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

Wala nay mahimo pa, i-package lang ang programa gamit ang mosunud nga mando:

$ debuild -us -uc

Karon sulayi nga i-install ang bag-ong gihimo nga pakete gamit ang mosunud nga mando.

$ sudo dpkg -i myprogram_1.0_all.deb

Ug usba ang sistema nga pinulongan gamit ang programa nga \Suporta sa Pinulongan o gamit ang bisan unsa nga programa ngadto sa Arabic (o ang pinulongan nga imong gihubad sa imong file):

Human sa pagpili, ang imong programa mahubad sa Arabic nga pinulongan.

Dinhi natapos ang among serye bahin sa PyGObject programming alang sa Linux desktop, siyempre adunay daghang uban pang mga butang nga imong mahibal-an gikan sa reference sa Python GI API.

Unsay imong hunahuna bahin sa serye? Nakita ba nimo kini nga mapuslanon? Nakahimo ka ba sa paghimo sa imong una nga aplikasyon pinaagi sa pagsunod niini nga serye? Ipakigbahin kanamo ang imong mga hunahuna!