| 1 | # $Id: Translate.pm 542 2013-12-12 16:36:34Z oetiker $ | ||||||||||||||||||||||||||||
| 2 | package CallBackery::Translate; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use CallBackery::Exception qw(mkerror); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use vars qw(@EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | @EXPORT_OK = qw(trm); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | CallBackery::Translate - gettext po file translation functionality | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use CallBackery::Translate qw(mtr); | ||||||||||||||||||||||||||||
| 20 | my $loc = CallBackery::Translate::new(localeRoot=>$dir); | ||||||||||||||||||||||||||||
| 21 | $loc->setLocale('de'); | ||||||||||||||||||||||||||||
| 22 | $loc->tra("Hello %1","Tobi"); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | trm("Mark but for translation but return original"); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Read translations from gettext po files and translate incoming data. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | has 'localeRoot'; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =over | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item C<setLocale>($locale); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Load the translations strings for $locale. First try the full name and then top-up with only the language part. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | my %lx; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub setLocale { | ||||||||||||||||||||||||||
|
Subroutine
setLocale: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | 0 | my $locale = shift; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($lx{$locale}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $self->{_lx} = $lx{$locale}; | |||||||||||||||||||||||||||
| 49 | 0 | return; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | my $lang = $locale; | |||||||||||||||||||||||||||
| 52 | 0 | $lang =~ s/_.+//; | |||||||||||||||||||||||||||
| 53 | 0 | for my $file ($lang,$locale){ | |||||||||||||||||||||||||||
| 54 | 0 | my $mode = 'id'; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (open my $fh, '< :encoding(utf8)', $self->localeRoot.'/'.$file.'.po'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $key; | |||||||||||||||||||||||||||
| 57 | my %var; | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | while (<$fh>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | chomp; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | /^msg(id|str)\s+"(.*)"/ && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | $var{$1} = $2; | |||||||||||||||||||||||||||
| 62 | 0 | $key = $1; | |||||||||||||||||||||||||||
| 63 | 0 | next; | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | /^"(.*)"/ && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $var{$key} .= $1; | |||||||||||||||||||||||||||
| 67 | 0 | next; | |||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | /^\s*$/ && $var{id} && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^\s*$/u and $var{"id"}
MC/DC0%0/2
/^\s*$/u and $var{"id"}
/^\s*$/u
$var{"id"}
Truth table0%0/3
A and B
A= /^\s*$/uB= $var{"id"}
| |||||||||||||||||||||||||||||
| 70 | 0 | $lx{$locale}{$var{id}} = $var{str}; | |||||||||||||||||||||||||||
| 71 | 0 | next; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | $self->{_lx} = $lx{$locale}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item C<tra>(str[,arg,arg,...]) | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Translate string into the curent language. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub tra { | ||||||||||||||||||||||||||
|
Subroutine
tra: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 88 | 0 | my @args = @_; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $lx = $self->{_lx} // {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"_lx"} // {}
MC/DC0%0/1
$self->{"_lx"} // {}
$self->{"_lx"}
Truth table0%0/2
A
A= $self->{"_lx"}
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $str = $lx->{$str} if $lx->{$str}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | my $id = 1; | |||||||||||||||||||||||||||
| 92 | 0 | for my $a (@args){ | |||||||||||||||||||||||||||
| 93 | 0 | $str =~ s/%$id/$a/g; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | return $str; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item C<trm>(str[,arg,arg,...]) | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | mark for translation but return an array pointer so that the string can be translated | ||||||||||||||||||||||||||||
| 101 | dynamically in the forntend. I<This functionality is not yet fully implemented>. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # trm("Hello %1",$name); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 9 | ▶ | sub trm { | ||||||||||||||||||||||||||
|
Subroutine
trm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 9 | return pop @_; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||