r30024 - in /branches/upstream/libtemplate-multilingual-perl/current: Changes META.yml README lib/Template/Multilingual.pm lib/Template/Multilingual/Parser.pm t/basic.t
rmayorga at users.alioth.debian.org
rmayorga at users.alioth.debian.org
Sat Jan 24 03:12:44 UTC 2009
Author: rmayorga
Date: Sat Jan 24 03:12:41 2009
New Revision: 30024
URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=30024
Log:
[svn-upgrade] Integrating new upstream version, libtemplate-multilingual-perl (1.00)
Modified:
branches/upstream/libtemplate-multilingual-perl/current/Changes
branches/upstream/libtemplate-multilingual-perl/current/META.yml
branches/upstream/libtemplate-multilingual-perl/current/README
branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual.pm
branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual/Parser.pm
branches/upstream/libtemplate-multilingual-perl/current/t/basic.t
Modified: branches/upstream/libtemplate-multilingual-perl/current/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtemplate-multilingual-perl/current/Changes?rev=30024&op=diff
==============================================================================
--- branches/upstream/libtemplate-multilingual-perl/current/Changes (original)
+++ branches/upstream/libtemplate-multilingual-perl/current/Changes Sat Jan 24 03:12:41 2009
@@ -1,4 +1,8 @@
Revision history for Template-Multilingual
+
+1.00 2009-01-18
+ add support for language subtags, e.g. en_US or en-US
+ Language codes are now matched by [^<>]+ (previously \w+)
0.09 2006-07-30
remove Test::Pod and Test::Pod::Coverage tests
Modified: branches/upstream/libtemplate-multilingual-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtemplate-multilingual-perl/current/META.yml?rev=30024&op=diff
==============================================================================
--- branches/upstream/libtemplate-multilingual-perl/current/META.yml (original)
+++ branches/upstream/libtemplate-multilingual-perl/current/META.yml Sat Jan 24 03:12:41 2009
@@ -1,6 +1,6 @@
---
name: Template-Multilingual
-version: 0.09
+version: 1.00
author:
- 'Eric Cholet <cholet at logilune.com>'
abstract: Multilingual templates for Template Toolkit
@@ -13,11 +13,11 @@
provides:
Template::Multilingual:
file: lib/Template/Multilingual.pm
- version: 0.09
+ version: 1.00
Template::Multilingual::Parser:
file: lib/Template/Multilingual/Parser.pm
- version: 0.09
-generated_by: Module::Build version 0.2805
+ version: 1.00
+generated_by: Module::Build version 0.31012
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.2.html
version: 1.2
Modified: branches/upstream/libtemplate-multilingual-perl/current/README
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtemplate-multilingual-perl/current/README?rev=30024&op=diff
==============================================================================
--- branches/upstream/libtemplate-multilingual-perl/current/README (original)
+++ branches/upstream/libtemplate-multilingual-perl/current/README Sat Jan 24 03:12:41 2009
@@ -1,4 +1,4 @@
-$Id: README,v 1.2 2006/08/26 11:52:51 eric Exp $
+$Id: README,v 1.2 2006-08-26 11:52:51 eric Exp $
--------
Abstract
Modified: branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual.pm?rev=30024&op=diff
==============================================================================
--- branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual.pm (original)
+++ branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual.pm Sat Jan 24 03:12:41 2009
@@ -4,7 +4,7 @@
use base qw(Template);
use Template::Multilingual::Parser;
-our $VERSION = '0.09';
+our $VERSION = '1.00';
sub _init
{
@@ -82,7 +82,7 @@
LANGUAGE_VAR => 'global.language',
});
-If this option is set, you code is responsible for setting the
+If this option is set, your code is responsible for setting the
variable's value to the current language when processing the
template. Calling C<language()> will have no effect.
@@ -100,6 +100,38 @@
Used exactly as the original Template Toolkit C<process> method.
Be sure to call C<language> before calling C<process>.
+
+=head1 LANGUAGE SUBTAG HANDLING
+
+This module supports language subtags to express variants, e.g. "en_US" or "en-US".
+Here are the rules used for language matching:
+
+=over
+
+=item *
+
+Exact match: the current language is found in the template
+
+ language template output
+ fr <fr>foo</fr><fr_CA>bar</fr_CA> foo
+ fr_CA <fr>foo</fr><fr_CA>bar</fr_CA> bar
+
+=item *
+
+Fallback to the primary language
+
+ language template output
+ fr_CA <fr>foo</fr><fr_BE>bar</fr_BE> foo
+
+=item *
+
+Fallback to first (in alphabetical order) other variant of the primary language
+
+ language template output
+ fr <fr_FR>foo</fr_FR><fr_BE>bar</fr_BE> bar
+ fr_CA <fr_FR>foo</fr_FR><fr_BE>bar</fr_BE> bar
+
+=back
=head1 AUTHOR
@@ -136,7 +168,7 @@
=head1 COPYRIGHT & LICENSE
-Copyright 2005, 2006 Eric Cholet, All Rights Reserved.
+Copyright 2009 Eric Cholet, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Modified: branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual/Parser.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual/Parser.pm?rev=30024&op=diff
==============================================================================
--- branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual/Parser.pm (original)
+++ branches/upstream/libtemplate-multilingual-perl/current/lib/Template/Multilingual/Parser.pm Sat Jan 24 03:12:41 2009
@@ -3,7 +3,7 @@
use strict;
use base qw(Template::Parser);
-our $VERSION = '0.09';
+our $VERSION = '1.00';
sub new
{
@@ -29,17 +29,45 @@
$self->_tokenize($text);
# replace multilingual sections with TT directives
- $text = '';
+ my ($S, $E, $LANGVAR) = map $self->{$_}, qw(_start _end _langvar);
+
+ # if language is a variant (en_US), create a template variable holding the fallback value (en)
+ $text = "$S IF (tm_matches = $LANGVAR.match('^(\\w+)[-_].*\$')); tm_fb = tm_matches.0; END $E";
+
for my $section (@{$self->{_sections}}) {
if ($section->{nolang}) {
$text .= $section->{nolang};
}
elsif (my $t = $section->{lang}) {
- $text .= "$self->{_start} SWITCH $self->{_langvar} $self->{_end}";
- for my $lang (keys %$t) {
- $text .= "$self->{_start} CASE '$lang' $self->{_end}" . $t->{$lang};
- }
- $text .= "$self->{_start} END $self->{_end}";
+ my @languages = keys %$t;
+
+ # first loop through languages: look for exact match
+ $text .= "$S tm_f = 0; SWITCH $LANGVAR $E";
+ for my $lang (@languages) {
+ $text .= "$S CASE '$lang' $E" . $t->{$lang};
+ }
+ # add a default case to trigger fallback
+ $text .= "$S CASE; tm_f=1; END; $E";
+
+ # second loop: fallback to primary language (en_US matches en)
+ $text .= "$S IF tm_fb AND tm_f; tm_f=0; SWITCH tm_fb; $E";
+ for my $lang (@languages) {
+ $text .= "$S CASE '$lang' $E" . $t->{$lang};
+ }
+ # add a default case to trigger last resort fallback
+ # LANG is fr_XX or fr but template has neither
+ # we try to fallback to fr_YY is present
+ my %seen;
+ my @fallbacks = map { /^(\w+)[-_].*$/ && !$seen{$_}++ ? [ $1 => $_] : () } sort @languages;
+ if (@fallbacks) {
+ # third loop: fallback to first available variant
+ $text .= "$S CASE; tm_f=1; END; END; IF tm_f; SWITCH tm_fb || $LANGVAR; $E";
+ for my $ref (@fallbacks) {
+ my ($lang, $variant) = @$ref;
+ $text .= "$S CASE '$lang' $E" . $t->{$variant};
+ }
+ }
+ $text .= "$S END; END $E";
}
}
return $self->SUPER::parse ($text);
@@ -56,7 +84,7 @@
for my $t (@tokens) {
if ($i) { # <t>...</t> multilingual section
my %section;
- while ($t =~ m!<(\w+)>(.*?)</\1>!gs) {
+ while ($t =~ m!<([^<>]+)>(.*?)</\1>!gs) {
$section{$1} = $2;
}
push @{$self->{_sections}}, { lang => \%section }
@@ -78,7 +106,7 @@
use Template;
use Template::Multilingual::Parser;
-
+
my $parser = Template::Multilingual::Parser->new();
my $template = Template->new(PARSER => $parser);
$template->process('example.ttml', { language => 'en'});
@@ -158,6 +186,42 @@
{ lang => { fr => 'bonjour', en => 'hello' } },
{ nolang => ' bar' },
]
+
+=head1 LANGUAGE SUBTAG HANDLING
+
+This module supports language subtags to express variants, e.g. "en_US" or "en-US".
+Here are the rules used for language matching:
+
+=over
+
+=item *
+
+Exact match: the current language is found in the template
+
+ language template output
+ fr <fr>foo</fr><fr_CA>bar</fr_CA> foo
+ fr_CA <fr>foo</fr><fr_CA>bar</fr_CA> bar
+
+=item *
+
+Fallback to the primary language
+
+ language template output
+ fr_CA <fr>foo</fr><fr_BE>bar</fr_BE> foo
+
+=item *
+
+Fallback to first (in alphabetical order) other variant of the primary language
+
+ language template output
+ fr <fr_FR>foo</fr_FR><fr_BE>bar</fr_BE> bar
+ fr_CA <fr_FR>foo</fr_FR><fr_BE>bar</fr_BE> bar
+
+=back
+
+=head1 AUTHOR
+
+Eric Cholet, C<< <cholet at logilune.com> >>
=head1 BUGS
@@ -189,7 +253,7 @@
=head1 COPYRIGHT & LICENSE
-Copyright 2005, 2006 Eric Cholet, All Rights Reserved.
+Copyright 2009 Eric Cholet, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Modified: branches/upstream/libtemplate-multilingual-perl/current/t/basic.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtemplate-multilingual-perl/current/t/basic.t?rev=30024&op=diff
==============================================================================
--- branches/upstream/libtemplate-multilingual-perl/current/t/basic.t (original)
+++ branches/upstream/libtemplate-multilingual-perl/current/t/basic.t Sat Jan 24 03:12:41 2009
@@ -3,52 +3,51 @@
use strict;
my @templates = (
- { in => '',
- out => '',
- sections => [ ],
- },
- { in => 'foo',
- out => 'foo',
- sections => [ { nolang => 'foo' } ],
- },
- { in => '<t></t>',
+ {
+ name => 'empty',
+ in => '',
out => '',
sections => [ ],
},
{
+ name => 'nolang section',
+ in => 'foo',
+ out => 'foo',
+ sections => [ { nolang => 'foo' } ],
+ },
+ {
+ name => 'empty lang section',
+ in => '<t></t>',
+ out => '',
+ sections => [ ],
+ },
+ {
+ name => 'simple lang section',
in => '<t><fr>foo</fr></t>',
out => 'foo',
sections => [ { lang => { fr => 'foo' } } ],
},
{
+ name => 'other lang section',
in => '<t><en>foo</en></t>',
out => '',
sections => [ { lang => { en => 'foo' } } ],
},
{
+ name => 'multi lang section',
in => "<t><fr>foo</fr>\n<en>bar</en></t>",
out => "foo",
sections => [ { lang => { fr => 'foo', en => 'bar' } } ],
},
{
- lang =>'en',
- in => '<t><fr>foo</fr></t>',
- out => '',
- sections => [ { lang => { fr => 'foo' } } ],
+ name => 'arbitrary lang names',
+ lang => ';',
+ in => '<t><;>foo</;></t>',
+ out => 'foo',
+ sections => [ { lang => { ';' => 'foo' } } ],
},
{
- lang => 'en',
- in => '<t><en>foo</en></t>',
- out => 'foo',
- sections => [ { lang => { en => 'foo' } } ],
- },
- {
- lang => 'eng',
- in => "<t><fra>foo</fra>\n<eng>bar</eng></t>",
- out => 'bar',
- sections => [ { lang => { fra => 'foo', eng => 'bar' } } ],
- },
- { # sections
+ name => 'multiple sections',
in => "A<t><fr>foo</fr></t>B<t><en>bar</en></t>C",
out => 'AfooBC',
sections => [ { nolang => 'A' },
@@ -57,6 +56,48 @@
{ lang => { en => 'bar' } },
{ nolang => 'C' },
],
+ },
+ {
+ name => 'fr_CA exact match',
+ lang => 'fr_CA',
+ in => "<t><fr>foo</fr><fr_CA>bar</fr_CA></t>",
+ out => 'bar',
+ sections => [ { lang => { fr => 'foo', fr_CA => 'bar' } } ],
+ },
+ {
+ name => 'fr exact match',
+ lang => 'fr',
+ in => "<t><fr>foo</fr><fr_CA>bar</fr_CA></t>",
+ out => 'foo',
+ sections => [ { lang => { fr => 'foo', fr_CA => 'bar' } } ],
+ },
+ {
+ name => 'fr_CA fallback to fr',
+ lang => 'fr_CA',
+ in => "<t><fr>foo</fr><fr_BE>bar</fr_BE></t>",
+ out => 'foo',
+ sections => [ { lang => { fr => 'foo', fr_BE => 'bar' } } ],
+ },
+ {
+ name => 'fr-CA fallback to fr',
+ lang => 'fr-CA',
+ in => "<t><fr>foo</fr><fr_BE>bar</fr_BE></t>",
+ out => 'foo',
+ sections => [ { lang => { fr => 'foo', fr_BE => 'bar' } } ],
+ },
+ {
+ name => 'fr_CA fallback to fr_BE',
+ lang => 'fr_CA',
+ in => "<t><fr_FR>foo</fr_FR><fr_BE>bar</fr_BE><fr_CH>baz</fr_CH></t>",
+ out => 'bar',
+ sections => [ { lang => { fr_FR => 'foo', fr_BE => 'bar', fr_CH => 'baz' } } ],
+ },
+ {
+ name => 'fr fallback to fr_BE',
+ lang => 'fr',
+ in => "<t><fr_FR>foo</fr_FR><fr_BE>bar</fr_BE><fr_CH>baz</fr_CH></t>",
+ out => 'bar',
+ sections => [ { lang => { fr_FR => 'foo', fr_BE => 'bar', fr_CH => 'baz' } } ],
},
);
use Test::More;
@@ -69,22 +110,23 @@
for my $t (@templates) {
my $lang = $t->{lang} || 'fr';
$template->language($lang);
- is($template->language, $lang, "get/set language");
+ is($template->language, $lang, "$t->{name}: get/set language");
my $output;
- ok($template->process(\$t->{in}, {}, \$output), 'process');
- is($output, $t->{out}, 'output');
- is_deeply($template->{PARSER}->sections, $t->{sections}, 'sections');
+ ok($template->process(\$t->{in}, {}, \$output), "$t->{name}: process");
+ is($output, $t->{out}, "$t->{name}: output");
+ is_deeply($template->{PARSER}->sections, $t->{sections}, "$t->{name}: sections");
}
+# 2nd pass with overridden LANGUAGE_VAR
$template = Template::Multilingual->new(LANGUAGE_VAR => 'global.language');
ok($template);
for my $t (@templates) {
my $lang = $t->{lang} || 'fr';
my $output;
- ok($template->process(\$t->{in}, { global => { language => $lang }}, \$output), 'process');
- is($output, $t->{out}, 'output');
- is_deeply($template->{PARSER}->sections, $t->{sections}, 'sections');
+ ok($template->process(\$t->{in}, { global => { language => $lang }}, \$output), "$t->{name}: process (overridden LANGUAGE_VAR)");
+ is($output, $t->{out}, "$t->{name}: output (overridden LANGUAGE_VAR)");
+ is_deeply($template->{PARSER}->sections, $t->{sections}, "$t->{name}: sections (overridden LANGUAGE_VAR)");
}
__END__
More information about the Pkg-perl-cvs-commits
mailing list