| 1 | package Template::Flute::I18N; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Template::Flute::I18N - Localization class for Template::Flute | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | %german_map = (Cart=> 'Warenkorb', Price => 'Preis'); | ||
| 13 | |||
| 14 | sub translate { | ||
| 15 | my $text = shift; | ||
| 16 | |||
| 17 | return $german_map{$text}; | ||
| 18 | }; | ||
| 19 | |||
| 20 | $i18n = Template::Flute::I18N->new(\&translate); | ||
| 21 | |||
| 22 | $flute = Template::Flute(specification => ..., | ||
| 23 | template => ..., | ||
| 24 | i18n => $i18n); | ||
| 25 | |||
| 26 | =head1 CONSTRUCTOR | ||
| 27 | |||
| 28 | =head2 new [CODEREF] | ||
| 29 | |||
| 30 | Create a new Template::Flute::I18N object. CODEREF is used by | ||
| 31 | localize method for the text translation. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my ($proto, @args) = @_; | ||
| 37 | my ($class, $self); | ||
| 38 | |||
| 39 | $class = ref($proto) || $proto; | ||
| 40 | $self = {}; | ||
| 41 | |||
| 42 | if (ref($args[0]) eq 'CODE') { | ||
| 43 | # use first parameter as localization function | ||
| 44 | $self->{func} = shift(@args); | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | # noop translation | ||
| 48 | $self->{func} = sub {return;} | ||
| 49 | } | ||
| 50 | |||
| 51 | bless ($self, $class); | ||
| 52 | } | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 localize STRING | ||
| 57 | |||
| 58 | Calls localize function with provided STRING. The result is | ||
| 59 | returned if it contains non blank characters. Otherwise the | ||
| 60 | original STRING is returned. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub localize { | ||
| 65 | my ($self, $text) = @_; | ||
| 66 | my ($trans); | ||
| 67 | |||
| 68 | $trans = $self->{func}->($text); | ||
| 69 | |||
| 70 | if (defined $trans && $trans =~ /\S/) { | ||
| 71 | return $trans; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $text; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head1 AUTHOR | ||
| 78 | |||
| 79 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 80 | |||
| 81 | =head1 LICENSE AND COPYRIGHT | ||
| 82 | |||
| 83 | Copyright 2010-2016 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 84 | |||
| 85 | This program is free software; you can redistribute it and/or modify it | ||
| 86 | under the terms of either: the GNU General Public License as published | ||
| 87 | by the Free Software Foundation; or the Artistic License. | ||
| 88 | |||
| 89 | See http://dev.perl.org/licenses/ for more information. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | 1; |