Differences between revisions 6 and 7
Revision 6 as of 2009-02-24 21:00:31
Size: 6073
Comment:
Revision 7 as of 2009-02-27 02:49:50
Size: 7973
Editor: GreyCat
Comment: note on LANG and LC_ALL variables
Deletions are marked like this. Additions are marked like this.
Line 193: Line 193:
{{{
------------------------------------------------------------------------
}}}
----

Note, systems get ''really bloody pissy'' about the exact settings of `LANG` and `LC_ALL` variables. It's hard as hell to figure out what you need to set things to, to make things work. I recommend brute force:

{{{
$ msgunfmt grep.mo
...
msgid "out of memory"
msgstr "memoria agotada"

$ LANG=es_ES TEXTDOMAIN=grep gettext -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES TEXTDOMAIN=grep gettext -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES gettext -d grep -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES gettext -s grep "out of memory"
grep out of memory

$ LANG=es_ES LC_ALL=es_ES gettext grep "out of memory"
out of memory

$ LANG=es_ES grep -dfsdf
grep: unknown directories method

$ LANG=es_ES LC_ALL=es_ES grep -z
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ LANG=es_ES LC_ALL=es_ES bash -c 'grep -z'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ sudo dpkg-reconfigure locales
[sudo] password for greg:
Generating locales (this might take a while)...
  en_US.ISO-8859-1... done
  en_US.ISO-8859-15... done
  en_US.UTF-8... done
  es_ES.UTF-8... done
Generation complete.

$ LANG=es_ES LC_ALL=es_ES bash -c 'grep -z'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ locale -a
C
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
es_ES.utf8
POSIX

$ LANG=es_ES LC_ALL=es_ES.utf8 bash -c 'grep -z'
Modo de empleo: grep [OPCIÓN]... PATRÓN [FICHERO]...
Pruebe `grep --help' para más información.

$ LANG=es_ES LC_ALL=es_ES bash -c 'grep -z'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ LANG=es_ES LC_ALL=es_ES gettext -d grep -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES.utf8 gettext -d grep -s "out of memory"
memoria agotada
}}}

Holy fucking hallelujah, ''finally'' it worked! Christ!

How to add localization support to your bash scripts

Looking for examples how to add simple localization to your bash scripts, and how to do testing, this is probably what you need ...

# Tue Feb 24 19:59:40 CET 2009 <jelledejong@powercraft.nl> <http://www.tuxcrafter.net/>

# localization:
http://pgas.freeshell.org/mirror/ABSlocalization.html
http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/localization.html

# gettext
http://www.gnu.org/software/gettext/manual/html_node/index.html

# step 1: update your strings

# before translation
function version()
{
   echo "Usage:     $0 --help"
   echo "Version:   $version"
   echo "Author:    $author"
   echo "Donation:  $donation"
}

# after translation
function version()
{
   echo $"Usage:     $0 --help"
   echo $"Version:   $version"
   echo $"Author:    $author"
   echo $"Donation:  $donation"
}

# step 2: create some sort of structure for your source
mkdir --parent --verbose locale/nl/LC_MESSAGES/
mkdir --parent --verbose lang

# step 3: check the strings
bash -D pct-scanner-script

# step 4: dump po strings to your directory
bash --dump-po-strings pct-scanner-script > lang/nl.pot

# step 5: try to merge existing po with new updates
# remove duplicated strings by hand or with sed or something else
# awk '/^msgid/&&!seen[$0]++;!/^msgid/' lang/nl.pot > lang/nl.pot.new
msgmerge lang/nl.po lang/nl.pot

# step 5.1: try to merge existing po with new updates
cp --verbose lang/pct-scanner-script-nl.po lang/pct-scanner-script-nl.po.old
awk '/^msgid/&&!seen[$0]++;!/^msgid/' lang/pct-scanner-script-nl.pot > lang/pct-scanner-script-nl.pot.new
msgmerge lang/pct-scanner-script-nl.po.old lang/pct-scanner-script-nl.pot.new > lang/pct-scanner-script-nl.po

# step 5.2: try to merge existing po with new updates
touch lang/pct-scanner-script-process-nl.po lang/pct-scanner-script-process-nl.po.old
awk '/^msgid/&&!seen[$0]++;!/^msgid/' lang/pct-scanner-script-process-nl.pot > lang/pct-scanner-script-process-nl.pot.new
msgmerge lang/pct-scanner-script-process-nl.po.old lang/pct-scanner-script-process-nl.pot.new > lang/pct-scanner-script-process-nl.po

# step 6: create .mo file
msgfmt -o locale/nl/LC_MESSAGES/pct-scanner-script.mo lang/pct-scanner-script-nl.po
msgfmt -o locale/nl/LC_MESSAGES/pct-scanner-script-process.mo lang/pct-scanner-script-process-nl.po

# step 7: move single .mo file to system locales
sudo cp --verbose locale/nl/LC_MESSAGES/pct-scanner-script.mo /usr/share/locale/nl/LC_MESSAGES/
sudo chown root:root /usr/share/locale/nl/LC_MESSAGES/pct-scanner-script.mo
sudo chmod 644 /usr/share/locale/nl/LC_MESSAGES/pct-scanner-script.mo
sudo ls -hal /usr/share/locale/nl/LC_MESSAGES/pct-scanner-script.mo

# step 8: move all .mo files to system locales
sudo cp --verbose --recursive locale/* /usr/share/locale/
sudo chown root:root /usr/share/locale/nl/LC_MESSAGES/pct-scanner-*.mo
sudo chmod 644 /usr/share/locale/nl/LC_MESSAGES/pct-scanner-*.mo
sudo ls -hal /usr/share/locale/nl/LC_MESSAGES/pct-scanner-*.mo

# step 9: add the following to you bash script under #!/bin/bash
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=pct-scanner-script

# step 10: start testing

LANG=nl_NL
bash pct-scanner-script --version

LANG=en_GB
bash pct-scanner-script --version

------------------------------------------------------------------------

# sudo dpkg-reconfigure locales

export LANG=en_GB
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=pct-scanner-script
echo $"lineart: yes"
echo $"color: yes"
echo $"source: $SOURCE"
echo $"testing"

LANG=nl_NL
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=pct-scanner-script
echo $"lineart: yes"
echo $"color: yes"
echo $"source: $SOURCE"
echo $"testing"

------------------------------------------------------------------------

jelle@debian-eeepc:~/packages-checkout/source/pct-scanner-scripts/pct-scanner-scripts-devel$ LANG=nl_NL
jelle@debian-eeepc:~/packages-checkout/source/pct-scanner-scripts/pct-scanner-scripts-devel$ bash pct-scanner-script --version
Gebruik:  pct-scanner-script --help
Versie:   0.0.5
Maker:    Jelle de Jong <jelledejong@powercraft.nl>
Donatie:  http://www.tuxcrafter.net/pages/contact.html#paypal-donation
jelle@debian-eeepc:~/packages-checkout/source/pct-scanner-scripts/pct-scanner-scripts-devel$
jelle@debian-eeepc:~/packages-checkout/source/pct-scanner-scripts/pct-scanner-scripts-devel$ LANG=en_GB
jelle@debian-eeepc:~/packages-checkout/source/pct-scanner-scripts/pct-scanner-scripts-devel$ bash pct-scanner-script --version
Usage:     pct-scanner-script --help
Version:   0.0.5
Author:    Jelle de Jong <jelledejong@powercraft.nl>
Donation:  http://www.tuxcrafter.net/pages/contact.html#paypal-donation
jelle@debian-eeepc:~/packages-checkout/source/pct-scanner-scripts/pct-scanner-scripts-devel$

------------------------------------------------------------------------

jelle@debian-eeepc:~$ export LANG=en_GB
jelle@debian-eeepc:~$ export TEXTDOMAINDIR=/usr/share/locale
jelle@debian-eeepc:~$ export TEXTDOMAIN=pct-scanner-script
jelle@debian-eeepc:~$ echo $"lineart: yes"
lineart: yes
jelle@debian-eeepc:~$ echo $"color: yes"
color: yes
jelle@debian-eeepc:~$ echo $"source: $SOURCE"
source:
jelle@debian-eeepc:~$ echo $"testing"
testing
jelle@debian-eeepc:~$
jelle@debian-eeepc:~$ LANG=nl_NL
jelle@debian-eeepc:~$ TEXTDOMAINDIR=/usr/share/locale
jelle@debian-eeepc:~$ TEXTDOMAIN=pct-scanner-script
jelle@debian-eeepc:~$ echo $"lineart: yes"
lineart: ja
jelle@debian-eeepc:~$ echo $"color: yes"
kleur: ja
jelle@debian-eeepc:~$ echo $"source: $SOURCE"
bron:
jelle@debian-eeepc:~$ echo $"testing"
dit is een test
jelle@debian-eeepc:~$


Note, systems get really bloody pissy about the exact settings of LANG and LC_ALL variables. It's hard as hell to figure out what you need to set things to, to make things work. I recommend brute force:

$ msgunfmt grep.mo 
...
msgid "out of memory"
msgstr "memoria agotada"

$ LANG=es_ES TEXTDOMAIN=grep gettext -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES TEXTDOMAIN=grep gettext -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES gettext -d grep -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES gettext -s grep "out of memory"
grep out of memory

$ LANG=es_ES LC_ALL=es_ES gettext grep "out of memory"
out of memory

$ LANG=es_ES grep -dfsdf
grep: unknown directories method

$ LANG=es_ES LC_ALL=es_ES grep -z
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ LANG=es_ES LC_ALL=es_ES bash -c 'grep -z'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ sudo dpkg-reconfigure locales
[sudo] password for greg: 
Generating locales (this might take a while)...
  en_US.ISO-8859-1... done
  en_US.ISO-8859-15... done
  en_US.UTF-8... done
  es_ES.UTF-8... done
Generation complete.

$ LANG=es_ES LC_ALL=es_ES bash -c 'grep -z'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ locale -a
C
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
es_ES.utf8
POSIX

$ LANG=es_ES LC_ALL=es_ES.utf8 bash -c 'grep -z'
Modo de empleo: grep [OPCIÓN]... PATRÓN [FICHERO]...
Pruebe `grep --help' para más información.

$ LANG=es_ES LC_ALL=es_ES bash -c 'grep -z'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

$ LANG=es_ES LC_ALL=es_ES gettext -d grep -s "out of memory"
out of memory

$ LANG=es_ES LC_ALL=es_ES.utf8 gettext -d grep -s "out of memory"
memoria agotada

Holy fucking hallelujah, finally it worked! Christ!

BashFAQ/098 (last edited 2014-05-13 18:34:11 by 93-103-91-248)