| 1 | # ============================================================================ | ||||||||||||
| 2 | package CatalystX::I18N::Role::Maketext; | ||||||||||||
| 3 | # ============================================================================ | ||||||||||||
| 4 | |||||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 30 | ▶ | sub maketext { | ||||||||||
|
Subroutine
maketext: called CC=5 SCAR=19.7
Pod
documented
| |||||||||||||
| 9 | 30 | my ($c,$msgid,@args) = @_; | |||||||||||
| 10 | |||||||||||||
| 11 | 30 | my @args_expand; | |||||||||||
| 12 | 30 | foreach my $arg (@args) { | |||||||||||
| 13 | 30 | ▶ | push @args_expand, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 14 | (ref $arg eq 'ARRAY') ? @$arg : $arg; | ||||||||||||
| 15 | } | ||||||||||||
| 16 | |||||||||||||
| 17 | # TODO: Check if Maketext model is available | ||||||||||||
| 18 | 30 | my $handle = $c->model('Maketext'); | |||||||||||
| 19 | 30 | my $msgstr = $handle->maketext( $msgid, @args_expand ); | |||||||||||
| 20 | |||||||||||||
| 21 | 30 | ▶ | return $msgstr | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | if defined $msgstr; | ||||||||||||
| 23 | |||||||||||||
| 24 | # Method expansion | ||||||||||||
| 25 | 0 | ▶ | my $replacesub = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 26 | 0 | my $method = shift; | |||||||||||
| 27 | 0 | my @params = split(/,/,shift); | |||||||||||
| 28 | 0 | ▶ | if ($handle->can($method)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 0 | return $handle->$method(@params); | |||||||||||
| 30 | } | ||||||||||||
| 31 | 0 | return $method; | |||||||||||
| 32 | 4 | }; | |||||||||||
| 33 | |||||||||||||
| 34 | # TODO: use gettext/maketext style | ||||||||||||
| 35 | 4 | $msgstr = $msgid; | |||||||||||
| 36 | 0 | ▶ | $msgstr =~s{%(\d+)}{ $args[$1-1] || 'missing value %'.$1 }eg; | ||||||||||
|
Condition0%0/2
$args[$1 - 1] || 'missing value %' . $1
MC/DC0%0/1
$args[$1 - 1] || 'missing value %' . $1
$args[$1 - 1]
Truth table0%0/2
A
A= $args[$1 - 1]
| |||||||||||||
| 37 | 0 | $msgstr =~s/%(\w+)\(([^)]+)\)/$replacesub->($1,$2)/eg; | |||||||||||
| 38 | |||||||||||||
| 39 | 4 | return $msgstr; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 4 | ▶ | no Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 4 | 1; | |||||||||||
| 44 | |||||||||||||
| 45 | =encoding utf8 | ||||||||||||
| 46 | |||||||||||||
| 47 | =head1 NAME | ||||||||||||
| 48 | |||||||||||||
| 49 | CatalystX::I18N::Role::Maketext - Support for maketext | ||||||||||||
| 50 | |||||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||||
| 52 | |||||||||||||
| 53 | # In your catalyst base class | ||||||||||||
| 54 | package MyApp::Catalyst; | ||||||||||||
| 55 | |||||||||||||
| 56 | use Catalyst qw/MyPlugins | ||||||||||||
| 57 | CatalystX::I18N::Role::Base | ||||||||||||
| 58 | CatalystX::I18N::Role::Maketext/; | ||||||||||||
| 59 | |||||||||||||
| 60 | # Maketext model class | ||||||||||||
| 61 | package MyApp::Model::Maketext; | ||||||||||||
| 62 | use parent qw/CatalystX::I18N::Model::Maketext/; | ||||||||||||
| 63 | |||||||||||||
| 64 | # Create a Maketext class (must be a Locale::Maketext class) | ||||||||||||
| 65 | package MyApp::Maketext; | ||||||||||||
| 66 | use parent qw/CatalystX::I18N::Maketext/; | ||||||||||||
| 67 | |||||||||||||
| 68 | # In your controller class(es) | ||||||||||||
| 69 | package MyApp::Catalyst::Controller::Main; | ||||||||||||
| 70 | use strict; | ||||||||||||
| 71 | use warnings; | ||||||||||||
| 72 | use parent qw/Catalyst::Controller/; | ||||||||||||
| 73 | |||||||||||||
| 74 | sub action : Local { | ||||||||||||
| 75 | my ($self,$c) = @_; | ||||||||||||
| 76 | |||||||||||||
| 77 | $c->stash->{results} = $c->maketext('Your search found %quant(%1,result,results)',$count); | ||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | =head1 DESCRIPTION | ||||||||||||
| 81 | |||||||||||||
| 82 | This role adds support for L<Locale::Maketext> localisation via the | ||||||||||||
| 83 | L<CatalystX::I18N::Model::Maketext> model. | ||||||||||||
| 84 | |||||||||||||
| 85 | In order to work properly this role needs a model called C<Maketext>. A call | ||||||||||||
| 86 | to C<$c-E<gt>model('Maketext')> should return a handle for a Maketext / | ||||||||||||
| 87 | L<Locale::Maketext> class. You can either write your own Model and Maketext | ||||||||||||
| 88 | class or use L<CatalystX::I18N::Model::Maketext> and | ||||||||||||
| 89 | L<CatalystX::I18N::Maketext>. | ||||||||||||
| 90 | |||||||||||||
| 91 | =head1 METHODS | ||||||||||||
| 92 | |||||||||||||
| 93 | =head3 maketext | ||||||||||||
| 94 | |||||||||||||
| 95 | my $translated_string = $c->maketext($msgid,@params); | ||||||||||||
| 96 | OR | ||||||||||||
| 97 | my $translated_string = $c->maketext($msgid,\@params); | ||||||||||||
| 98 | |||||||||||||
| 99 | Translates a string via L<Locale::Maketext>. | ||||||||||||
| 100 | |||||||||||||
| 101 | =head1 SEE ALSO | ||||||||||||
| 102 | |||||||||||||
| 103 | L<Locale::Maketext>, L<CatalystX::I18N::Model::Maketext> | ||||||||||||
| 104 | and L<CatalystX::I18N::Maketext> | ||||||||||||
| 105 | |||||||||||||
| 106 | =head1 AUTHOR | ||||||||||||
| 107 | |||||||||||||
| 108 | Maroš Kollár | ||||||||||||
| 109 | CPAN ID: MAROS | ||||||||||||
| 110 | maros [at] k-1.com | ||||||||||||
| 111 | |||||||||||||
| 112 | L<http://www.k-1.com> | ||||||||||||