« How to take out the system built-in beeper in Linux | Home | Free and open source XML beautifers to format XML »
Simple C++ gettext-like toolkit
By admin | December 20, 2009
Here is a small gettext-compatible interface for reading simple translation catalogs (not to be confused with gettext’s *.mo style catalogs, which are binary) in form of “original/english string<tab>translated string”. It is not fool-proof, but you can play around with it and it works.
/* * gettextpd.h * Main file for GettextPD * * © Copyright 2009 Compdigitec. All rights reserved. * * Copying and distribution of this file, with or without modification, * are permitted in any medium without royalty provided the copyright * notice and this notice are preserved. This file is offered as-is, * without any warranty. */ #ifndef __GETTEXTPD__ #define __GETTEXTPD__ #include <stdlib.h> #include <cstring> #include <string> #include <iostream> #include <fstream> // gettext functions char* gettext (char *msgid); std::string gettext (std::string msgid); char* textdomain (const char *domain_name); char* bindtextdomain (const char *domain_name, const char *dir_name); char* setlocale (int category, const char* locale); // macros #define _(str) gettext(str) // globals std::string gtpd_locale = ""; std::string gtpd_domain = ""; std::string gtpd_location = ""; // function bodies char* gettext(char* msgid) { std::string x(msgid); std::string result; result = gettext(x); char* nb = (char*)malloc(sizeof(char)*result.length()); strcpy(nb,result.c_str()); return nb; } std::string gettext(std::string msgid) { std::string path = gtpd_location + "/" + gtpd_locale + "/LC_MESSAGES/" + gtpd_domain + ".mo"; std::ifstream stream; stream.open(path.c_str()); std::string resultline; while(!stream.eof()) { getline(stream,resultline); if(strstr(resultline.c_str(),msgid.c_str()) != NULL) { break; } resultline = ""; } if(resultline == "") { // not found return msgid; } int splitloc = resultline.find("\t"); return resultline.substr(splitloc+1); } char* textdomain(const char *domain_name) { gtpd_domain = domain_name; char* res = (char*)malloc(sizeof(char)*100); strcpy(res,domain_name); return res; } char* bindtextdomain (const char *domain_name, const char *dir_name) { gtpd_location = dir_name; textdomain(domain_name); char* res = (char*)malloc(sizeof(char)*100); strcpy(res,dir_name); return res; } char* setlocale (int category, const char* locale) { gtpd_locale = locale; char* res = (char*)malloc(sizeof(char)*100); strcpy(res,locale); return res; } #endif
Example usage:
// test.cpp #include "gettextpd.h" #include <stdio.h> #include <stdlib.h> int main(void) { setlocale( LC_ALL, "fr_FR" ); bindtextdomain( "hello", "." ); textdomain( "hello" ); std::cout << gettext("Hello, world!") << "\n"; std::cout << _("THis is A TeST StriNg") << "\n"; exit(0); }
folder/fr_FR/LC_MESSAGES/hello.mo
Hello, world! Zu bu zu translated str THis is A TeST StriNg test string translated this is#2
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!
Topics: Linux | 18 Comments »
March 26th, 2022 at 14:05
… [Trackback]
[…] There you can find 55527 additional Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
March 28th, 2022 at 07:32
… [Trackback]
[…] Read More Info here on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
April 25th, 2022 at 19:34
… [Trackback]
[…] Find More on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
May 2nd, 2022 at 05:06
… [Trackback]
[…] Find More Info here to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
May 30th, 2022 at 18:34
… [Trackback]
[…] Find More Information here on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
June 26th, 2022 at 17:11
… [Trackback]
[…] There you will find 76663 more Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
June 29th, 2022 at 21:05
… [Trackback]
[…] Here you will find 52095 more Info on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
July 12th, 2022 at 18:43
… [Trackback]
[…] Find More on to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
August 21st, 2022 at 10:38
… [Trackback]
[…] Info to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
August 31st, 2022 at 12:30
… [Trackback]
[…] Read More on to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
September 18th, 2022 at 10:35
… [Trackback]
[…] Read More Information here on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
September 27th, 2022 at 19:27
… [Trackback]
[…] Info to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
November 25th, 2022 at 19:45
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
December 2nd, 2022 at 19:12
… [Trackback]
[…] Read More on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
January 6th, 2023 at 19:47
… [Trackback]
[…] Info to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
January 24th, 2023 at 04:26
… [Trackback]
[…] Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
January 28th, 2023 at 06:54
… [Trackback]
[…] Info to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
January 28th, 2023 at 13:38
… [Trackback]
[…] Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]