Message catalogs (I18N) overnight hack

H

Hal E. Fulton

Hi, everyone.

I've been thinking about message catalogs (as in I18N)
for the last day or so.

I've hacked something together that works like this:


require "msgcat"
MsgCat.load("de_DE")

puts "Hello, world!"
puts "Hello, Tom!"
puts "User adam has opened file garden.eden already."
puts "User foo has opened file bar already."
printf "User %s has opened file %s already.\n", "eve", "apple"

puts "Douglas Adams says that 'the meaning of life' is 42."
puts "This %1 symbol will be ignored."
printf "The cost of %s is $%5.2f.\n", "this item", 49.95

x = "Hello, world!"
y = x.xlate
lputs y # Hallo, Welt!

And the output is:

Hallo, Welt!
Hallo, Tom!
Datei garden.eden wird schon geoeffnet von adam.
Datei bar wird schon geoeffnet von foo.
Datei apple wird schon geoeffnet von eve.
Douglas Adams hat gesagt, dass 'the meaning of life' '42' ist.
This %1 symbol will be ignored.
$49.95 ist der Preis von 'this item'.
Hallo, Welt!

What do you think?

It's just an overnight hack. I might clean it up and release it
if there is interest.

Here are some highlights and limitations:

- Obviously I'm fiddling with Kernel#puts, #print, and #printf
- I didn't think #p should be changed
- For the heck of it, I added a String#xlate
- Anything that can't be found in the message list remains
untranslated
- For "literal" (untranslated) output, I'm exposing the aliases
lputs, lprint, and lprintf
- Parameters may be ordered (since word order differs between
languages)
- There are always two catalogs in use -- the primary one is
called "native.cat" and it allows a lookup into the target
language in the other catalog
- The native.cat catalog doesn't have to be in English
- The message ordering is irrelevant (though they are sorted
internally by number of parameters to avoid matching problems)
- Message order does not have to be the same in corresponding
catalogs (though the ids must correspond)
- Currently I'm not dealing with character set issues at all
- There's no real compliance with anyone else's way of doing
things as yet -- I'm reinventing the wheel until it seems
better not to
- The message catalogs are YAML-based (thanks, _why!)
- I'm planning a little utility to create catalogs
- Besides a utility, I think a little API for adding messages
is appropriate
- I'm thinking of a "logging mode" to aid in extracting message
strings for translation
- I'm also thinking of a "warning mode" when an untranslated
string is found
- There's error checking possible that I'm not doing yet
- The MsgCat.detect method will read the $LANG environment
variable (but so far it ignores the .utf8 or whatever)
- Right now, message catalogs are searched for only in the
current directory
- Internally a message is a Symbol
- I'm thinking of mputs/mprint/mprintf to take a first arg which
would explicitly identify the message and bypass the lookup
- Currently there are some ugly hacks in the code, and it's
possible to mislead or confuse the matching (e.g., a message
with parameters with missing numbers, etc.)

Cheers,
Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top