Skip to content
Search

Gettext

lastmod 2025-02-17created 2014-06-1225 edits

In computing, gettext is an internationalization and localization (i18n) system commonly used for writing multilingual programs on Unix-like computer operating systems. The most widely used implementation of gettext is GNU gettext, released by the GNU Project in 1995.

Int the MaxOSX

On MacOSX, install it using brew.

$ brew install gettext

Then add the gettext/bin path to PATH as shown below.

export PATH=${PATH}:/usr/local/opt/gettext/bin

How to recursive build

$ find . -iname *.po | sed -e 's;\(.*\)\.po$;\1\.po:-o:\1.mo;g' | awk -F':' '{print($1);print($2);print($3);}' | xargs -n 3 msgfmt

Dictionary directory structure

The structure of the dictionary is as follows.

 +---- locale ----+--- en ---+--- LC_MESSAGES 
                  |
                  +--- kr ---+--- LC_MESSAGES ---+--- messages.mo
                                                 |
                                                 +--- messages.po

Using gettext in shell scripts

#! /bin/sh
# Hello

TEXTDOMAINDIR=/usr/local/share/locale
TEXTDOMAIN=Hello
export TEXTDOMAINDIR
export TEXTDOMAIN

echo "$(gettext -s "Hello")"
echo "$(gettext -s "World!")"

Using gettext in C code

#include <libintl.h>
#include <locale.h>
#include <stdio.h>

int
main(void)
{
    /* 현재 호스트의 locale 을 사용한다. */
    setlocale(LC_ALL,"");

    /* Hello 의 message table 을 /usr/local/share/locale 아래에서 찾도록 한다. */
    bindtextdomain("Hello", "/usr/local/share/locale");
    textdomain("Hello");

    printf("original message : %s\n", "Greeting");
    printf("gettext trans  : %s\n", gettext("Greeting"));

    printf("original message : %s\n", "Hello");
    printf("gettext trans  : %s\n", gettext("Hello"));

    printf("original message : %s\n", "World!");
    printf("gettext trans  : %s\n", gettext("World!"));

    return 0;
}

See also

Favorite site

author Zer0 sitemap