package i18n; $i18n::VERSION = '0.07'; =head1 NAME i18n - Perl Internationalization Pragma =head1 VERSION This document describes version 0.07 of i18n, released November 4, 2004. =head1 SYNOPSIS 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"; =head1 DESCRIPTION Internationalization (abbreviated C) 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. Strings for humans need to get localized (translated to the language of the human using your program) and strings for computers B get translated. =head2 Syntax The C module gives you a remarkably simple way to mark strings that are intended for humans. All you do is put two tilde signs (C<~~>) 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 = ~~< the string in the user's language. To turn off the magic of C<~~> lexically, just say: no i18n; One nice thing about this particular markup, is that you can completely turn off internationalization, by simply removing the C statement. The C<~~> signs are actually valid Perl that just happen to not do anything in this context, and thus are constant-optimized away at compile time. =head2 Implementation When you say: my $string = ~~"Bob is your uncle"; then C<$string> really is an C object that is overloaded to stringify as a localized translation. Currently, the magic is just a thin wrapper on C, which makes it equivalent to this call: 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); =head1 CAVEATS The authors of this module are not linguists. If you would like to help us define suitable C 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 C<~~> notation, and 24 hours later it would "just work" in Swahili. =cut use strict; use constant DATA => 0; use constant LINE => 1; use constant PACKAGE => 2; use constant NEGATED => 3; use warnings::register; use overload ( '~' => \&_negate, '.' => \&_concat, '""' => \&_stringify, fallback => 1, ); sub import { my $class = shift; my $caller = caller; eval { require Locale::Maketext::Simple; 1 } or return; overload::constant( q => sub { shift; pop; bless([ \@_, # DATA (caller(1))[2], # LINE $caller, # PACKAGE ], $class); }, ); { no strict 'refs'; no warnings 'redefine'; delete ${"$class\::"}{loc}; delete ${"$class\::"}{loc_lang}; unshift @_, 'Path' if @_ % 2; Locale::Maketext::Simple->import(@_); *{"$caller\::loc"} = $class->can('loc'); *{"$caller\::loc_lang"} = $class->can('loc_lang'); *{"$class\::loc"} = \&_loc; *{"$class\::loc_lang"} = \&_loc_lang; } @_ = (warnings => $class); goto &warnings::import; } sub unimport { my $class = shift; overload::remove_constant('q'); @_ = (warnings => $class); goto &warnings::unimport; } sub loc { goto \&_loc } sub loc_lang { goto \&_loc_lang } sub _loc { my $class = shift; return $_[0] unless UNIVERSAL::can($_[0], '_negate'); goto &_do_loc; } sub _loc_lang { my $class = shift; my $caller = caller; my $loc_lang = $caller->can('loc_lang') or return; goto &$loc_lang; } sub _negate { my $class = ref $_[0]; return ~_stringify($_[0]) unless warnings::enabled($class); goto &_do_loc if $_[0][NEGATED]; bless([ [@{$_[0][DATA]}], # DATA $_[0][LINE], # LINE $_[0][PACKAGE], # PACKAGE 1, # NEGATED ], $class); } sub _concat { my $class = ref $_[0]; my $pkg = $_[0][PACKAGE]; @_ = reverse(@_) if pop; return join('', @_) unless warnings::enabled($class); my $line = (caller)[2]; my ($seen, @data); foreach (@_) { (push(@data, bless(\\$_, "$class\::var")), next) unless ref($_) and UNIVERSAL::isa($_, $class); $seen++; (push(@data, bless(\\$_, "$class\::var")), next) unless $_->[LINE] == $line and !$_->[NEGATED]; $seen++; $pkg = $_->[PACKAGE]; push @data, @{$_->[DATA]}; } return join('', @data) if $seen < 2; return bless([ \@data, # DATA $line, # LINE $pkg, # PACKAGE ], $class); } sub _stringify { ($_[0][NEGATED]) ? ~join('', map { (ref $_) ? "$$$_" : "$_" } @{$_[0][DATA]}) : join('', map { (ref $_) ? "$$$_" : "$_" } @{$_[0][DATA]}); } sub _do_loc { my $class = ref $_[0]; my $pkg = $_[0][PACKAGE]; ($pkg eq caller) and my $loc = $pkg->can('loc') or return ~"$_[0]"; my @vars; my $format = join('', map { UNIVERSAL::isa($_, "$class\::var") ? do { push(@vars, $$$_); "[_".@vars."]" } : do { my $str = $_; $str =~ s/(?=[\[\]~])/~/g; $str }; } @{$_[0][DATA]}); # Defeat constant folding return bless([$loc => $format], 'i18n::string') if !@vars; @_ = ($format, @vars); goto &$loc; } package i18n::string; use overload ( '""' => \&_stringify, '0+' => \&_stringify, fallback => 1, ); sub _stringify { $_[0][0]->($_[0][1]); } package i18n::var; use overload ( '""' => \&_stringify, '0+' => \&_stringify, fallback => 1, ); sub _stringify { ${${$_[0]}} } 1; =head1 SEE ALSO L, L =head1 AUTHORS Autrijus Tang Eautrijus@autrijus.orgE, Brian Ingerson EINGY@cpan.orgE. =head1 COPYRIGHT Copyright 2004 by Autrijus Tang Eautrijus@autrijus.orgE, Brian Ingerson EINGY@cpan.orgE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L =cut