| 1 | # ============================================================================ | ||||||||||
| 2 | package CatalystX::I18N::Model::Maketext; | ||||||||||
| 3 | # ============================================================================ | ||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | extends 'CatalystX::I18N::Model::Base'; | |||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use CatalystX::I18N::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use Scalar::Util qw(weaken); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 4 | has 'gettext_style' => ( | |||||||||
| 14 | is => 'rw', | ||||||||||
| 15 | isa => 'Bool', | ||||||||||
| 16 | default => 1, | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 3 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||
| 20 | 3 | my ($self) = @_; | |||||||||
| 21 | |||||||||||
| 22 | 3 | my $class = $self->class; | |||||||||
| 23 | |||||||||||
| 24 | # Load Maketext class | ||||||||||
| 25 | 3 | my ($ok,$error) = Class::Load::try_load_class($class); | |||||||||
| 26 | 3 | ▶ | Catalyst::Exception->throw(sprintf("Could not load '%s' : %s",$class,$error)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | unless $ok; | ||||||||||
| 28 | |||||||||||
| 29 | 3 | ▶ | Catalyst::Exception->throw(sprintf("Could initialize '%s' because is is not a 'Locale::Maketext' class",$class)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | unless $class->isa('Locale::Maketext'); | ||||||||||
| 31 | |||||||||||
| 32 | 3 | my $app = $self->_app; | |||||||||
| 33 | |||||||||||
| 34 | # Load lexicons in the Maketext class if possible | ||||||||||
| 35 | 3 | ▶ | if ($class->can('load_lexicon')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 3 | my (@locales,%inhertiance,$config); | |||||||||
| 37 | 3 | $config = $app->config->{I18N}{locales}; | |||||||||
| 38 | 3 | foreach my $locale (keys %$config) { | |||||||||
| 39 | 18 | push(@locales,$locale); | |||||||||
| 40 | $inhertiance{$locale} = $config->{$locale}{inherits} | ||||||||||
| 41 | 18 | ▶ | if defined $config->{$locale}{inherits}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | 3 | ▶ | $app->log->debug(sprintf("Loading maketext lexicons for locales %s",join(',',@locales))) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | if $app->debug; | ||||||||||
| 45 | |||||||||||
| 46 | 3 | $class->load_lexicon( | |||||||||
| 47 | locales => \@locales, | ||||||||||
| 48 | directories => $self->directories, | ||||||||||
| 49 | gettext_style => $self->gettext_style, | ||||||||||
| 50 | inheritance => \%inhertiance, | ||||||||||
| 51 | ); | ||||||||||
| 52 | } else { | ||||||||||
| 53 | 0 | $app->log->warn(sprintf("'%s' does not implement a 'load_lexicon' method",$class)) | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 3 | return; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 30 | ▶ | sub ACCEPT_CONTEXT { | ||||||||
|
Subroutine
ACCEPT_CONTEXT: called CC=3 SCAR=12.6
Pod
documented
| |||||||||||
| 60 | 30 | my ( $self, $c ) = @_; | |||||||||
| 61 | |||||||||||
| 62 | # set locale and fallback | ||||||||||
| 63 | 30 | my $handle = $self->class->get_handle( $c->locale ); | |||||||||
| 64 | |||||||||||
| 65 | # Catch error | ||||||||||
| 66 | 30 | ▶ | Catalyst::Exception->throw(sprintf("Could not fetch lanuage handle for locale '%s'",$c->locale)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | unless ( scalar $handle ); | ||||||||||
| 68 | |||||||||||
| 69 | 30 | ▶ | if ($self->can('fail_with')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | weaken($c); | |||||||||
| 71 | 0 | ▶ | $handle->fail_with( sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 72 | 0 | $self->fail_with($c,@_); | |||||||||
| 73 | 0 | } ); | |||||||||
| 74 | } else { | ||||||||||
| 75 | 30 | ▶ | $handle->fail_with( sub { } ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 30 | return $handle; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 4 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | |||||||||
| 82 | 4 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 4 | 1; | |||||||||
| 84 | |||||||||||
| 85 | =encoding utf8 | ||||||||||
| 86 | |||||||||||
| 87 | =head1 NAME | ||||||||||
| 88 | |||||||||||
| 89 | CatalystX::I18N::Model::Maketext - Glues Locale::Maketext into Catalyst | ||||||||||
| 90 | |||||||||||
| 91 | =head1 SYNOPSIS | ||||||||||
| 92 | |||||||||||
| 93 | # In your catalyst base class | ||||||||||
| 94 | package MyApp::Catalyst; | ||||||||||
| 95 | use Catalyst qw/CatalystX::I18N::Role::Base/; | ||||||||||
| 96 | |||||||||||
| 97 | __PACKAGE__->config( | ||||||||||
| 98 | 'Model::Maketext' => { | ||||||||||
| 99 | class => 'MyApp::Maketext', # optional | ||||||||||
| 100 | directories => '/path/to/maketext/files', # optional | ||||||||||
| 101 | }, | ||||||||||
| 102 | ); | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | # Create a model class | ||||||||||
| 106 | package MyApp::Model::Maketext; | ||||||||||
| 107 | use parent qw/CatalystX::I18N::Model::Maketext/; | ||||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | # Create a Maketext class (must be a Locale::Maketext class) | ||||||||||
| 111 | package MyApp::Maketext; | ||||||||||
| 112 | use parent qw/CatalystX::I18N::Maketext/; | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | # In your controller class(es) | ||||||||||
| 116 | package MyApp::Controller::Main; | ||||||||||
| 117 | use parent qw/Catalyst::Controller/; | ||||||||||
| 118 | |||||||||||
| 119 | sub action : Local { | ||||||||||
| 120 | my ($self,$c) = @_; | ||||||||||
| 121 | |||||||||||
| 122 | my $model = $c->model('Maketext'); | ||||||||||
| 123 | $c->stash->{title} = $model->maketext('Hello world'); | ||||||||||
| 124 | # See CatalystX::I18N::Role::Maketext for a convinient wrapper | ||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | =head1 DESCRIPTION | ||||||||||
| 128 | |||||||||||
| 129 | This model glues a L<Locale::Maketext> class | ||||||||||
| 130 | (eg. L<CatalystX::I18N::Maketext>) into you Catalyst application. | ||||||||||
| 131 | |||||||||||
| 132 | The method C<fail_with> will be called for each missing msgid if present | ||||||||||
| 133 | in your model class. | ||||||||||
| 134 | |||||||||||
| 135 | package MyApp::Model::Maketext; | ||||||||||
| 136 | use parent qw/CatalystX::I18N::Model::Maketext/; | ||||||||||
| 137 | |||||||||||
| 138 | sub fail_with { | ||||||||||
| 139 | my ($self,$c,$language_handle,$msgid,$params) = @_; | ||||||||||
| 140 | # Do somenthing clever | ||||||||||
| 141 | return $string; | ||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | See L<Catalyst::Helper::Model::Maketext> for gerating an Maketext model from | ||||||||||
| 145 | the command-line. | ||||||||||
| 146 | |||||||||||
| 147 | =head1 CONFIGURATION | ||||||||||
| 148 | |||||||||||
| 149 | =head3 class | ||||||||||
| 150 | |||||||||||
| 151 | Set the L<Locale::Maketext> class you want to use from this model. | ||||||||||
| 152 | |||||||||||
| 153 | Defaults to $APPNAME::Maketext | ||||||||||
| 154 | |||||||||||
| 155 | =head3 gettext_style | ||||||||||
| 156 | |||||||||||
| 157 | Enable gettext style. C<%quant(%1,document,documents)> instead of | ||||||||||
| 158 | C<[quant,_1,document,documents]> | ||||||||||
| 159 | |||||||||||
| 160 | Default TRUE | ||||||||||
| 161 | |||||||||||
| 162 | =head3 directories | ||||||||||
| 163 | |||||||||||
| 164 | Array reference of directories to be searched for maketext files. | ||||||||||
| 165 | |||||||||||
| 166 | See L<CatalystX::I18N::Maketext> for more details on the C<directory> | ||||||||||
| 167 | parameter | ||||||||||
| 168 | |||||||||||
| 169 | =head1 SEE ALSO | ||||||||||
| 170 | |||||||||||
| 171 | L<CatalystX::I18N::Maketext>, L<Locale::Maketext>, | ||||||||||
| 172 | L<Locale::Maketext::Lexicon> and L<CatalystX::I18N::Role::Maketext> | ||||||||||
| 173 | |||||||||||
| 174 | =head1 AUTHOR | ||||||||||
| 175 | |||||||||||
| 176 | Maroš Kollár | ||||||||||
| 177 | CPAN ID: MAROS | ||||||||||
| 178 | maros [at] k-1.com | ||||||||||
| 179 | |||||||||||
| 180 | L<http://www.k-1.com> | ||||||||||