Pythonã§gettextã«ããã¡ãã»ã¼ã¸å½éåãç¨ããä¸ã§ã®gettext.install()ã«é¢ãã追å ã¡ã¢(åå)
以åãPythonでgettextを使用してNLS(Native Language Support)によるメッセージの国際化を行う(Pythonのコードについて)ãã§ããgettext.gettext()ãç¨ããã¨ngettext()ãç¨ããç®æãæ¥æ¬èªã«ãªããªããã¨æ¸ãã¦ããããããã«é¢ãã追å ã¡ã¢ãããã§æ±ãã
gettext.install()ã«ã¤ãã¦
gettext.install()ã¯
- 翻訳ãã¡ã¤ã³(.moãã¡ã¤ã«ã®ãã¡ã¤ã«åããã.moããé¤ããé¨å)
- ãã±ã¼ã«ãã£ã¬ã¯ããª(ä¸ã«LC_MESSAGESãã£ã¬ã¯ããªãå«ããã±ã¼ã«åãã£ã¬ã¯ããªç¾¤ãå«ãã ãã£ã¬ã¯ããªã»/usr/share/locale/ãªã©)
ãå¼æ°ã«æå®ãããã¨ã§ãã®ãã±ã¼ã«ãã£ã¬ã¯ããªå
ã®ç¿»è¨³ãã¡ã¤ã³ã«åã£ã.moãã¡ã¤ã«ããè±èªã¡ãã»ã¼ã¸ã«åã£ãåèªç¶è¨èªã®ããã¹ãã_()é¢æ°ã§åå¾ã§ããç¶æ
ã«ããã
ãã®é¢æ°ãç¨ããå ´åã¯gettext.bindtextdomain()ãgettext.textdomain()ã¯å¿
è¦ãªãã
çµã¿è¾¼ã¿åå空éã¸ã®_()ãngettext()ã®çµã¿è¾¼ã¿
ã_()é¢æ°ã§åå¾ã§ããç¶æ
ã«ãããã¨ããã®ãgettext.install()ã®ç¹å¾´ã§ãgettext.gettext()ããgettext.gettext()ãã®ããã«ã¢ã¸ã¥ã¼ã«åä»ãã®è¡¨è¨ã§å¼ã³åºããããfrom gettext import gettext as _ãã®importæãå®è¡ããããããã¨ãªããã.ããå«ã¾ãªã表è¨(çµã¿è¾¼ã¿åå空éã»ç¾å¨ã®åå空é)ã®ã_()ãã§è¨è¿°ã§ããããã«ãªãã
ããã¯æ¨æºã§ã¯gettext.gettext()ã«ç¸å½ãã_()ã«ã®ã¿é©ç¨ãããããgettext.install()ã®å¼æ°namesã®ã·ã¼ã±ã³ã¹å(ã¿ãã«ããªã¹ã)ã«æååãngettextããå«ããããã«ããã¨ãåèªã®åæ°å½¢/è¤æ°å½¢ãå¦çã§ããngettext()ã«ã¤ãã¦ãã.ããå«ã¾ãªã表è¨(çµã¿è¾¼ã¿åå空é)ã®ãngettext()ãã§è¨è¿°ã§ããããã«ãªãã
æ£ããå¼ã³åºãæ¹
ãngettext()ãç¨ããç®æãæ¥æ¬èªã«ãªããªããã¨æ¸ãã¦ããã®ã¯
- gettext.install()ã®nameså¼æ°ãä»ãã¦ããªã
- ãgettext.ngettext()ãã¨ãã¦å¼ã³åºãã¦ãã
ã®2ã¤ãåå ã§
- gettext.install()ã®nameså¼æ°ã«æååãngettextããå«ãã
- ngettext()ã¯(çµã¿è¾¼ã¿åå空éã®)ãngettext()ãã¨ãã¦å¼ã³åºã
ã¨ãããã¨ã§ãæ£ããæ¥æ¬èªãªã©ã®ã¡ãã»ã¼ã¸ã«ç½®ãæããããã«ãªã£ãã
(ãPythonでgettextによるメッセージ国際化を用いる上でのgettext.install()に関する追加メモ(後半)ãã«ç¶ã)
é¢é£è¨äº: