When
shadox posted his
JPEG Files ReSize with output in Spanish, and
bladx replied with
"Converting the Spanish text to English, (for easier readability for English-speaking persons. Or perhaps, the english equivilents maybe?) :)", that got me thinking.
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:
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";
I wonder if anyone can further improve on the syntax and usability?
Here is the code:
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;
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.
—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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.