I18N Shell Scripts (gettext)

I18N Shell Scripts (gettext)

am 01.11.2007 19:58:18 von wayne

I don't have a need for this, but I can't
get it to work and it's frustrating! Please
tell me what I'm doing wrong! I am using
Fedora Linux with Bash and gettext:
$ gettext --version
gettext (GNU gettext-runtime) 0.16.1

Suppose I have this script as i18n_greet.sh:

#!/bin/sh
printf "Enter your name: "
read NAME
printf "Hello, $NAME!\n"
# End of Script

To internationalize it, say to support French as well
as English, I have transformed it to this:

#!/bin/sh
export TEXTDOMAINDIR=~/locale # normally /usr/share/locale is used
export TEXTDOMAIN=i18n_greet
.. gettext.sh
printf "$(gettext -n 'Enter your name: ')"
read NAME
printf "$(eval_gettext 'Hello, $NAME!')"
# End of Script

That works for English. To make it work for French as well,
I generate a French PO file (i18n_greet.fr.po) with:
$ umask 022
$ xgettext -d i18n_greet.fr -L Shell i18n_greet.sh

And this template PO file is edited to produce:

# French .po file for i18n_greet.sh script.
# Copyright (C) 2007
# This file is distributed under the same license as the i18n_greet.sh package.
# Author: Wayne , 2007
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: i18n_greet.sh-1.0\n"
"Report-Msgid-Bugs-To: Wayne \n"
"POT-Creation-Date: 2007-11-01 11:02-0400\n"
"PO-Revision-Date: 2007-11-01 11:02-0400\n"
"Last-Translator: Wayne \n"
"Language-Team: French \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: i18n_greet.sh:10
msgid "Enter your name: "
msgstr "Entrez votre nom"

#: i18n_greet.sh:11
#, sh-format
msgid "Hello, $NAME!"
msgstr "Salut, $NAME!"


Now compile this PO file to an MO file:

$ umask 022
$ msgfmt -o i18n_greet.mo i18n_greet.fr.po
$ mkdir ~/fr/LC_MESSAGES/
$ mv i18n_greet.mo ~/fr/LC_MESSAGES/

(Note that the directories and the files have
the proper permissions, so the file can be found
and read.)

That should be it. But, even if I set (and export)
LANG and/or LC_ALL to 'fr', it comes out in English.

I read someplace there is a POSIX alternative to the
Gnu gettext / PO / MO system "gencat"?). If I need
To use that, can anyone point out an example
"hello, world" script?

Thanks!

-Wayne

Re: I18N Shell Scripts (gettext)

am 01.11.2007 20:11:50 von Stephane CHAZELAS

2007-11-01, 14:58(-04), Wayne:
[...]
> #!/bin/sh
> export TEXTDOMAINDIR=~/locale # normally /usr/share/locale is used
> export TEXTDOMAIN=i18n_greet
[...]
> $ msgfmt -o i18n_greet.mo i18n_greet.fr.po
> $ mkdir ~/fr/LC_MESSAGES/
> $ mv i18n_greet.mo ~/fr/LC_MESSAGES/
[...]

Shouldn't it be

mkdir -p ~/locale/fr/LC_MESSAGES/
mv i18n_greet.mo ~/locale/fr/LC_MESSAGES/

Have you tried strace?

--
Stéphane

Re: I18N Shell Scripts (gettext)

am 01.11.2007 21:41:32 von wayne

Stephane CHAZELAS wrote:
> 2007-11-01, 14:58(-04), Wayne:
> [...]
>> #!/bin/sh
>> export TEXTDOMAINDIR=~/locale # normally /usr/share/locale is used
>> export TEXTDOMAIN=i18n_greet
> [...]
>> $ msgfmt -o i18n_greet.mo i18n_greet.fr.po
>> $ mkdir ~/fr/LC_MESSAGES/
>> $ mv i18n_greet.mo ~/fr/LC_MESSAGES/
> [...]
>
> Shouldn't it be
>
> mkdir -p ~/locale/fr/LC_MESSAGES/
> mv i18n_greet.mo ~/locale/fr/LC_MESSAGES/

