{"id":1211,"date":"2015-12-29T18:31:55","date_gmt":"2015-12-29T17:31:55","guid":{"rendered":"https:\/\/www.hutsky.cz\/blog\/?p=1211"},"modified":"2015-12-29T18:31:55","modified_gmt":"2015-12-29T17:31:55","slug":"gettext-translations","status":"publish","type":"post","link":"https:\/\/www.hutsky.cz\/blog\/2015\/12\/gettext-translations\/","title":{"rendered":"Gettext translations"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-1265\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/120px-Official_gnu.svg_.png\" alt=\"120px-Official_gnu.svg\" width=\"120\" height=\"120\" \/><a href=\"https:\/\/www.gnu.org\/software\/gettext\/\">Gettext<\/a> is a great way to localize and internationalize (simply put, to translate) your (web)applications. It&#8217;s especially useful when you&#8217;re building a multi-language application because you can separate the code from the translation process effectively.<\/p>\n<ol>\n<li>\n<div>\n<p>all strings that need to get translated have to be wrapped in <a href=\"http:\/\/php.net\/manual\/en\/function.gettext.php\">_()<\/a> function in the source code files:<\/p>\n<pre class=\"\">echo _('text to be translated');<\/pre>\n<\/div>\n<\/li>\n<li>\n<div>\n<div>\n<p>cd to the project directory and run in bash (this will go through all the php files in the current working directory):<\/p>\n<pre class=\"\">xgettext --from-code=UTF-8 -d projectname *.php<\/pre>\n<\/div>\n<\/div>\n<\/li>\n<li>\n<div>\n<div>\n<p>to search for all php files in the project directory recursively, try this:<\/p>\n<pre class=\"\">find . -iname \"*.php\" | xargs xgettext --from-code=UTF-8 -d projectname<\/pre>\n<\/div>\n<div>\n<p>If you need to omit a particular directory, use inverted grep like this:<\/p>\n<pre class=\"\"> find . -iname \"*.php\" | grep -v \"\/omitted_directory\/\" | xargs xgettext --from-code=UTF-8 -d projectname<\/pre>\n<\/div>\n<\/div>\n<\/li>\n<li>\n<div>The above mentioned commands will output a file named projectname.po, which can be translated using <a href=\"http:\/\/poedit.net\/\">Poedit<\/a> or <a href=\"http:\/\/pootle.translatehouse.org\/\">Pootle<\/a>. Poedit will generate binary file projectname.mo on save.<\/div>\n<\/li>\n<li>\n<div>\n<p>To use this binary file, create another directory in your project folder (I&#8217;m using cz_CS <a href=\"https:\/\/en.wikipedia.org\/wiki\/Locale#POSIX_platforms\">locale identifier<\/a>)<\/p>\n<pre class=\"\">.\/translation\/cs_CZ\/LC_MESSAGES<\/pre>\n<p>and copy the .mo file there.<\/p>\n<\/div>\n<\/li>\n<li>\n<div>\n<p>In your application, use the following:<\/p>\n<pre class=\"\">$languange = 'cs_CZ';\r\nputenv(\"LANG=$language\");\r\nsetlocale(LC_ALL, $language); \r\nbindtextdomain(\"projectname\", \".\/translation\/\"); \r\ntextdomain(\"projectname\");\r\nbind_textdomain_codeset(\"projectname\", 'UTF-8');<\/pre>\n<\/div>\n<\/li>\n<\/ol>\n<p>This will load the .mo file with translated strings and replace all the _() wrapped strings in there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gettext is a great way to localize and internationalize (simply put, to translate) your (web)applications. It&#8217;s especially useful when you&#8217;re building a multi-language application because you can separate the code from the translation process effectively. all strings that need to &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.hutsky.cz\/blog\/2015\/12\/gettext-translations\/\"> <span class=\"screen-reader-text\">Gettext translations<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,22],"tags":[4,41,42],"class_list":["post-1211","post","type-post","status-publish","format-standard","hentry","category-programming","category-web-related","tag-php","tag-shell","tag-web"],"_links":{"self":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":14,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":1270,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions\/1270"}],"wp:attachment":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}