网站搜索

将 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

这将在主项目文件夹的“po”文件夹中创建“myprogram.pot”文件,其中包含以下代码:

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

现在,为了开始翻译字符串..为您想要将程序翻译成的每种语言创建一个单独的文件,使用“po”中的“ISO-639-1”语言代码”文件夹,例如,如果要将程序翻译为阿拉伯语,请创建一个名为“ar.po”的文件,然后复制“ myprogram.pot ” 文件。

如果您想将程序翻译为德语,请创建一个“de.po”文件并复制“myprogram.pot”中的内容因此,您必须为要将程序翻译成的每种语言创建一个文件。

现在,我们将处理“ar.po”文件,复制“myprogram.pot”文件中的内容并将其放入该文件中并编辑以下内容:

  1. 一些描述性标题:如果需要,您可以在此处输入项目的标题。
  2. YEAR THE PACKAGE'S COPYRIGHT HOLDER:将其替换为您创建该项目的年份。
  3. PACKAGE:将其替换为包的名称。
  4. 第一作者,年份:将其替换为您的真实姓名、电子邮件和翻译文件的年份。
  5. 包版本:将其替换为 debian/control 文件中的包版本。
  6. YEAR-MO-DA HO:MI+ZONE:不需要解释,你可以把它改成任何你想要的日期。
  7. 全名:也将其替换为您的姓名和电子邮件。
  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

并使用“语言支持”程序或使用任何其他程序将系统语言更改为阿拉伯语(或您将文件翻译成的语言):

选择后,您的程序将被翻译成阿拉伯语。

我们关于 Linux 桌面 PyGObject 编程的系列到这里就结束了,当然还有很多其他的东西你可以从官方文档和 Python GI API 参考中学习。

您对该系列有何看法?你觉得有用吗?您能够按照本系列教程创建您的第一个应用程序吗?与我们分享您的想法!