In response to recent messages asked for a "how-to" on script localization, 
hereis 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 
localizethe following very simple shell script called "demo" which is located 
in thesubdirectory "/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 
seethe strings enclose in double quotations (message strings) in their native 
language.
First we must rewrite the message strings in a format which the shell 
understandsto mean "translate this string if possible by retrieving the 
appropriate stringfrom 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. 
Tokeep it simple the decision was to place them in a subdirectory under where 
theshell 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 catalogsby 
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 
havethe same meaning as when used with NLSPATH.  I choose not to use any of the 
defaultlocations 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 
thisdirectory - otherwise message localization does not work. The message 
strings inthis message catalog must be the same as the strings in your script. 
This messagecatalog is used to find the appropriate message string to retrieve 
from otherlocale message catalogs.
Code in libast (../libast/port/mg.c) compares the message string from the 
shellscript 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 
messagestring 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 toexplain 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 
messagecatalog:
  $ msggen locale/C/demo.cat demo.msg
You do not need the .cat extension.  In fact you can call the catalog 
anythingyou 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. 
Iwill write up a better description of the process in my blog in the next few 
weeksbut this should be sufficient to get you started.
- Finnbarr
P.S.  Above example tested on version 93t+.
                                          
_______________________________________________
ast-developers mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-developers

Reply via email to