ค้นหาเว็บไซต์

การแปลแอปพลิเคชัน 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 ” ตัวอย่างเช่น หากคุณต้องการแปลโปรแกรมของคุณเป็น Arabic ให้สร้างไฟล์ชื่อ “ar.po ” และคัดลอกเนื้อหาจากโฟลเดอร์ “ myprogram.pot ” ลงไป

หากคุณต้องการแปลโปรแกรมของคุณเป็น ภาษาเยอรมัน ให้สร้างไฟล์ “de.po ” และคัดลอกเนื้อหาจาก “myprogram.pot ” ไฟล์ลงไป.. และอื่นๆ คุณต้องสร้างไฟล์สำหรับแต่ละภาษาที่คุณต้องการแปลโปรแกรมของคุณ

ตอนนี้ เราจะทำงานกับไฟล์ “ar.po ” คัดลอกเนื้อหาจากไฟล์ “myprogram.pot ” และใส่ไว้ในไฟล์นั้นและแก้ไขสิ่งต่อไปนี้ : :

  1. ชื่อที่อธิบายบางส่วน: คุณสามารถป้อนชื่อโครงการของคุณที่นี่ได้หากต้องการ
  2. ปีผู้ถือลิขสิทธิ์ของบรรจุภัณฑ์: แทนที่ด้วยปีที่คุณสร้างโครงการ
  3. แพ็คเกจ: แทนที่ด้วยชื่อของแพ็คเกจ
  4. ผู้เขียนคนแรก ปี: แทนที่ด้วยชื่อจริง อีเมล และปีที่คุณแปลไฟล์
  5. เวอร์ชันแพ็คเกจ: แทนที่ด้วยเวอร์ชันแพ็คเกจจากไฟล์เดเบียน/ควบคุม
  6. YEAR-MO-DA HO:MI+ZONE: ไม่ต้องการคำอธิบาย คุณสามารถเปลี่ยนเป็นวันใดก็ได้ที่ต้องการ
  7. ชื่อเต็ม : แทนที่ชื่อและอีเมลของคุณด้วย
  8. ทีมภาษา: แทนที่ด้วยชื่อภาษาที่คุณกำลังแปล เช่น "อาหรับ " หรือ "ฝรั่งเศส "
  9. ภาษา: ที่นี่ คุณต้องใส่รหัส ISO-639-1 สำหรับภาษาที่คุณกำลังแปล เช่น “ar ”, “fr ”, “de ”..etc คุณสามารถ ค้นหารายการทั้งหมดได้ที่นี่
  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

และเปลี่ยนภาษาของระบบโดยใช้โปรแกรม “การสนับสนุนภาษา ” หรือใช้โปรแกรมอื่นเป็น อาหรับ (หรือภาษาที่คุณแปลไฟล์ของคุณ):

หลังจากเลือกแล้ว โปรแกรมของคุณจะถูกแปลเป็นภาษาอาหรับ

ต่อไปนี้จะจบซีรีส์เกี่ยวกับการเขียนโปรแกรม PyGObject สำหรับเดสก์ท็อป Linux แน่นอนว่ายังมีสิ่งอื่นๆ อีกมากมายที่คุณสามารถเรียนรู้ได้จากเอกสารอย่างเป็นทางการและการอ้างอิง Python GI API

คุณคิดอย่างไรเกี่ยวกับซีรีส์นี้? คุณคิดว่ามันมีประโยชน์หรือไม่? คุณสามารถสร้างแอปพลิเคชันแรกของคุณโดยติดตามซีรี่ส์นี้ได้หรือไม่ แบ่งปันความคิดของคุณ!