Finnbarr, thank you for writing this cook book.
I have one major feature request: NLSPATH should be set able at run
time of the script. I experimented with script localisation and the
following script but found that neither changing LC_ALL/LC_MESSAGES
nor NLSPATH will alter the message catalogs being used:
===== cut =====
#!/bin/ksh
NLSPATH=$PWD/locale/%l/%N.cat
name="David"
function mymsg
{
print "Simple demonstration of ksh93 message translation"
print "Message locale is: $LC_MESSAGES"
echo $"Hello"
print $"Goodbye"
printf $"Welcome %s\n" $name
print $"This string is not translated because it is not in the
message catalog"
}
mymsg
print "#####"
LC_ALL=C mymsg
===== cut =====
AFAIK there is no need to make the choice of message catalog dynamic
for LC_ALL/LC_MESSAGES changes but changing or setting NLSPATH
*within* the script which uses $"..." string literals has it's value
and practical use, i.e. in cases scripts and their translation
catalogs are installed not in a standard location.
Glenn, can you change the libast l10n catalog code and allow libast
based applications and scripts to change NLSPATH at run time, please?
Olga
On Wed, Jul 14, 2010 at 7:32 AM, Finnbarr Murphy <[email protected]> wrote:
>
> In response to recent messages asked for a "how-to" on script localization,
> here
> is a quick write up on how to localize "messages" in ksh93 scripts.
> An example is the best way to demonstate the process. Assume we want to
> localize
> the following very simple shell script called "demo" which is located in the
> subdirectory "/work/msg":
> #!/bin/ksh
> name="David"
> print "Simple demonstration of ksh93 message translation"
> print "Message locale is: $LC_MESSAGES"
> echo "Hello"
> print "Goodbye"
> printf "Welcome %s\n" $name
> print "This string is not translated because it is not in the message
> catalog"
> exit 0
> The shell script is to be localized for French and Italian users so that they
> see
> the strings enclose in double quotations (message strings) in their native
> language.
> First we must rewrite the message strings in a format which the shell
> understands
> to mean "translate this string if possible by retrieving the appropriate
> string
> from a message catalog."
> Here is the modified version of demo. It works just like the original version.
> #!/bin/ksh
> name="David"
> print "Simple demonstration of ksh93 message translation"
> print "Message locale is: $LC_MESSAGES"
> echo $"Hello"
> print $"Goodbye"
> printf $"Welcome %s\n" $name
> print $"This string is not translated because it is not in the message
> catalog"
> exit 0
> The changes should be obvious, i.e. the use of $"...".
> A decision has to be made as to where to place the localized message
> catalogs. To
> keep it simple the decision was to place them in a subdirectory under where
> the
> shell script is located. Thus the following directory structure was set up:
> /work/msg/demo
> /work/msg/demo/locale
> /work/msg/demo/locale/C
> /work/msg/demo/locale/fr
> /work/msg/demo/locale/it
>
> As an aside, AST-BASE supports the following locations for message catalogs
> by default:
> ${ROOT}/share/lib/locale/%l/%C/%N
> ${ROOT}/share/locale/%l/%C/%N
> ${ROOT}/lib/locale/%l/%C/%N
> where ${ROOT} is the directory containing the shell script and %l,%C and %N
> have
> the same meaning as when used with NLSPATH. I choose not to use any of the
> default
> locations for this example in order to demonstrate the use of NLSPATH.
> Note the ../locale/C subdirectory. It is mandatory to have a message catalog
> in this
> directory - otherwise message localization does not work. The message strings
> in
> this message catalog must be the same as the strings in your script. This
> message
> catalog is used to find the appropriate message string to retrieve from other
> locale message catalogs.
> Code in libast (../libast/port/mg.c) compares the message string from the
> shell
> script to all the message strings in this message catalog. If there is a
> match,
> the set and member numbers are used to quickly retrieve the corresponding
> message
> string from the appropriate locale message catalog if one exists.
> Here is the C locale message file (demo.msg):
> $quote "
> $set 3 This is the C locale message set
> 1 "Hello"
> 2 "Goodbye"
> 3 "Welcome %s\\n"
> I assume you are familar with message files and catalogs in general so I am
> not going to
> explain the syntax or layout of the above message file. Note, however, the
> use of 3 for
> the set number. This is mandatory for ksh93 shell scripts. It is hardcoded
> into ksh93.
> Use the AST-BASE msggen utility to generate (compile) the corresponding
> message
> catalog:
> $ msggen locale/C/demo.cat demo.msg
> You do not need the .cat extension. In fact you can call the catalog anything
> you like but the default is to expect the name of the message catalog to be
> exactly
> the same as the name of the shell script. You can work around this
> restriction to a
> certain extent by using NLSPATH.
> In this case, we set NLSPATH to handle the .cat extension.
> $ NLSPATH=/work/msg/locale/%l/%N.cat; export NLSPATH
> where %l is the language element and %N is the name parameter.
> You can also use msggen to check the compiled message catalog:
> $ msggen -l locale/C/demo.cat
> $quote "
> $set 3
> 1 "Hello"
> 2 "Goodbye"
> 3 "Welcome %s\\n"
> Here is the French message file (demo.fr.msg):
> $quote "
> $set 3 This is the French locale message set
> 1 "Bonjour"
> 2 "Au Revoir"
> 3 "Bienvenu %s\\n"
> which is compiled into a French locale message catalog by:
> $ msggen locale/fr/demo.cat demo.fr.msg
> and here is the Italian message file (demo.fr.msg):
> $quote "
> $set 3 This is the Italian locale message set
> 1 "Ciao"
> 2 "Addio"
> 3 "Benvenuto %s\\n"
> which is compiled into an Italian locale message catalog by:
> $ msggen locale/it/demo.cat demo.it.msg
> Now we are ready to test the localization of the demo script.
> $ LC_MESSAGES=en_US.utf8; export LANG
> $ ./demo
> Simple demonstration of ksh93 message translation
> Message locale is: en_US.utf8
> Hello
> Goodbye
> Welcome David
> This string should not be translated because it is not in the message
> catalog
> $ LC_MESSAGES=fr_FR.utf8; export LANG
> $ ./demo
> Simple demonstration of ksh93 message translation
> Message locale is: fr_FR.utf8
> Bonjour
> Au Revoir
> Bienvenu David
> This string should not be translated because it is not in the message
> catalog
> $ LC_MESSAGES=it_IT.utf8; export LANG
> $ ./demo
> Simple demonstration of ksh93 message translation
> Message locale is: it_IT.utf8
> Ciao
> Addio
> Benvenuto David
> This string should not be translated because it is not in the message
> catalog
> I hope this helps readers understand how to localize their ksh93 shell
> scripts. I
> will write up a better description of the process in my blog in the next few
> weeks
> but this should be sufficient to get you started.
> - Finnbarr
> P.S. Above example tested on version 93t+.
>
>
>
>
> _______________________________________________
> ast-users mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-users
>
--
, _ _ ,
{ \/`o;====- Olga Kryzhanovska -====;o`\/ }
.----'-/`-/ [email protected] \-`\-'----.
`'-..-| / http://twitter.com/fleyta \ |-..-'`
/\/\ Solaris/BSD//C/C++ programmer /\/\
`--` `--`
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users