Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


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.

In reply to Perl Programs That Support Multiple (Human) Languages by John M. Dlugosz

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2022-08-11 05:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found