Next Previous Contents

3. NLS sthn prajh

3.1 Paradeigma xrhshs toy NLS

Koitajte sto katalogo

RedHat-6.0: /usr/share/locale (pros ejakribwsh)
RedHat-5.x: /usr/share/locale
RedHat-4.x: /usr/share/locale (pros ejakribwsh)
Open Linux 2.2: /usr/share/locale (pros ejakribwsh)
SuSe 6.x: /usr/share/locale (pros ejakribwsh)
Slackware 4.0: /usr/share/locale (pros ejakribwsh)
Debian 2.x: /usr/share/locale (pros ejakribwsh)

sto susthma sas gia diagrammata poy aforoun tis yposthrizomenes xwres. 8a breite sigoyra gallika, ispanika, germanika, ollandika klp.

Proxwrhste sto ypokatalogo es kai meta LC_MESSAGES, dhladh /usr/share/es/LC_MESSAGES/ kai estw oti briskete to "fileutils.mo". Tote, exete yposthrijh gia ispanika sto ls, mkdir ktl. (An exete ypokatalogo el gia ellhnika kai to antistoixo arxeio sto susthma sas, mporeite na xrhsimopoihsete to lektiko ayto anti toy es).

Trejte

Twra trejte "ls --help" kai 8a deite ispanika mhnumata!

3.2 To dokimasa alla den doyleuei!

Se ayth thn kathgoria mporei na briskontai oi xrhstes me palia ekdosh toy Linux.

Sto susthma sas mporei na yparxoyn perissoteroi katalogoi me plhrofories gia metafraseis programmatwn (katalogoi locale). To sunolo twn katalogwn poy elegxontai, orizetai me th metablhth periballontos NLSPATH. Panta yparxoyn kapoioi katalogoi poy to susthma ereyna ej orismou, oi opoioi exoyn proka8oristei sth basikh biblio8hkh toy systhmatos sas, th libc. Dokimaste "strace -o output.log ls --help" kai meta "grep open output.log" gia na deite poy caxnei to susthma. Meta ka8oriste analoga to NLSPATH.

3.3 Me poio(poioys) tropoys ka8orizetai h glwssa me thn opoia emfanizontai ta mhnumata enos systhmatos;

Mesw metablhtwn periballontos. Gia kapoio logo, yparxei enas ari8mos apo metablhtes poy kanoyn sxedon thn idia doyleia. H metablhth poy fainete na xrhsimopoioun oi perissoteroi xrhstes einai h LANG, me th morfh

Diaforetika, yparxei h metablhth LANGUAGE, h opoia exei proteraiothta enanti ths LANG.

Gia ta systhmata me glibc 2.1 (ayth th stigmh RedHat 6.0), o xrhsths mporei na ektelesei (pros ejakribwsh)

3.4 Metaptwsh glwsswn se periptwsh apoysias metafrasewn

Trejte

Se oles tis periptwseis, yparxei h telikh metaptwsh sthn agglikh.

3.5 Programmatistikes plhrofories

To parakatw yliko proerxetai apo to Niko Mayrogiannopoylo.

Mias kai to nls xrhsimopoieitai olo kai perissotero sta nea programmata (idiaitera ayta poy apoteloun to GNU susthma), einai anagkaio na kanoyme mia perigrafh ths diadikasias poy xreiazetai gia na to enswmatwsoyme kai sta dika mas programmata.

H adeia toy gettext gia ta kommatia poy 8a enswmatwsoyme sto programma mas einai h GNU LGPL poy den mas periorizei ston ka8orismo ths adeias toy programmatos mas. Koitajte thn pantws gia na deite an eiste symbatoi(!).

As prosperasoyme omws ta "typika" kai as perasoyme sto chto. Ta programmata poy 8a xreiastoume einai ta GNU autoconf kai gettext-0.10, kai briskontai sto pio kontino gnu mirror.

Edw 8a prospa8hsoyme na pros8esoyme yposthrijh e8nikhs/topikhs glwssas (nls) se ena programma grammeno se C. As doume ena aplo programma, poy typwnei thn wra sthn o8onh:


#include <stdio.h>
#include <time.h>

main ()
{
        time_t tv;

        printf( "The time is: " );
        time(&tv);
        printf ( "%s",ctime(&tv) );

}

H wra opws kai to mhnyma typwnetai sta agglika. Epeidh h metafrash prepei na ginei genika - dhladh to idio programma na trexei kai sta germanika kai sta ellhnika kai se opoiadhpote allh glwssa - den metafrazoyme ap'ey8eias to keimeno. Xrhsimopoioume ta localedata kai th biblio8hkh intl ths C biblio8hkhs.

As xrhsimopoihsoyme ta localedata (topika dedomena) gia thn wra.

To idio programma twra ginetai:


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

main ()
{
        struct tm *tp;
        char buf[80];
        time_t date;
        
/* 8etei to locale sumfwna me thn metablhth periballontos LANG h LANGUAGE
 * Gia ta ellhnika prepei na einai el (ISO639) */
        setlocale (LC_TIME, ""); /* Gia thn wra kai mono */
                         
        printf( "The time is: " );

        time(&date);

/* Metatrepei thn wra ekfrash poy syndeetai me thn zwnh wras */
        tp = localtime(&date);
        strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %z %Y", tp);

/* ektypwnei thn wra */
        printf ("%s\n",buf);

}

