PHPã§Gettextæ¡å¼µã使ã£ã¦ã¿ã
PHPã®Gettextæ¡å¼µã¯ãPHPã«æ¨æºçã«ã¤ãã¦ãã¾ãã
èªåã§ãã«ããããããå ´åãããã©ã«ãã§æ©è½ãæå¹ã«ãªã£ã¦ãªãã£ãããããããããªãã®ã§æ³¨æã§ãã
ãªã®ã§ãPHPã§è¤æ°è¨èªå¯¾å¿ããã«ã¯ãgettextã使ãã®ãä¸çªç°¡åã§ãã
# ããä¿¡ãã¦ãã¾ããã
åè:
ã¦ãã¦ã©ã Unoh Labs: 5åã§ããã PHP ã§æ¸ããã Web ãµã¼ãã¹ã®å½éå(ãã®2) http://labs.unoh.net/2006/06/5_php_web_2.html
â»ãã»ã¨ãã©ãã¾ãã¾ã§ããã
ãã¦ãGettextã®ä½¿ãæ¹ã§ãã
åæã¨ãã¦ãphpãåä½ããããã«Webãµã¼ã(apache)ãè¨å®æ¸ã¿ã§ããã¤ãphpã®gettextãæå¹ã«ãªã£ã¦ãããã¨ãå¿
è¦ã§ãã
ã¾ããgettext ãã¤ã³ã¹ãã¼ã«ããã¦ããå¿
è¦ãããã¾ãã
ã¾ããã¹ã¯ãªããããã
ãã¡ã¤ã«ã®ã¨ã³ã³ã¼ãã¯UTF-8ã«ãã¾ãã
å½ç¶ã§ãããEUC-JPãSJISã ã¨ãä»ã®è¨èªã®æã«æååããããã¨ã«ãªãã¾ãã
âåç´ãªãµã³ãã«ã§ã
<?php $lang = "ja_JP.UTF-8"; $domain = "messages"; setlocale(LC_ALL, $lang); bindtextdomain($domain, "./locale/"); textdomain($domain); echo _("hello world"); ?>
ãã®ãã¡ã¤ã«ããtest.php ã¨ããååã§ä¿åãã¾ãã
次ã«ã翻訳ããã¹ãæååã xgettext ã§æ½åºãã¾ãã
æ½åºå¯¾è±¡ã¨ãªãããã°ã©ãã³ã°è¨èªã¯ãæ¡å¼µåã§èªåå¤å¥ãã¦ããã¾ãã
æ¡å¼µåã«ç¹æ®ãªãã®ã使ã£ã¦ããå ´åã¯ãªãã·ã§ã³ã§æå®ãã¦ãã ãããï¼manãåç
§)
$ xgettext test.php
ãã®ã³ãã³ããå®è¡ããã¨ã messages.po ã¨ãããã¡ã¤ã«ãã§ããã¨æãã¾ãã
ãã®ææ½åºãããæååã¯ã _("hello world")ãã¨ããGettextã®é¢æ°å
ã®æååã§ãã
# _() é¢æ°ã¯ gettext() é¢æ°ã®å¥åã§ã
ãã¡ã¤ã«ã®ä¸èº«ã¯ãããªãããã
# 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: 2008-03-28 12:40+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" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=CHARSET\n" "Content-Transfer-Encoding: 8bit\n" #: test.php:7 msgid "hello world" msgstr ""
ãã®ãã¡ã¤ã«ããã¼ã±ã¼ã«(è¨èª)æ¯ã«ç¿»è¨³ãã¦ãããã°ããããã§ãã
ã¾ãã次ã®è¡ã® CHARSET ããutf-8ãeuc-jp ãªã©ãæ¥æ¬èªãæ±ããæåã³ã¼ãã«ãã¾ãã
ä»å㯠utf-8 ã«ãã¾ããã
ãã¡ããããã¡ã¤ã«ã®æåã³ã¼ãã utf-8 ã§ãã
"Content-Type: text/plain; charset=CHARSET\n"
ã§ãæ¬é¡ã®ç¿»è¨³ã«ã¤ãã¦ã§ãã
ã¤ãã®ããã«ãmsgid ã¨ãmsgstr ã対ã«ãªã£ã¦ãã¾ãã
#: test.php:7 msgid "hello world" msgstr ""
msgid ã翻訳åã®æååãmsgstrã翻訳ããæååã§ãã
è¤æ°å¨ãå ´åã¯ãã®å¯¾ãã¡ãã»ã¼ã¸ã®æ°ã ã並ã³ã¾ãã
è¦ããã«ãmsgidã«å¯¾å¿ããããã«ãmsgstrã«ç¿»è¨³å¾ã®æåãæ¸ãã¦ããã ãã§ãã
翻訳ãããã¡ã¤ã«ã¯æ¬¡ã®ãã£ã¬ã¯ããªã«é ç½®ãã¾ãã
/<ãã±ã¼ã«å>/LC_MESSAGES/<æå®ãããã¡ã¤ã³å>.po
æ¥æ¬èªã®ãã±ã¼ã«ã¯ ja_JP ã§ãã è±èªã¯ enã en_US ãªã©ãããã¾ãã
ãã¡ã®ç°å¢ã§ã¯ãjaã§ã¯ãã¡ã§ãããç°å¢ã«ãã£ã¦ãã±ã¼ã«ã®æå®ã¯ç°ãªãã¿ããã§ãã
# ja_US ãããã¡ãªã®ãã¨ããããã³ããããããªãã¾ãããæ¥æ¬èªã«ã¯éããªããã§ãããã
æå¾ã®æ¹ã«ããã±ã¼ã«ã§ããã£ã¦ããã¨ãã°ãã°ãæ¸ãã¦ã¾ãããæ£è§£ãããã§ã
æåã³ã¼ãã UTF-8ã«ãã¦ããã®ã§ã æ¥æ¬èªã®ãã±ã¼ã«ã¯ ja_JP.UTF-8 ã¨ãã¾ãã
ãããç°å¢ä¾åãããããªãã®ã§ï¼ï¼ï¼ï¼
ã§ã¯ãªããã§ããã
翻訳ãããã¡ã¤ã«ãã³ã³ãã¤ã«ãã¦ãgettext ã§ä½¿ããå½¢å¼ã«å¤æãã¾ãã
å®è¡ããã¨ãmessages.mo ã¨ãããã¡ã¤ã«ãã§ãã¾ãã
ãã®ãã¡ã¤ã«ãã翻訳ããpoãã¡ã¤ã«ã¨åããã£ã¬ã¯ããªå
ã«é
ç½®ãã¾ãã
$ msgfmt messages.po
ããã§ç¿»è¨³ã®ä½æ¥ã¯å®äºã§ãã
ä»ã®è¨èªãæ±ãå ´åãåæ§ã«å¦çãã¾ãã
ãã¨ã¯ããã©ã¦ã¶ãããã¹ã¯ãªãããè¦ã«ãã£ã¦ãããã°ããã±ã¼ã«ã«ãã£ãè¨èªã§è¡¨ç¤ºãããã¯ãã§ãã
ã¡ãªã¿ã«ãç¨æãã¦ããªãè¨èªã¯ãã¹ã¯ãªããã«æ¸ãã¦ããæååããã®ã¾ã¾åºåãããããã§ãã
åé¡
ããã§ç¡äºç¿»è¨³æ©æ§å®äºã»ã»ã»ãã¨æãããã
ãã¡ã®éçºç°å¢ï¼Fedoraï¼ã§ã¯ããã ãã¯çµããã¾ããã§ããã
_("hello world")ãã§å¾ãããæåå㯠"ããã«ã¡ãä¸ç"ãã§ãã
ããããæåã³ã¼ãããEUC-JP ã§è¿ã£ã¦ãã¾ãã
ãã©ã¦ã¶ã®ã¨ã³ã³ã¼ããEUC-JPã«å¤ããã¨ã¡ããã¨è¦ãã¾ãã
ãªã®ã§ã
mb_convert_encoding(_("hello world", "UTF-8", "EUC-JP");
ã¨ããã¨ãã¡ããã¨UTF-8ã§è¡¨ç¤ºããã¾ãã
ã§ããããã ã¨ä»ã®è¨èªã§æååãããã®ã§ä½¿ãã¾ããã
ã³ã³ãã¤ã«ããmoãã¡ã¤ã«ãlvã§ç¡çããéãã¨ãæåã³ã¼ãã euc-jp ã«åæã«å¤æããã¦ãã¾ããã
å¥ã®ç°å¢ã®ãFreeBSDä¸ã§ã³ã³ãã¤ã«ããã¨ç¡äºãutf-8ã«ãªãã¾ããã
# ãªããã©ã¤ãã©ãªãããã¦ãªãã£ãã¿ããã§ããgettext-lib ã yumã§ããããç¡äº utf-8ã§ä½ãããããã«ãªãã¾ããã
ããã¾ã æååããã¾ãã
ä»åº¦ã¯ãã©ã¦ã¶ã®ã¨ã³ã³ã¼ãè¨å®ãå¤ãã¦ãæååããã¾ã¾ã§ãã
æåã³ã¼ããå£ãã¦ãããã§ãã
PHPã®gettextã utf-8 ãªã®ã« euc-jp ãªã¤ããã§èªã¿è¾¼ãã§ããã¦ãæ°ããã¾ãã
ããæ¸ããªããããµã¨ãããã¤ããã®ã§ããã±ã¼ã«ãUnixã®LANG ã¿ããã«ãã¦ã¿ã¾ããã
ja_JP.UTF-8 ã«ããã¨ãã£ãããããã¾ããã
ãªãã ããorz