{"id":238,"date":"2009-07-22T23:29:00","date_gmt":"2009-07-22T23:29:00","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=238"},"modified":"2009-07-22T23:29:00","modified_gmt":"2009-07-22T23:29:00","slug":"gotchas-with-gettext-in-php","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=238","title":{"rendered":"Gotchas with gettext in PHP"},"content":{"rendered":"<p>Be careful when you first try gettext in PHP, it can be very unreliable\/unpredictable because of different naming conventions.<\/p>\n<p><textarea name=\"code\" class=\"php\"><br \/>\n$vLang = &#8216;vi_VN&#8217;;<br \/>\n$vDomain = &#8216;MyApp&#8217;;<br \/>\nputenv(&#8220;LANGUAGE=&#8221;.$vLang);<br \/>\nputenv(&#8220;LANG=&#8221;.$vLang);<br \/>\nbindtextdomain($vDomain,AS_BASEDIR.&#8217;locale&#8217;);<br \/>\nbind_textdomain_codeset($vDomain,&#8221;UTF-8&#8243;);<br \/>\nsetlocale(LC_ALL,$vLang.&#8221;.utf8&#8243;,$vLang.&#8221;.UTF8&#8243;,$vLang.&#8221;.utf-8&#8243;,$vLang.&#8221;.UTF-8&#8243;,$vLang);<br \/>\ntextdomain($vDomain);<br \/>\n<\/textarea><\/p>\n<h4>MyApp.po vs MyApp.mo<\/h4>\n<p>PO is the human-readable and you perform the translation in here. MO is the binary file and it&#8217;s cached by Apache. Thus, you need to restart Apache (XAMPP) after each change to MyApp.po, then recompile it via &#8220;msgfmt MyApp.po -o MyApp.mo&#8221;. If you forgot to recompile, it won&#8217;t have any of the new modifications.<\/p>\n<h4>Folder Structure<\/h4>\n<pre>\n\/MyApp\/locale\/vi_VN\/LC_MESSAGES\/MyApp.mo\n\/MyApp\/locale\/da_DK\/LC_MESSAGES\/MyApp.mo\n\/MyApp\/locale\/en_US\/LC_MESSAGES\/MyApp.mo\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Be careful when you first try gettext in PHP, it can be very unreliable\/unpredictable because of different naming conventions. $vLang = &#8216;vi_VN&#8217;; $vDomain = &#8216;MyApp&#8217;; putenv(&#8220;LANGUAGE=&#8221;.$vLang); putenv(&#8220;LANG=&#8221;.$vLang); bindtextdomain($vDomain,AS_BASEDIR.&#8217;locale&#8217;); bind_textdomain_codeset($vDomain,&#8221;UTF-8&#8243;); setlocale(LC_ALL,$vLang.&#8221;.utf8&#8243;,$vLang.&#8221;.UTF8&#8243;,$vLang.&#8221;.utf-8&#8243;,$vLang.&#8221;.UTF-8&#8243;,$vLang); textdomain($vDomain); MyApp.po vs MyApp.mo PO is the human-readable and you perform the translation in here. MO is the binary file and it&#8217;s cached by Apache. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/238"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":0,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}