PyGObject アプリケーションを異なる言語に翻訳する - パート 5
私たちはあなたと共にPyGObjectプログラミングシリーズを続けており、こちらの第5部では、PyGObjectアプリケーションを異なる言語に翻訳する方法を学びます。アプリケーションを翻訳することは、世界に公開するつもりであれば重要です。英語を理解しない人も多いため、エンドユーザーにとって使いやすくなります。
翻訳プロセスの仕組み
Linuxデスクトップでプログラムを翻訳する手順は、次のように要約できます:
- Pythonファイルから翻訳可能な文字列を抽出してください。
- .potファイルに文字列を保存してください。この形式は、後で他の言語に翻訳することを可能にします。
- 文字列の翻訳を始めてください。
- .po ファイルに新しい翻訳された文字列をエクスポートし、システム言語が変更されたときに自動的に使用されるようにします。
- メインのPythonファイルと.desktopファイルにいくつかの小さなプログラムの変更を加えてください。
そして、それで終わりです!これらの手順を実行した後、あなたのアプリケーションは世界中の最終ユーザーが使えるようになります(ただし、プログラムを世界中のすべての言語に翻訳する必要があります!)。簡単に聞こえますよね?:-)
最初に、時間を節約するために、以下のリンクからプロジェクトファイルをダウンロードし、ホームディレクトリにファイルを抽出してください。
- 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 ""
今、文字列の翻訳を始めるために… 翻訳したいプログラムの各言語用に、“ISO-639-1” 言語コードを使用して “po” フォルダーの中に別々のファイルを作成します。例えば、プログラムをアラビア語に翻訳したい場合は、“ar.po” というファイルを作成し、“myprogram.pot” ファイルの内容をそれにコピーします。
プログラムをドイツ語に翻訳したい場合は、“de.po”ファイルを作成し、“myprogram.pot”ファイルの内容をコピーしてください。そして、同様に、翻訳したい各言語のためにファイルを作成する必要があります。
では、次に「ar.po」ファイルに取り組みます。「myprogram.pot」ファイルから内容をコピーしてそのファイルの中に置き、以下の項目を編集します:
- いくつかの説明的なタイトル: 望む場合はここにプロジェクトのタイトルを入力できます。
- パッケージの著作権所有者の年: プロジェクトを作成した年に置き換えてください。
- パッケージ: パッケージの名前に置き換えてください。
- 最初の著者 <EMAIL@ADDRESS>, 年: これをあなたの本名、メールアドレス、翻訳した年に置き換えてください。
- パッケージバージョン: debian/controlファイルからパッケージバージョンに置き換えてください。
- YEAR-MO-DA HO:MI+ZONE: 説明は必要ありません。任意の日付に変更できます。
- 氏名 <EMAIL@ADDRESS>: あなたの名前とメールアドレスに置き換えてください。
- 言語チーム: それを翻訳している言語の名前、例えば「アラビア語」や「フランス語」に置き換えてください。
- 言語: ここには、翻訳する言語のISO-639-1コードを挿入する必要があります。例えば、「ar」、「fr」、「de」などです。完全なリストはここで見つけることができます。
- 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 プログラムまたは他のプログラムを使用して アラビア語(またはファイルを翻訳した言語)に変更してください:
選択後、あなたのプログラムはアラビア語に翻訳されます。
ここで、Linuxデスクトップ向けのPyGObjectプログラミングに関するシリーズは終了します。もちろん、公式ドキュメントやPython GI APIリファレンスから学べる他の多くのことがあります。
このシリーズについてどう思いますか?役に立つと思いますか?このシリーズに従って最初のアプリケーションを作成できましたか?あなたの考えを教えてください!