H wra twra 8a typw8ei sthn morfh: Tet Noe 11 22:44:29 +0200 1998 me ellhnikous xarakthres opws blepete(an LANG=el h gr gia tis palies glibc2). Gi'ayto jexaste to ctime kai asxolh8eite me to strftime().

Se periptwsh poy den eidate ellhnikous xarakthres elegjte an sto /usr/share/locale/el yparxoyn ta aparaithta arxeia. An den yparxoyn promh8eyteite ena neo localedata - symperilambanetai synh8ws sthn libc h egkatasthste to tarball poy brisketai sto ftp://argeas.cs-net.gr/pub/unix/linux/GREEK/locale.glibc2.el.tar.gz

Gia thn metafrash twn mhnymatwn, poy einai kai h kyrioterh idiothta toy NLS xrhsimopoieitai o ejhs tropos:


#include <stdio.h>
#include <time.h>
#include <locale.h>
#include <libintl.h> /* arxeio ths GNU libc */

main ()
{
        struct tm *tp;
        char buf[80];
        time_t date;
        
/* pera apo to LC_ALL yparxoyn ta LC_TIME, LC_MESSAGES klp, ta opoia
 * prosdiorizoyn ep'akribws ti metafraseis 8a xrhsimopoihsoyme.
 */
        setlocale (LC_ALL, ""); 

        bindtextdomain ("my_time", "/usr/share/locale");
        textdomain ("my_time");
                          
        printf( gettext("The time is: ") );

        time(&date);
        tp = localtime(&date);
        strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %z %Y", tp);

        printf ("%s\n",buf);

}

Oi entoles bindtextdomain() kai textdomain() aforoun thn nls biblio8hkh (libintl) kai thn plhroforoun oti h metafrash toy programmatos brisketai sto my_time.mo ston katalogo /usr/share/locale/XX, opoy XX h glwssa toy xrhsth (ka8orizetai apo thn metablhth LANG h LANGUAGE).

Epeidh polles fores to na grafoyme gettext(...) einai epipono gia polla mhnumata, xrhsimopoioume to:


        #define _(Text) gettext(Text) /* stis epikefalides */

        printf( _("The time is: ") );

To programma den 8a leitoyrghsei akomh me ellhnika. Xreiazetai akomh th dhmioyrgia toy .po arxeioy. Ayto einai sxetika aplo an ginei me to programma xgettext (apo to paketo GNU gettext).

Gia to sygkekrimeno programma h ejodos toy xgettext einai: (suntajh "xgettext my_time.c")


# messages.po
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR Free Software Foundation, Inc.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 1998-11-11 22:52+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: ENCODING\n"

#: c.c:16
msgid "The time is: "
msgstr ""

#end of messages.po

kai edw metonomazoyme to messages.po se el.po, pros8etontas taytoxrona sto msgstr thn metafrash, px:
msgstr "H wra einai: "

Teleytaio melhma toy programmatisth einai na metatrecei ayto to .po arxeio se .mo h .gmo morfh. Ayto epitygxanetai me to programma msgfmt (apo to GNU gettext pali).


px msgfmt el.po -o el.gmo

kai meta na antigrafei sto /usr/share/locale/el/LC_MESSAGES/ to el.gmo san my_time.mo (ayta ta teleytaia kalo einai na ginoyn mesw kapoioy makefile).

To shmantikotero twra einai na aytomatopoihsoyme ayth th diadikasia. Ayto epitygxanetai me to GNU autoconf. As ftiajoyme ena configure.in poy 8a elegxei kata poso yparxei yposthrijh nls apo thn libc kai ka8ws kai toy strftime. (gia kaluteres diamorfwseis koitajte sto: http://teamball.sdsu.edu/doc/texi/gettext_toc.html)

Gia na aplopoihsoyme ta pragmata 8ewroume thn ejhs diamorfwsh twn arxeiwn toy programmatos: (estw oti to programma einai sto /xxx) ston katalogo /xxx/

configure.in: Xreiazetai gia to autoconf (deite parakatw)
config.h.in : (deite parakatw)
ston katalogo /xxx/src/
my_time.c   : To programma

ston katalogo /xxx/po/

my_time.pot :  Ayto einai to messages.po poy dhmioyrgeitai apo to
               xgettext aplws metonomasmeno
el.po       :  To my_time.pot metafrasmeno sta ellhnika
POTFILES.in :  Edw pros8etete ola ta .c arxeia sto src poy xrhsimopoioun
               to gettext. px:
               /xxx/src/my_time.c

Arkei twra na antigracete apo to paketo gettext-0.10 ta po/Makefile.in ston katalogo toy programmatos /xxx/po/ , olo ton katalogo intl/ ston /xxx/intl/ kai ta ABOUT-NLS, aclocal.m4 ston /xxx .

Pame twra ston prwtarxiko katalogo toy programmatos mas (/xxx) kai as ftiajoyme to configure.in apo to opoio 8a prokucei to gnwsto(;) script configure.


# configure.in for my_time.c
AC_INIT()
AC_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PATH_PROG(MAKE,make)
AC_PROG_INSTALL

VERSION=1.0
PROGRAMS="my_time"

AC_PREFIX_DEFAULT(/usr/local)
AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE")
AC_DEFINE_UNQUOTED(VERSION, "$VERSION")
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)
AC_SUBST(PROGRAMS)

