Compdigitec Labs

« | Home | »

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 »

7 Responses to “Simple C++ gettext-like toolkit”

  1. สล็อตเว็บใหญ่ PG Says:
    November 8th, 2024 at 03:18

    … [Trackback]

    […] Read More here to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

  2. รับซื้อแบรนด์เนม Says:
    November 9th, 2024 at 04:11

    … [Trackback]

    […] Read More Info here on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

  3. tokens cam 30 Says:
    November 11th, 2024 at 22:42

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

  4. ช่างกุญแจเพชรบุรี Says:
    November 12th, 2024 at 20:04

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

  5. รวมเกมไพ่ออนไลน์ทุกรูปแบบ ทุนน้อยก็เล่นได้ Says:
    November 18th, 2024 at 03:27

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

  6. กายภาพ Says:
    November 18th, 2024 at 22:14

    … [Trackback]

    […] Find More here on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

  7. this content Says:
    December 1st, 2024 at 10:47

    … [Trackback]

    […] Find More on that Topic: compdigitec.com/labs/2009/12/20/simple-c-gettext-like-toolkit/ […]

Comments