« 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 | 7 Comments »

July 3rd, 2026 at 21:22
… [Trackback]
[…] Read More on to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
July 5th, 2026 at 11:36
Ultimamente mi è capitato di vedere che questo mondo del betting stia diventando estremamente ampio.
Per esperienza diretta, penso che fondamentale non farsi
trasportare dalla frenesia del piazzare puntate subito,
dato che una corretta gestione del bankroll è sempre la base
fondamentale di giocare serenamente. Parecchi utenti
spesso dimenticano di controllare tutte le licenze ancora del
iscriversi, d’altra parte c’è chi scelgono puntare solo
su piattaforme che offrono incentivi più iscrizione.
Risulta curioso osservare inoltre come le moderne tecnologie stiano cambiando il feeling degli utenti, rendendo ogni gioco più fluido ma anche coinvolgente.
Qualora cercate approfondire alcuni aspetti, potete consultare kin bet it sign in per avere qualche visione ben completa sulle
opzioni disponibili. Voi avete provato differenze particolare simile ultimi test gioco?
Quale strategia seguite di per gestire le giocate?
Sarei interessato a sapere i vostri punti di vista.
July 13th, 2026 at 02:08
… [Trackback]
[…] There you can find 50713 additional Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
July 15th, 2026 at 21:44
… [Trackback]
[…] Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
July 16th, 2026 at 17:01
… [Trackback]
[…] Information on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
July 23rd, 2026 at 22:44
… [Trackback]
[…] Read More to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]
August 1st, 2026 at 20:05
… [Trackback]
[…] Info to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]