#!/bin/bash
# Initially written by Didier Spaier 2012
# This version available @ http://didier.spaier.free.fr/example.sh
# This file is intended to give a very simple example of how a shell
# script can be internationalized then localized.
# But the code snippet borrowed from Slackware this file is in the
# public domain.
# To illustrate the process we have extracted a very tiny part of the
# file /usr/lib/setup/setup included in Slackware 14's installer that we
# are going to translate (remember: we were requested to type 'setup'
# during Slackware's installation).
# As this is a working example, to prepare its execution we need to
# create a directory in /tmp where we will later put the file needed to
# perform the translation 'on the fly', called a 'messages catalog'.
# First, please make sure that the file you are now reading AND the
# file 'example.po' are in ~ (your home) before going further.
# Oh, and you will need to have both 'gettext' and 'gettext-tools'
# Slackware  packages installed to run the demo and check the results.

cd
TMP=/tmp/example_translation_slack
if [ ! -d $TMP ]; then
	mkdir -p $TMP
fi
if [ ! -f $TMP/example.po ]; then
	cp example.po $TMP
fi
if [ ! -d $TPM/fr/LC_MESSAGES ]; then
	mkdir -p $TMP/fr/LC_MESSAGES
fi
#
# The catalog of translated messages will be put in the directory we just
# created: /tmp/example_translation_slack/fr/LC_MESSAGES. In the 'real
# life' it would be /usr/share/locale/<language>/LC_MESSAGES. We will
# only translate into French hence <language> is 'fr' in this example.
#
# Now let us perform the internationalization process.
# We need to insert following lines at the top af the original script,
# after the first line but before any part of the script needing a
# translation. This should be done in every script to be internationalized.

export TEXTDOMAIN="example" # the catalog of messages will be 'example.mo'
export TEXTDOMAINDIR=$TMP # directory where to search for 'example.mo'
. gettext.sh

# the 'gettext.sh' script includes the 'eval_gettext' function, which will
# be used to spot the texts we want to localize, i.e. that we want to
# display in the langage chosen by the user.
# Let's suppose we want to localize the texts surrounded by double quotes
# in following 'code snippet' borrowed from the 'setup' script:

# dialog --title "NO LINUX PARTITIONS DETECTED" \
# --msgbox "There don't seem to be any partitions on this machine of type \
#Linux.  You'll need to make at least one of these to install Linux.  \
#To do this, you'll need to leave 'setup', and make the partitions using \
#'cfdisk' or 'fdisk'.  For more information, read the 'setup' help \
#file from the next menu." 10 60

# We will first edit this code snippet to spot the texts that we want to
# localize:

 dialog --title "`eval_gettext \"NO LINUX PARTITIONS DETECTED\"`" \
 --msgbox "`eval_gettext \"There don't seem to be any partitions on this machine of type \
Linux.  You'll need to make at least one of these to install Linux.  \
To do this, you'll need to leave 'setup', and make the partitions using \
'cfdisk' or 'fdisk'.  For more information, read the 'setup' help \
file from the next menu.\"`" 10 60

# Now we need to produce the 'catalog of messages' to be translated.
# Usually we would first produce a 'template' that every translator would
# copy before translating the messages in his or her own language. Let's
# skip this step and simply produce a catalog for translation into French.
# We will use the 'xgettext' command for that (see 'man xgettext'):

if [ ! -f $TMP/example.orig.po ] ; then
	xgettext -L Shell -o $TMP/example.orig.po example.sh
	clear
	echo " You can now type \"less $TMP/example.orig.po\" to see the messages catalog ready for translation,"
	echo -e " then \"less $TMP/example.po\" to see it after translation into French."
	echo -e " (hit the 'q' key to quit the 'less' viewer)\n"
	echo " Finally, type 'LANG=fr_FR.utf8 sh example.sh' to see the message box"
	echo -e " with the texts translated into French.\n"
	echo " When you are done with the demo, you may wish to remove the directory $TMP with its content."
	echo -e " For that, type following command: rm -rf $TMP\n"
fi

# The 'xgettext' command has written the file example.orig.po.
# Now the translator should edit that file, translating into French after
# the "msgtr" tags every text preceded by the "msgid" tag.
# We have done that already, check the provided file 'example.po'
#

# Last step, we need to format the catalog to make it usable to
# translate the messages 'on the fly' during the script's execution.
# We will use the "msgfmt' command to do that.

if [ ! -f $TMP/fr/LC_MESSAGES/example.mo ]; then
	msgfmt -o $TMP/fr/LC_MESSAGES/example.mo $TMP/example.po
fi

# That's all there is to it.
#
# Now you can try and check the results.
#
# First execute the script typing 'sh example.sh' to do the magic.
#
# Then type 'LANG=fr_FR.utf8 sh example.sh' to see the same message box
# translated into French.
#
# Now if we prepare a catalog of messages in Portuguese we will give it
# the same name: 'example.mo' and put it in the relevant directory:
# /tmp/example_translation_slack/pt/LC_MESSAGES then when typing:
# 'LANG=pt_PT.utf8 sh example.po' we will see the messages in Portuguese.
# There is no need to further edit the shell script for that.
# In the 'real life' the end user would have already set up his or her
# preferred language at the system or user level so he or she wouldn't
# have to type 'LANG=<preferred language>' every time.
#
# This illustrates the process. To sum it up:
# (1) Prepare the shell script for internationalisation: do some editing
#     if need be to ease the task of translators, then spot the messages
#     needing a translation, for instance with the 'eval_gettext' function.
# (2) Internationalize the shell script in producing a template catalog of
#     messages including the messages to be translated with 'xgettext'
# (3) Localize the messages: for each target language copy the template
#     catalog of messages, edit that copy to include the translated
#     messages, format it and put it in its place with 'msgfmt'.
#
# In the real life we should take care of one more step: every time a
# shell script will be added or edited by the maintainer, update the
# template catalog of messages, merge it with the old one and request the 
# translators to update their respective translations. There are tools to
# help us doing that in the 'gettexet-tools' Slackware package.
# 
# Thanks for reading, now you may wish to remove the files created
# running the demo. Just type 'rm -rf /tmp/example_translation_slack'.
#
# Have fun!
