Pythonã§gettextã«ããã¡ãã»ã¼ã¸å½éåãç¨ããä¸ã§ã®gettext.install()ã«é¢ãã追å ã¡ã¢(å¾å)
ãPythonでgettextによるメッセージ国際化を用いる上でのgettext.install()に関する追加メモ(前半)ãã®ç¶ãã¨ãã¦ããPythonでgettextを使用してNLS(Native Language Support)によるメッセージの国際化を行う(作業の流れと動作確認・前半)ãã¨åæ§ã®ä¾ãè²¼ãä»ããã
ä¸ã®ä¾ã¯æååã«ã¤ãã¦ã¡ã³ãé¢æ°format()ãç¨ãã¦ãããããå®è¡ã«ã¯ãã¼ã¸ã§ã³2.6以ä¸(3ç³»å«ã)ã®Pythonãå¿
è¦ã
[ä»»æ]ãã¡ã¤ã«å: bin/gettexttest2.py
#! /usr/bin/python # -*- coding: utf-8 -*- import gettext import os # ãã¡ã¤ã³(ããã±ã¼ã¸)åã¨.moãã¡ã¤ã«ã®å ´æãæå®ãã¦ãgettextã使ç¨ã§ããããã«ãã # å¼æ°namesã¯gettext.ngettext()ãçµã¿è¾¼ã¿åå空éã«çµã¿è¾¼ãã§ãngettextãã¨ãã¦ä½¿ããããã«ããæå® # (çµã¿è¾¼ã¿åå空éã¯ã.ããä»ããã«è¡¨ç¾å¯è½ãªåå空é) # gettext,ngettext,lgettextã®ãããããå«ãæååã®ã·ã¼ã±ã³ã¹(ãªã¹ããã¿ãã«)ãæå® gettext.install ('gettexttest2', os.path.join (os.path.dirname (os.path.dirname (os.path.realpath (os.path.realpath (__file__)))), 'share', 'locale'), names=('ngettext',)) # gettext.install()ã使ç¨ããªãå ´åã®ä¸é£ã®å¦ç # from gettext import gettext as _ # çµã¿è¾¼ã¿åå空éã®_()é¢æ°ã¨ãã¦gettext.gettext()é¢æ°ãç¨ãã # from gettext import ngettext # gettext.ngettext()é¢æ°ãçµã¿è¾¼ã¿åå空éã«çµã¿è¾¼ã # gettext.bindtextdomain ('gettexttest2', os.path.join (os.path.dirname (os.path.dirname (os.path.realpath (__file__))), 'share', 'locale')) # gettext.textdomain ('gettexttest2') # é常ã®ã¡ãã»ã¼ã¸ # TRANSLATORS: comment string print (_('message')) # ãã©ã¼ããã str = 'test' print (_('string: {0}').format (str)) # æ°ã®è¡¨ç¾ # ngettext()ã¯åæ°ç¨ãã¿ã¼ã³ã¨è¤æ°ç¨ãã¿ã¼ã³ãæ±ãã # ãgettext.ngettext()ãã¨ããã¨è±èªã®ã¾ã¾ã¨ãªã items = 1 # åæ°æ±ã print (ngettext ('{0:d} item', '{0:d} items', items).format (items)) items = 2 # è¤æ°æ±ã print (ngettext ('{0:d} item', '{0:d} items', items).format (items)) items = 0 # è¤æ°æ±ã print (ngettext ('{0:d} item', '{0:d} items', items).format (items)) # é çªãé転ããä¾ # ãã¼ã¸ã§ã³2.6系以ä¸ã§ä½¿ç¨å¯è½ãªæååã¡ã½ããformat()ã§ã¯é çªãæå®ã§ããã®ã§ # 翻訳æååå´ã§æ°åãå ¥ãæ¿ãããã¨ãã§ãã # æ³¢æ¬å¼§ã«ååãå«ã¾ããå ´åã¯ãã¼ã¯ã¼ãå¼æ°ã§ä¸èº«ãæå®ããå½¢(è¾æ¸ã§ã¯ãªã) aaa = 'A' bbb = 'B' print (_('{0} by {1}').format (aaa, bbb)) print (_('{xxx} by {yyy}').format (xxx=aaa, yyy=bbb))
xgettextã§çæãã.potããã¡ã¤ã«ã¯ä¸ã®ããã«ãªãã
[ä»»æ]ãã¡ã¤ã«å: po/gettexttest2.pot
# SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR myname # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: gettexttest2 0.1\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2011-02-22 00:00+0900\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <[email protected]>\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" #. TRANSLATORS: comment string #: ../bin/gettexttest2.py:21 msgid "message" msgstr "" #: ../bin/gettexttest2.py:25 msgid "string: {0}" msgstr "" #: ../bin/gettexttest2.py:31 ../bin/gettexttest2.py:33 #: ../bin/gettexttest2.py:35 msgid "{0:d} item" msgid_plural "{0:d} items" msgstr[0] "" msgstr[1] "" #: ../bin/gettexttest2.py:43 msgid "{0} by {1}" msgstr "" #: ../bin/gettexttest2.py:44 msgid "{xxx} by {yyy}" msgstr ""
ä¸ã¯ä¸ã®.potãã¡ã¤ã«ããã¨ã«ç¿»è¨³ãããã¡ã¤ã«ã®ä¾ã
[ä»»æ]ãã¡ã¤ã«å: po/ja.po ã¨ã³ã³ã¼ãã£ã³ã°: UTF-8
# Japanese translations for gettexttest2 package. # Copyright (C) 2011 myname # This file is distributed under the same license as the gettexttest2 package. # <[email protected]>, 2011. # msgid "" msgstr "" "Project-Id-Version: gettexttest2 0.1\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2011-02-22 00:00+0900\n" "PO-Revision-Date: 2011-02-22 00:00+0900\n" "Last-Translator: <[email protected]>\n" "Language-Team: Japanese\n" "Language: ja\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=1; plural=0;\n" #. TRANSLATORS: comment string #: ../bin/gettexttest2.py:21 msgid "message" msgstr "ã¡ãã»ã¼ã¸" #: ../bin/gettexttest2.py:25 msgid "string: {0}" msgstr "æåå: {0}" #: ../bin/gettexttest2.py:31 ../bin/gettexttest2.py:33 #: ../bin/gettexttest2.py:35 msgid "{0:d} item" msgid_plural "{0:d} items" msgstr[0] "{0:d} åã®é ç®" #: ../bin/gettexttest2.py:43 msgid "{0} by {1}" msgstr "{1} ã«ãã {0}" #: ../bin/gettexttest2.py:44 msgid "{xxx} by {yyy}" msgstr "{yyy} ã«ãã {xxx}"
ä¸ã¯å®è¡ä¾ããxgettext()ãã¨ãã¦å¼ã³åºããé¨å(ãåã®é ç®ããå«ãè¡)ãæ¥æ¬èªã«ãªã£ã¦ãããã¨ãåããã
ã¡ãã»ã¼ã¸ æåå: test 1 åã®é ç® 2 åã®é ç® 0 åã®é ç® B ã«ãã A B ã«ãã A
é¢é£è¨äº:
- Pythonでgettextを使用してNLS(Native Language Support)によるメッセージの国際化を行う(作業の流れと動作確認・前半)
- Pythonでgettextを使用してNLS(Native Language Support)によるメッセージの国際化を行う(作業の流れと動作確認・後半)
- Pythonでgettextを使用してNLS(Native Language Support)によるメッセージの国際化を行う(Pythonのコードについて)
- Pythonのimport文について
- バージョン2.6/3.0系以上のPythonにおける新しい文字列のフォーマット付けについて(前半)
- バージョン2.6/3.0系以上のPythonにおける新しい文字列のフォーマット付けについて(後半)
- Pythonでgettextによるメッセージ国際化を用いる上でのgettext.install()に関する追加メモ(前半)
使ç¨ãããã¼ã¸ã§ã³:
- Python 2.6.6, 3.1.2