Template::Multilingual3pm
Multilingual templates for template toolkit
Usage
This subclass of Template Toolkit's "Template" class supports multilingual templates: templates that contain text in several languages.
<t><en>Hello!</en><fr>Bonjour !</fr></t>
Specify the language to use when processing a template:
use Template::Multilingual;my $template = Template::Multilingual->new();$template->language('en');$template->process('example.ttml');
You can also provide the name of the template variable that will hold the language:
my $template = Template::Multilingual->new(LANGUAGE_VAR => 'foo');$template->process('example.ttml', { foo => 'en' });
Methods
new(\%params)
The new() constructor creates and returns a reference to a new template object. A reference to a hash may be supplied as a parameter to provide configuration values.
Configuration values are all valid "Template" superclass options, and one specific to this class:
- LANGUAGE_VAR
-
The LANGUAGE_VAR option can be used to set the name of the template variable which contains the current language.
my $parser = Template::Multilingual->new({LANGUAGE_VAR => 'global.language',});If this option is set, your code is responsible for setting the variable's value to the current language when processing the template. Calling
"language()"will have no effect.If this option is not set, it defaults to language.
language($lcode)
Specify the language to be used when processing the template. Any string that matches "\w+" is fine, but we suggest sticking to ISO-639 which provides 2-letter codes for common languages and 3-letter codes for many others.
process
Used exactly as the original Template Toolkit "process" method. Be sure to call "language" before calling "process".
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:
- •
-
Exact match: the current language is found in the template
language template outputfr <fr>foo</fr><fr_CA>bar</fr_CA> foofr_CA <fr>foo</fr><fr_CA>bar</fr_CA> bar
- •
-
Fallback to the primary language
language template outputfr_CA <fr>foo</fr><fr_BE>bar</fr_BE> foo
- •
-
Fallback to first (in alphabetical order) other variant of the primary language
language template outputfr <fr_FR>foo</fr_FR><fr_BE>bar</fr_BE> barfr_CA <fr_FR>foo</fr_FR><fr_BE>bar</fr_BE> bar
Copyright & license
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.
Releases
10 releases since Debian 4.0 (Etch); the text changed 5 times.