Software Index
Linux Software Utilities  

po2c

download download home home   report broken
important software information
company name:
Angel Ortega
license: Freeware
minimum requirements: · Perl
functional limitations:
po2c description
po2c is a small Perl script that takes a bunch of gettext (.po) language translation files from its command line arguments and dumps a C source code file containing all those messages and the necessary functions to use them without any other dependency.

This is useful when you want to deploy .po translated strings to systems where no easy gettext support exist (for example, win32) or when a unique, self-contained binary without external file dependencies is convenient (for example, embedded or small systems).

The resulting C source code file include the original and translated messages as a group of static, NULL terminated pointer-to-string arrays and the following two C functions:

void po2c_setlang(char * lang);
char * po2c_gettext(char * msgid);

The first one, po2c_setlang(), is used to set the user favourite language, probably got from him or read from any of the LANG, LC_ALL or LC_MESSAGES environment variables. the processed .po files' names must match those language strings.

The second one, po2c_gettext(), is exactly orthogonal to the original gettext() function; the msgid is the character string to translate. It returns the translated string if found, or the same msgid otherwise.

EXAMPLES

Suppose you have a directory named po and the following two files,
po/es.po:

#spanish translations
msgid "Good morning!"
msgstr "?Buenos d?as!"

and po/fr.po:

#french translations
msgid "Good morning!"
msgstr "Bon jour!"

Then running the following command:

po2c po/*.po > lang.c

Will result in the following lang.c C source file:

/* generated by po2c 1.0.2 - Do not modify */

#include
#include

static char * _po2c_msgids[] = {
/* 0 */ "Good morning!",
NULL
};

struct _po2c_msg {
int msgid;
char * msgstr;
};

static struct _po2c_msg _po2c_lang_fr[] = {
{ 0, "Bon jour!" },
{ -1, NULL }
};

static struct _po2c_msg _po2c_lang_es[] = {
{ 0, "?Buenos d?as!" },
{ -1, NULL }
};

static struct {
char * lang;
struct _po2c_msg * msgs;
} _po2c_langs[] = {
{ "fr", _po2c_lang_fr },
{ "es", _po2c_lang_es },
{ NULL, NULL }
};

/* code */

static struct _po2c_msg * _po2c_lang=NULL;
static int _po2c_lang_size=0;

void po2c_setlang(char * lang)
{
int n;

_po2c_lang=NULL;
_po2c_lang_size=0;

/* if lang is NULL or "", deactivate it */
if(lang == NULL || *lang == '?')
return;

/* searches for a valid language array */
for(n=0;_po2c_lang == NULL && _po2c_langs[n].lang != NULL;n++)
{
if(strcmp(lang, _po2c_langs[n].lang) == 0)
_po2c_lang=_po2c_langs[n].msgs;
}

/* try partial searches */
for(n=0;_po2c_lang == NULL && _po2c_langs[n].lang != NULL;n++)
{
if(strncmp(lang, _po2c_langs[n].lang, 2) == 0)
_po2c_lang=_po2c_langs[n].msgs;
}

/* if found, count entries */
if(_po2c_lang != NULL)
{
struct _po2c_msg * m;

for(m=_po2c_lang;m->msgid != -1;m++)
_po2c_lang_size++;
}
}

char * po2c_gettext(char * msgid)
{
struct _po2c_msg * m;
int b, t, n, c;

/* if no language is set or msgid is empty, return msgid as is */
if(_po2c_lang == NULL || *msgid == '?')
return(msgid);

/* binary-search for the msgid */
b=0; t=_po2c_lang_size - 1;

while(t >= b)
{
n=(b + t) / 2;
m=&_po2c_lang[n];

c=strcmp(msgid, _po2c_msgids[m->msgid]);

if(c == 0)
return(m->msgstr);
else
if(c < 0)
t=n - 1;
else
b=n + 1;
}

return(msgid);
}

You can compile and link it against your code. There, you'll add the function prototypes and use them.. . Angel Ortega is a programmer, writer, composer and illustrator based in Madrid, Spain. Author of the award-winning Minimum Profit text editor for programmers, main musician of the Ann Hell band and many other things.
Similar software
process-getopt (Popularity: ) : The process_getopt project is a wrapper around getopt for bash script developers. It saves you time, reduces the chance for errors and increases the chance that doco is actually written. It is the bash equivalent of RedHat's popt and GNU's ...
phoNIX (Popularity: ) : phoNIX is a software that helps young children learn to read English by using the 'Phonics' system, commonly taught in English Primary schools.

Phonics is a widely used method of teaching children to read and decode words, teaching children to connect ...

User reviews

Write a review:
1 2 3 4 5 6 7 8 9 10
1=poor 10=excellent
Write review*
Your name*
Email*
  (Comments are moderated, and will not appear on this site until the editor has approved them)
 
Similar scripts
po2c (Popularity: ) : po2c is a Perl script that takes a bunch of gettext (.po) language translation files from its command line arguments and dumps a C source code file containing all those messages and the necessary functions to use them without any ...
Rate me
supported os's
stats
downloads 7
version 1.0.2
size in Kb 10
popularity   
863/1272475
user rating 0/10
our rating 0 Stars
share info
Recommend po2c
Report spyware
New Software
Popular Software
Latest Reviews