blib/lib/Catalyst/Plugin/I18N/DBIC.pm

filter: Statementstmt 47.8% 11 / 23 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 37.5% 15 / 40 CC 5 SCAR 29.5
CC
5
Cov
34%
load_lexicon
30.0
BEGIN
1.0
BEGIN
1.0
CRAP 19.1
↑ Summary
1 package Catalyst::Plugin::I18N::DBIC;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base 'Catalyst::Plugin::I18N';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.04';
9  
10 0 ▶sub load_lexicon {
Subroutine load_lexicon: not called CC=5 SCAR=34.0
Pod documented
11 0 my ($c, @paths) = @_;
12  
13 0 ▶ my $class = ref $c || $c;
Condition0%0/3
ref $c || $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table0%0/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
14 0 my $lang = $c->language;
15  
16 0 my $where = {
17 language => $lang,
18 path => [@paths],
19 };
20  
21 0 ▶ my $lexicon_model = $c->config->{'I18N::DBIC'}{lexicon} || 'DBIC::Lexicon';
Condition0%0/2
$c->config->{'I18N::DBIC'}{'lexicon'} || 'DBIC::Lexicon'
l!l
00
MC/DC0%0/1
$c->config->{'I18N::DBIC'}{'lexicon'} || 'DBIC::Lexicon'
$c->config->{'I18N::DBIC'}{'lexicon'}
Truth table0%0/2
A
A= $c->config->{'I18N::DBIC'}{'lexicon'}
Aresult
11
00
22 0 my $lexicons_rs = $c->model($lexicon_model)->search($where);
23  
24 0 ▶ while (my $lex = $lexicons_rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lex = $lexicons_rs->next) 0 0 0
25 0 my $message = $lex->message;
26 0 my $value = $lex->value;
27  
28 0 eval <<"EOF";
29 \$$class\::I18N::$lang\::Lexicon{\$message} = \$value;
30 EOF
31 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
32 0 $c->log->error(qq/Couldn't write $class::I18N::$lang, "$@"/);
33 }
34 }
35 }
36  
37 1 1;
38  
39 =pod
40  
41 =head1 NAME
42  
43 Catalyst::Plugin::I18N::DBIC - Internationalization for Catalyst, data loaded
44 from database
45  
46 =head1 SYNOPSIS
47  
48 use Catalyst qw(-Debug I18N::DBIC);
49  
50 __PACKAGE__->config(
51 name => 'MyApp',
52 'I18N::DBIC' => {
53 lexicon_model => 'DBIC::MyLexicon',
54 },
55 );
56  
57 You can load the lexicon in your controller.
58  
59 $c->languages( ['de'] );
60 $c->load_lexicon( qw(footer header user/list navigation) );
61 print $c->localize('Hello Catalyst');
62  
63 Or in your template
64  
65 [% c.load_lexicon('header', 'navigation') %]
66  
67 [% c.loc('Home Page') %]
68 [% c.loc('Welcome to Catalyst') %]
69  
70 =head1 DESCRIPTION
71  
72 This module is based on L<Catalyst::Plugin::I18N> and L<I18N> and you should
73 refer to those modules for further information.
74  
75 These modules hold their localization data in files (mo, po or pm files) and
76 for a very large application these files can become very large and difficult
77 to maintain.
78  
79 L<Catalyst::Plugin::I18N::DBIC> however allows you to hold the localization
80 data in a database (using L<Catalyst::Model::DBIC::Schema> ) which has
81 several advantages.
82  
83 =over 4
84  
85 =item
86  
87 The localization data can be split into several 'paths' which represent the
88 natural organization of the application. e.g. 'footer', 'header', 'navigation',
89 'user/list'.
90  
91 =item
92  
93 You can write an application that directly modifies the database so that
94 your translators can do their stuff more easily and directly.
95  
96 =item
97  
98 If you have a client that requires custom text it is easier to do this by
99 making a database change than by releasing a new text file.
100  
101 =back
102  
103 =head1 EXTENDED METHODS
104  
105 =head2 load_lexicon
106  
107 Takes an array of 'paths' which should be searched to load the Lexicon data
108 from the database.
109  
110 It is more efficient in database requests to request all paths that may be
111 used on a page in one go. It may however be more convenient to make several
112 requests if you include templates in other templates (such as header and
113 footer templates) and make separate calls in each template.
114  
115 =head1 Database Schema
116  
117 The module requires a table called C<lexicon> with the following structure
118  
119 CREATE TABLE lexicon (
120 id int(11) NOT NULL auto_increment,
121 language varchar(15) default NULL,
122 path varchar(255) default NULL,
123 message text,
124 value text,
125 notes text,
126 PRIMARY KEY (id)
127 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
128  
129 By default the table C<lexicon> is used if you don't specify the
130 lexicon_model in the config. If you use an alternative table you must still
131 use the same structure.
132  
133 Actually you may want to change the index method and the 'notes' field is not
134 required but can be useful to hold information to help the translator put the
135 message in context.
136  
137 The C<value> is the tranlated C<message>. The C<path> is the context where
138 the message is used. For example you may wish to group all the menu button
139 text and navigation text into the C<navigation> path. All the text for the
140 generic header template could be in the C<header> path etc.
141  
142 =head1 SEE ALSO
143  
144 Refer to L<Catalyst::Plugin::I18N> for information on the other methods used.
145  
146 =head1 TO-DO
147  
148 =over 4
149  
150 =item Implement caching methods on-demand or pre-load
151 =item Reduce name clashes for text by making use of the path data
152  
153 =back
154  
155 =head1 AUTHOR
156  
157 Ian Docherty, C<cpan@iandocherty.com>
158  
159 With thanks to Kazuma Shiraiwa, Brian Cassidy and others for feedback and advice.
160  
161 =head1 COPYRIGHT & LICENSE
162  
163 Copyright (c) 2005 the aforementioned authors. All rights
164 reserved. This program is free software; you can redistribute
165 it and/or modify it under the same terms as Perl itself.
166  
167 =cut
↑ Summary