Multilanguage capable scripts howto?

R

Rainer Krienke

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?

Thanks
Rainer
 
A

A. Sinan Unur

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/perl-5.8.7/lib/Locale/Maketext.pod>

There are also modules corresponding to gettext etc.

Sinan
 
R

Rainer Krienke

A. Sinan Unur wrote:


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

A. Sinan Unur

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
 
G

Gunnar Hjalmarsson

Rainer said:
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.
 
R

Rainer Krienke

Gunnar said:
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

A. Sinan Unur

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
 
R

Rainer Krienke

A. Sinan Unur said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top