{"id":147,"date":"2009-12-20T19:04:14","date_gmt":"2009-12-21T00:04:14","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=147"},"modified":"2009-12-20T19:07:48","modified_gmt":"2009-12-21T00:07:48","slug":"simple-c-gettext-like-toolkit","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2009\/12\/20\/simple-c-gettext-like-toolkit\/","title":{"rendered":"Simple C++ gettext-like toolkit"},"content":{"rendered":"<p>Here is a small gettext-compatible interface for reading simple translation catalogs (not to be confused with gettext&#8217;s *.mo style catalogs, which are binary) in form of &#8220;original\/english string&lt;tab&gt;translated string&#8221;. It is not fool-proof, but you can play around with it and it works.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/*\r\n *      gettextpd.h\r\n * \t\tMain file for GettextPD\r\n *\r\n *      \u00a9 Copyright 2009 Compdigitec. All rights reserved.\r\n *\r\n *      Copying and distribution of this file, with or without modification,\r\n * \t\tare permitted in any medium without royalty provided the copyright\r\n * \t\tnotice and this notice are preserved.  This file is offered as-is,\r\n * \t\twithout any warranty.\r\n *\/\r\n#ifndef __GETTEXTPD__\r\n#define __GETTEXTPD__\r\n\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;string&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;fstream&gt;\r\n\r\n\/\/ gettext functions\r\nchar* gettext (char *msgid);\r\nstd::string gettext (std::string msgid);\r\nchar* textdomain (const char *domain_name);\r\nchar* bindtextdomain (const char *domain_name, const char *dir_name);\r\nchar* setlocale (int category, const char* locale);\r\n\r\n\/\/ macros\r\n#define _(str) gettext(str)\r\n\r\n\/\/ globals\r\nstd::string gtpd_locale = &quot;&quot;;\r\nstd::string gtpd_domain = &quot;&quot;;\r\nstd::string gtpd_location = &quot;&quot;;\r\n\r\n\/\/ function bodies\r\nchar* gettext(char* msgid)\r\n{\r\n\tstd::string x(msgid);\r\n\tstd::string result;\r\n\tresult = gettext(x);\r\n\tchar* nb = (char*)malloc(sizeof(char)*result.length());\r\n\tstrcpy(nb,result.c_str());\r\n\treturn nb;\r\n}\r\n\r\nstd::string gettext(std::string msgid)\r\n{\r\n\tstd::string path = gtpd_location + &quot;\/&quot; + gtpd_locale + &quot;\/LC_MESSAGES\/&quot; + gtpd_domain + &quot;.mo&quot;;\r\n\tstd::ifstream stream;\r\n\tstream.open(path.c_str());\r\n\tstd::string resultline;\r\n\twhile(!stream.eof()) {\r\n\t\tgetline(stream,resultline);\r\n\t\tif(strstr(resultline.c_str(),msgid.c_str()) != NULL) {\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tresultline = &quot;&quot;;\r\n\t}\r\n\tif(resultline == &quot;&quot;) {\r\n\t\t\/\/ not found\r\n\t\treturn msgid;\r\n\t}\r\n\tint splitloc = resultline.find(&quot;\\t&quot;);\r\n\treturn resultline.substr(splitloc+1);\r\n}\r\n\r\nchar* textdomain(const char *domain_name)\r\n{\r\n\tgtpd_domain = domain_name;\r\n\tchar* res = (char*)malloc(sizeof(char)*100);\r\n\tstrcpy(res,domain_name);\r\n\treturn res;\r\n}\r\n\r\nchar* bindtextdomain (const char *domain_name, const char *dir_name)\r\n{\r\n\tgtpd_location = dir_name;\r\n\ttextdomain(domain_name);\r\n\tchar* res = (char*)malloc(sizeof(char)*100);\r\n\tstrcpy(res,dir_name);\r\n\treturn res;\r\n}\r\n\r\nchar* setlocale (int category, const char* locale)\r\n{\r\n\tgtpd_locale = locale;\r\n\tchar* res = (char*)malloc(sizeof(char)*100);\r\n\tstrcpy(res,locale);\r\n\treturn res;\r\n}\r\n\r\n#endif\r\n<\/pre>\n<p>Example usage:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ test.cpp\r\n#include &quot;gettextpd.h&quot;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\nint main(void)\r\n{\r\n  setlocale( LC_ALL, &quot;fr_FR&quot; );\r\nbindtextdomain( &quot;hello&quot;, &quot;.&quot; );\r\ntextdomain( &quot;hello&quot; );\r\nstd::cout &lt;&lt; gettext(&quot;Hello, world!&quot;) &lt;&lt; &quot;\\n&quot;;\r\nstd::cout &lt;&lt; _(&quot;THis is A TeST StriNg&quot;) &lt;&lt; &quot;\\n&quot;;\r\nexit(0);\r\n}\r\n<\/pre>\n<p>folder\/fr_FR\/LC_MESSAGES\/hello.mo<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nHello, world!\tZu bu zu translated str\r\nTHis is A TeST StriNg\ttest string translated this is#2\r\n<\/pre>\n<p>If you found this article helpful or interesting, please help Compdigitec spread the word. Don\u2019t forget to <a title=\"Subscribe to Compdigitec Labs\" href=\"\/labs\/feed\/\">subscribe to Compdigitec Labs<\/a> for more interesting articles!<\/p>","protected":false},"excerpt":{"rendered":"<p>Here is a small gettext-compatible interface for reading simple translation catalogs (not to be confused with gettext&#8217;s *.mo style catalogs, which are binary) in form of &#8220;original\/english string&lt;tab&gt;translated string&#8221;. It is not fool-proof, but you can play around with it and it works. Example usage: folder\/fr_FR\/LC_MESSAGES\/hello.mo If you found this article helpful or interesting, please [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[289,621,614,615,619,616,617,622,623,618,620],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/147"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}