For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > Re: Multilanguage capable scripts howto?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Multilanguage capable scripts howto?
A. Sinan Unur

2005-06-06, 3:58 pm

Rainer Krienke <krienke@uni-koblenz.de> wrote in
news:d81ahh$cll$1@news.uni-koblenz.de:

> Hello,
>
> I am looking for a way to make a perl script running on a linux system
> output messages in several languages depending on the LOCALE in the
> environment. The translations for the output of the script should be
> kept in seperate files so the application is seperate from the
> translation.
>
> Well most of the linux applications I know can do this so I guess
> there is a generic i18n API to do this. The problem is I do not find
> information exactly how it can be done? Can anyone give me a hint?



CPAN is your friend.

I like Locale::Maketext:

<URL: http://search.cpan.org/~nwclark/per...le/Maketext.pod>

There are also modules corresponding to gettext etc.

Sinan
A. Sinan Unur

2005-06-06, 3:58 pm

"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in
news:Xns966D7955C423Dasu1cornelledu@132.236.56.8:

> Rainer Krienke <krienke@uni-koblenz.de> wrote in
> news:d81ahh$cll$1@news.uni-koblenz.de:


....


....
[color=darkred]
> I like Locale::Maketext:
>
> <URL:
> http://search.cpan.org/~nwclark/per...le/Maketext.pod>


Actually, you probably want:

<URL: http://search.cpan.org/~sburke/Locale-Maketext-1.09/>

Sinan
Rainer Krienke

2005-06-07, 8:57 am

A. Sinan Unur wrote:


>
> Actually, you probably want:
>
> <URL: http://search.cpan.org/~sburke/Locale-Maketext-1.09/>


Thanks for the hint. Actually I tried Locale::Maketext::Simple but without
any success. Probably I am missing something. But what? Here is what I
tried:

The perl script t.pl:

#!/usr/bin/perl
package t;
use Locale::Maketext::Simple (
Path => '/home/krienke/tmp/perl/auto'
);
loc_lang('de_DE'); # also tried "de" or without loc_lang
print loc("Hello");


In /home/krienke/tmp/perl/auto there is the translation file t.po and the
file t.mo created with msgfmt -o t.mo t.po. In ~krienke/tmp/perl/auto there
is also a de_DE/LC_MESSAGES subdirectory containing the same t.mo file. The
file t.po looks like this:

# translator-comments
#. automatic-comments
#: reference...
#, flag...
msgid "Hello"
msgstr "Hallo"

Calling the script results in the output of "Hello" but not the german
"Hallo". The LANG env variable on my machine is de_DE@euro. I also tried to
copy the file t.mo to /usr/share/locale/de_DE/LC_MESSAGES and tried the
default package initialisation: use Locale::Maketext::Simple; instead of
the above but no change.

Can anyone help me out?

Thanks
Rainer
A. Sinan Unur

2005-06-07, 4:01 pm

Rainer Krienke <krienke@uni-koblenz.de> wrote in
news:d83ipu$j29$1@news.uni-koblenz.de:

> A. Sinan Unur wrote:
>
>
>
> Thanks for the hint. Actually I tried Locale::Maketext::Simple but
> without any success.


Sorry, I have never used that module.

Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
Gunnar Hjalmarsson

2005-06-07, 4:01 pm

Rainer Krienke wrote:
> The perl script t.pl:
>
> #!/usr/bin/perl
> package t;
> use Locale::Maketext::Simple (
> Path => '/home/krienke/tmp/perl/auto'
> );
> loc_lang('de_DE'); # also tried "de" or without loc_lang


There you set the language explicitly, while I was under the impression
that your problem was how to derive it from the current LOCALE.

> The file t.po looks like this:
>
> # translator-comments
> #. automatic-comments
> #: reference...
> #, flag...
> msgid "Hello"
> msgstr "Hallo"


That looks like the gettext format. However, shouldn't the file be named
de.po, and the other file de.mo, or else how would the module know from
which file it's supposed to grab the string 'Hallo'?

Just guessing here ... I know nothing about Locale::Maketext::Simple.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Rainer Krienke

2005-06-07, 4:01 pm

Gunnar Hjalmarsson wrote:

> Rainer Krienke wrote:
>
> There you set the language explicitly, while I was under the impression
> that your problem was how to derive it from the current LOCALE.
>


Thank good. You had the right idea. I thought (the documentation does not
drop a word about this) that it the filename had to be according to the
name of the package ("t") and that in auto/ there should be a structure
like it is in /usr/share/locale i.e. subdirectories with the locale names
like de_DE or en_US, ... But this was not true.

I renamed the mo-file in auto/ to de.mo and suddenly it worked.

Thanks a lot.

Rainer
A. Sinan Unur

2005-06-07, 4:01 pm

Rainer Krienke <krienke@uni-koblenz.de> wrote in
news:d84aut$jj9$1@news.uni-koblenz.de:

> Gunnar Hjalmarsson wrote:
>
>
> Thank good. You had the right idea. I thought (the documentation does
> not drop a word about this)


Not true:

> that it the filename had to be according to the name of the package
> ("t") and that in auto/ there should be a structure like it is in
> /usr/share/locale i.e. subdirectories with the locale names like
> de_DE or en_US, ... But this was not true.


From the module documentation:

SYNOPSIS

Minimal setup (looks for auto/Foo/*.po and auto/Foo/*.mo):

package Foo;

Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
Rainer Krienke

2005-06-09, 8:57 am

A. Sinan Unur wrote:

>
> Not true:
>
>
> From the module documentation:
>
> SYNOPSIS
>
> Minimal setup (looks for auto/Foo/*.po and auto/Foo/*.mo):
>
> package Foo;
>


Well and exactly in auto/Foo it does not seem to search the files. At least
it did not for me. It found them only in auto/Foo.mo . Besides the
Documentation does really not say any word about where exactly in the
filesystem the default place for "auto" is, which would also be interesting
to know I think? I still do not know this. Because of this I set the Path
explicitly. Next the description of the docs

"Minimal setup (looks for auto/Foo/*.po and auto/Foo/*.mo)"

is misleading if you do not what "*" should stand for. Now I know it stands
for the the language name but it could have been anything else as well. The
documentation could simply say: auto/Foo/*.mo, where *.mo stands for the
language mo-file like de.mo etc or something like this. This would make
things clearer.

Thanks
Rainer
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com