how to add localisation support to your bash scripts

# 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 6: create .mo file
msgfmt -o locale/nl/LC_MESSAGES/pct-scanner-script.mo lang/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-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 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:~$

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