dnl yposthrizoyme ellhnika (to el einai apo to iso639 poy prepei na
dnl xrhsimopoieitai gia thn endeijh glwssas stis metafraseis sumfwna me to
dnl egxeiridio to gettext.)
ALL_LINGUAS="el"

dnl Gia to gettext 0.10.
ud_GNU_GETTEXT

AC_LINK_FILES($nls_cv_header_libgt, $nls_cv_header_intl)

AC_FUNC_STRFTIME

AC_OUTPUT([Makefile src/Makefile intl/Makefile po/Makefile.in
[sed -e "/POTFILES =/r po/POTFILES" po/Makefile.in > po/Makefile])

#end of configure.in

kai amesws meta dhmioyrgoume ena config.h.in (einai bash toy arxeioy config.h poy 8a symperilambanetai sto programma mas. H dhmioyrgia toy config.h ginetai apo to script configure.) To config.h.in 8a periexei:


# config.h.in for my_time.c
/* To onoma toy paketoy (8a xrhsimopoih8ei gia to textdomain) */
#undef PACKAGE
#undef VERSION

/* strftime */
#undef HAVE_STRFTIME
#undef ENABLE_NLS

#end of config.h.in

To Makefile.in gia to programma (to Makefile dhmioyrgeitai epishs aytomata apo to configure), prepei na exei se genikes grammes ta:


# Makefile.in
CC      = @CC@
LIBS    = @LIBS@
CCOPTS  = @CFLAGS@ -I. -I..
LN      = @LN_S@
INSTALL = @INSTALL@
prefix  = @prefix@
exec_prefix  = @prefix@
datadir = $(prefix)/lib
bin     = $(prefix)/bin
localedir = $(datadir)/locale
DEFS    = -DLOCALEDIR=\"$(localedir)\"
SUBDIRS = @INTLSUB@ src @POSUB@
MAKE    = @MAKE@
INSTALL = @INSTALL@

#Ean o kwdikas sas den einai ston src/ katalogo xreiazetai orismenes allages
all:
        @for subdir in $(SUBDIRS); do \
                echo making all in $$subdir; \
                (cd $$subdir && $(MAKE) all) \
                || case "$(MFLAGS)" in *k*) fail=yes;; *) exit 1;; esac; \
        done && test -z "$$fail"

install:
        @$(INSTALL) my_time $(bin)
        @$(MAKE) -C po/ install

# end of makefile.in


# src/Makefile.in
# Edw oi epikefalides einai peripoy idies me prin:
CC      = @CC@
LIBS    = @LIBS@
CCOPTS  = @CFLAGS@ -I../intl -I. -I..
LN      = @LN_S@
INSTALL = @INSTALL@
prefix  = @prefix@
exec_prefix  = @prefix@
datadir = $(prefix)/lib
localedir = $(datadir)/locale
DEFS    = -DLOCALEDIR=\"$(localedir)\"

all: my_time

my_time: my_time.o
        $(CC) $(OBJECTS) -o ../my_time $(LIBDIRS) $(LIBS)

my_time.o: my_time.c
        $(CC) -c my_time.c $(CCOPTS) $(DEFS)

#end of src/Makefile.in

To programma twra 8a ginei:


# src/my_time.c

#include <stdio.h>
#include <time.h>
#include <config.h> /* h "config.h" an den balete to -I. sto Makefile */

#ifdef ENABLE_NLS
#include <libintl.h>
#endif

main ()
{
#ifdef HAVE_STRFTIME
        struct tm *tp;
        char buf[80];
        time_t date;
#else
        time_t tv;
#endif


#ifdef ENABLE_NLS
        setlocale (LC_ALL, "");
        bindtextdomain (PACKAGE, LOCALEDIR);
        textdomain (PACKAGE);
# define _(Text) gettext(Text)
#else
# define _(Text) (Text)
#endif
                          

        printf( _("The time is: ") );

#ifdef HAVE_STRFTIME_H
        time(&date);
        tp = localtime(&date);
        strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %z %Y", tp);
        printf ("%s\n",buf);
#else
        time(&tv);
        printf ( "%s",ctime(&tv) );
#endif

}

#end of src/my_time.c

Twra trexoyme to autoconf poy dhmioyrgei to configure script. An ola phgan kala exoyme ena programma poy milaei jenes glwsses!

To parapanw keimeno einai mia eisagwgh mono sto 8ema. Den einai se kamia periptwsh plhres, an 8elete parapanw plhrofories symboyleyteite ta egxeiridia twn programmatwn autoconf, gettext. Perissotera gia to gettext (kai thn xrhsh toy me to autoconf) sto: http://teamball.sdsu.edu/doc/texi/gettext_toc.html


Next Previous Contents