Yeah, that was just a typo in the email, I did that right (I just
checked).

> Have you tried strace?

Just now, but none of the 800+ lines of output refer to ~/locale/...
I don't see any obvious problems in there, but I'm not sure what
to look for.

-Wayne

Re: I18N Shell Scripts (gettext)

am 01.11.2007 21:52:48 von Stephane CHAZELAS

2007-11-01, 14:58(-04), Wayne:
[...]
> That should be it. But, even if I set (and export)
> LANG and/or LC_ALL to 'fr', it comes out in English.
[...]

Does "fr" appear in the list returned by "locale -a".

You may need to use fr_FR or fr_FR.iso885915@euro...

--
Stéphane

Re: I18N Shell Scripts (gettext)

am 02.11.2007 02:58:45 von wayne

Stephane CHAZELAS wrote:
> 2007-11-01, 14:58(-04), Wayne:
> [...]
>> That should be it. But, even if I set (and export)
>> LANG and/or LC_ALL to 'fr', it comes out in English.
> [...]
>
> Does "fr" appear in the list returned by "locale -a".
>
> You may need to use fr_FR or fr_FR.iso885915@euro...
>
That did it! locale -a | grep fr shows "french" and "fr_FR"
but not "fr". Making that change fixed it. Thanks for the
help.

For anyone who cares, here's the script and the PO file.
the comments in the script show how to set this up.

-Wayne

=================~/bin/i18n_greet.sh===============
#!/bin/sh

# Internationalized version of script.
#
# To get I18N for this script, use the Gnu (not POSIX) system:
# Create a template PO file "i18n_greet.po" with:
# xgettext -d i18n_greet -L Shell i18n_greet.sh
# Customize that file. Now make (translated) copies for
# each language:
# cp i18n_greet.po i18n_greet.lang.po; vi $_
# ...
# where lang is fr_FR, en_US, ... Note that 'lang' must be one of
# the systems supported locales returned by: locale -a
# Now create if needed a place for the locale files, such as:
# umask 022; mkdir -p ~/locale/lang/LC_MESSAGES/
# Next compile the .po files:
# msgfmt -o ~/locale/lang/LC_MESSAGES/i18n_greet.mo i18n_greet.lang.po
# ...
# Be sure the files are readable!
#
# Finally try it out: LANG=fr_FR i18n_greet.sh
#
# $Id: i18n_greet.sh,v 1.2 2007/11/01 16:08:40 wayne Exp $

# Since several programs in one package may share the same catalog
# of strings, the strings are looked-up by TEXTDOMAIN and language,
# rather than by program-name and language. We need to set
# TEXTDOMAIN, and tell gettext where to find the language-specific
# .mo files:
export TEXTDOMAINDIR=~/locale # normally /usr/share/locale is used
export TEXTDOMAIN=i18n_greet

# Source the gettext.sh script, provides some shell functions:
.. gettext.sh

printf "$(gettext 'Enter your name: ')"
read NAME
printf "$(eval_gettext 'Hello, $NAME!')\n"

=================~/bin/i18n_greet.sh===============

===~/locale/fr_FR/LC_MESSAGES/i18n_greet.fr_FR.po==

# .po template for i18n_greet.sh script.
# Copyright (C) 2007
# This file is distributed under the same license as the i18n_greet.sh package.
# Author: Wayne , 2007
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: i18n_greet.sh-1.0\n"
"Report-Msgid-Bugs-To: Wayne \n"
"POT-Creation-Date: 2007-11-01 11:02-0400\n"
"PO-Revision-Date: 2007-11-01 11:02-0400\n"
"Last-Translator: Wayne \n"
"Language-Team: Français\n"
"Content-Type: text/plain; charset=utf-8\n"
"MIME-Version: 1.0\n"
"Content-Transfer-Encoding: 8bit\n"

#: i18n_greet.sh:10
msgid "Enter your name: "
msgstr "Entrez votre nom: "

#: i18n_greet.sh:11
#, sh-format
msgid "Hello, $NAME!"
msgstr "Salut, $NAME!"

===~/locale/fr_FR/LC_MESSAGES/i18n_greet.fr_FR.po==