i18n - Perl Internationalization Pragma
This document describes version 0.07 of i18n, released November 4, 2004.
In one-liners:
% export LANG=sp % perl -Mi18n=/path/to/po-files/ -le 'print ~~"Hello, world"'; Hola, mundo
In your module:
use i18n "/path/to/po-files"; my $place = ~~'world'; print ~~"Hello, $world";
Internationalization (abbreviated i18n) is the process of designing an application so that it can be adapted to various languages and regions. The most basic task is to let your program know which strings are meant for human consumption and which strings are intended for the computer.
i18n
Strings for humans need to get localized (translated to the language of the human using your program) and strings for computers must not get translated.
The i18n module gives you a remarkably simple way to mark strings that are intended for humans. All you do is put two tilde signs (~~) in front of every string that is intended to be translated. That's it. All the other details of localization are handled outside the program. Here are some examples:
~~
my $str1 = ~~'The time is now'; my $str2 = ~~"$str1 for having a cow"; my $str3 = ~~qq{Wow! $str2}; my $str4 = ~~<<END; How now. Brown cow. END
Think of the tilde signs as an indicator that you are looking for things that approximates the string in the user's language. To turn off the magic of ~~ lexically, just say:
no i18n;
One nice thing about this particular markup, is that you can completely turn off internationalization, by simply removing the use i18n; statement. The ~~ signs are actually valid Perl that just happen to not do anything in this context, and thus are constant-optimized away at compile time.
use i18n;
When you say:
my $string = ~~"Bob is your uncle";
then $string really is an i18n::string object that is overloaded to stringify as a localized translation.
$string
i18n::string
Currently, the magic is just a thin wrapper on Locale::Maketext::Simple, which makes it equivalent to this call:
Locale::Maketext::Simple
my $string = loc("Bob is your uncle");
Similarly, this line:
my $string = ~~"$person is your uncle";
will be turned into this at runtime:
my $string = loc("[_1] is your uncle", $person);
The authors of this module are not linguists. If you would like to help us define suitable i18n magic for your language, please send us an email.
One thing we plan to do next is to provide collaborative, just-in-time lexicon creation tools. So Joe American could write his wonderful Perl script in English, with the appropriate ~~ notation, and 24 hours later it would "just work" in Swahili.
Locale::Maketext::Simple, Locale::Maketext::Lexicon
Autrijus Tang <autrijus@autrijus.org>, Brian Ingerson <INGY@cpan.org>.
Copyright 2004 by Autrijus Tang <autrijus@autrijus.org>, Brian Ingerson <INGY@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install i18n, copy and paste the appropriate command in to your terminal.
cpanm
cpanm i18n
CPAN shell
perl -MCPAN -e shell install i18n
For more information on module installation, please visit the detailed CPAN module installation guide.