After spending most of the weekend working on maintaining my computer, I finally got around to trying this.
Obviously, a Perl program can use the native or normal multi-language features of the platform. In Win32, that means using a "resource" file, and since Perl doesn't use PE (COFF) files for the executable, it would have to be an additional file. And you would need tools for making it etc. You could have some kind of external file listing all the strings per language, but no matter what format that file's in, you need to replace all the strings in the program with ID values. That can make things less-than-readable, especially for simple scripts that have lots of on-the-fly output (as opposed to labels on widgets loaded from a database).
So, I think it will help if all possible forms are supplied in the program's text, near where it is used. Ideally, it could be done right where the string would have gone.
Here is my proof-of-concept:
I wonder if anyone can further improve on the syntax and usability?use strict; use warnings; use LangString; my $s1= new LangString { English => "Error opening directory", Spanish => "Error abriendo directorio" }; print "My string is $s1\n"; $LangString::Language= 'Spanish'; print "My string is $s1\n";
Here is the code:
It's simplistic, because it's just a concept. In a full implementation, the default would automatically obtain the language from the OS, and strings would be checked for absense and use an inheritance mechanism (e.g. British and American only specified when there are differences, but English specified when it works for both) and other error checking.use strict; use warnings; package LangString; our $Language= 'English'; use overload '""' => \&string; sub new { my $class= shift; my $self= shift; bless $self, $class; return $self; } sub string { my $self= shift; return $$self{$Language}; } 1;
—John
Summary of commentary and additional thoughts:
- This doesn't directly handle interpolation. With Window's FormatMessage API function, you can pass an array of strings in that will substitute markers in the translated string. Can easily do the same here.
- To extend that, can populate the table with a sub instead of a string, for more complex context-aware formatting (e.g. number/gender changes).
- See DiscusWare source code, Locale::gettext
- crazyinsomniac suggets one could have a tie interface to a database. I agree that populating a collection of these things via an external file rather than declaring them one-by-one has value, I don't see why a tie would buy you anything over just reading in the whole thing up front.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re (tilly) 1: Perl Programs That Support Multiple (Human) Languages
by tilly (Archbishop) on Aug 27, 2001 at 08:47 UTC | |
by pmas (Hermit) on Aug 27, 2001 at 09:34 UTC | |
by dws (Chancellor) on Aug 27, 2001 at 09:32 UTC | |
by John M. Dlugosz (Monsignor) on Aug 27, 2001 at 11:04 UTC | |
by John M. Dlugosz (Monsignor) on Aug 27, 2001 at 09:22 UTC | |
Re: Perl Programs That Support Multiple (Human) Languages
by cLive ;-) (Prior) on Aug 27, 2001 at 08:29 UTC | |
Re: Perl Programs That Support Multiple (Human) Languages
by arhuman (Vicar) on Aug 27, 2001 at 11:07 UTC | |
Re: Perl Programs That Support Multiple (Human) Languages
by stefp (Vicar) on Aug 28, 2001 at 05:20 UTC |