lib/HON/I18N/Converter.pm

filter: Statementstmt 100% 89 / 89 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod 20.0% 1 / 5 totaltotal 95.0% 116 / 122 CC 15 SCAR 10.8
CC
15
Cov
98%
p_buildHash
6.0
p_getLanguage
4.0
p_write_INI_i18n
3.0
CRAP 2.9
↑ Summary script/build-properties-INI-file.pl »
1 package HON::I18N::Converter;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Spreadsheet::ParseExcel;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use IO::All -utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 HON::I18N::Converter - perl I18N Converter
16  
17 =head1 VERSION
18  
19 Version 0.03
20  
21 =cut
22  
23 6 our $VERSION = '0.03';
24  
25 =head1 SYNOPSIS
26  
27 Convert Excel (2003) i18n file to another format
28  
29 use HON::I18N::Converter;
30  
31 my $converter = HON::I18N::Converter->new( excel => 'path/to/my/file.xls' );
32 $converter->build_properties_file('INI', 'destination/folder/', $comment);
33 ...
34  
35 =head1 DESCRIPTION
36  
37 perl I18N Converter
38  
39 =cut
40  
41 {
42 6 ▶ use Object::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 6 my @workbook : Field : Acc( 'Name' => 'workbook' );
45  
46 #Tableau labels
47 6 my @labels : Field : Type('Hash') : Acc( 'Name' => 'labels' );
48  
49 #Table de hachage init_args
50 6 my %init_args : InitArgs = (
51 'EXCEL' => {
52 Regex => qr/^excel$/i,
53 Mandatory => 1,
54 Type => 'Scalar',
55 },
56 );
57  
58 9 ▶ sub init : Init {
Subroutine init: called CC=2 SCAR=6.9
59 9 my ( $self, $args ) = @_;
60  
61 9 $self->labels( {} );
62  
63 9 my $parser = Spreadsheet::ParseExcel->new();
64 9 $self->workbook( $parser->parse( $args->{EXCEL} ) );
65  
66 9 ▶ if ( !defined $self->workbook ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->workbook) 1 8 9
67 1 die $parser->error(), ".\n";
68 }
69 6 ▶ }
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 #Retourne le tableau contenant la liste des langues
72 7 ▶ sub p_getLanguage {
Subroutine p_getLanguage: called CC=4 SCAR=13.9
Pod undocumented
73  
74 #La fonction shift prend un tableau en argument; elle supprime son premier element
75 #(les autres sont alors decales) et renvoie cet element.
76 7 my ($self) = shift;
77  
78 #Declaration tableau vide
79 7 my @line = ();
80  
81 7 for my $worksheet ( $self->workbook->worksheets() ) {
82 21 my ( $col_min, $col_max ) = $worksheet->col_range();
83  
84 #Recuperation des cellules de la premiere ligne (ligne correspondant a la langue)
85 21 for my $col ( $col_min .. $col_max ) {
86  
87 #Valeur de la cellule
88 20 my $cell = $worksheet->get_cell( 0, $col );
89  
90 #Va a la prochaine cellule sauf si la cellule est vide
91 20 ▶ next unless $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless $cell 0 20 20
92  
93 #Push permet d'ajouter une liste de valeurs scalaires au tableau @line
94 20 push @line, $cell->value();
95 }
96 }
97  
98 #Retourne le tableau contenant la liste des langues
99 7 return @line;
100 }
101  
102 7 ▶ sub p_buildHash {
Subroutine p_buildHash: called CC=6 SCAR=17.9
Pod undocumented
103 7 my ( $self, $languages ) = @_;
104  
105 #Parcours ligne par ligne
106 #Colonne par colonne
107 7 for my $worksheet ( $self->workbook->worksheets() ) {
108  
109 21 my ( $row_min, $row_max ) = $worksheet->row_range();
110 21 my ( $col_min, $col_max ) = $worksheet->col_range();
111  
112 21 for my $row ( 1 .. $row_max ) {
113  
114 14 my $label;
115  
116 14 for my $col ( $col_min .. $col_max ) {
117 40 my $cell = $worksheet->get_cell( $row, $col );
118 40 ▶ next unless $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless $cell 0 40 40
119  
120 40 ▶ if ( $col == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col == 0) { } 14 26 40
121 14 $label = $cell->value();
122 }
123 else {
124 26 $self->labels->{ $languages->[$col] }->{$label} = $cell->value();
125 }
126 }
127 }
128 }
129 7 return;
130 }
131  
132 #Fonction valable pour le javascript
133 2 ▶ sub p_write_JS_i18n {
Subroutine p_write_JS_i18n: called CC=2 SCAR=6.9
Pod undocumented
134 2 my ( $self, $folder, $header ) = @_;
135  
136 #En tete du fichier jQuery
137 2 my $content = $header . "(function(\$){\n";
138  
139 #Pour encodage
140 2 my $encoder = JSON::XS->new->ascii->pretty->allow_nonref;
141  
142 #Parcours d'une table de hachage
143 2 foreach my $lang ( keys %{ $self->labels } ) {
144  
145 4 my $json = $encoder->encode( { strings => $self->labels->{$lang} } );
146  
147 #Intitule de chaque section
148 4 $content .= "\$.i18n.$lang = $json;\n";
149 }
150  
151 #Derniere ligne du document jQuery
152 2 $content .= '})(jQuery);';
153  
154 2 $content > io( $folder . '/jQuery-i18n.js' );
155 2 return;
156 }
157  
158 #Fonction valable pour le.ini
159 2 ▶ sub p_write_INI_i18n {
Subroutine p_write_INI_i18n: called CC=3 SCAR=11.0
Pod undocumented
160 2 my ( $self, $folder, $header ) = @_;
161  
162 2 foreach my $lang ( keys %{ $self->labels } ) {
163 4 my $content = $header;
164 4 foreach my $LAB ( keys %{ $self->labels->{$lang} } ) {
165 8 $content .= ( $LAB . q{=} . $self->labels->{$lang}->{$LAB} . "\n" );
166 }
167 4 $content > io( $folder . q{/} . $lang . '.ini' );
168 }
169 2 return;
170 }
171  
172 =head1 SUBROUTINES/METHODS
173  
174 =head2 $self->build_properties_file()
175  
176 Convert Excel file to INI or Jquery i18n plugin
177  
178 =cut
179  
180 5 ▶ sub build_properties_file {
Subroutine build_properties_file: called CC=3 SCAR=11.0
Pod documented
181 5 my ( $self, $format, $folder, $header ) = @_;
182 5 my @languges = $self->p_getLanguage();
183 5 $self->p_buildHash( \@languges );
184  
185 5 ▶ if ( $format eq 'JS' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'JS') { } 2 3 5
elsif ($format eq 'INI') { } 2 1 3
186 2 return $self->p_write_JS_i18n( $folder, $header );
187 }
188 elsif ( $format eq 'INI' ) {
189 2 return $self->p_write_INI_i18n( $folder, $header );
190 }
191 else {
192 1 croak 'Unknown format';
193 }
194 }
195 }
196  
197 =head1 AUTHOR
198  
199 Samia Chahlal, C<< <samia.chahlal at yahoo.com> >>
200  
201 =head1 BUGS AND LIMITATIONS
202  
203 Please report any bugs or feature requests to C<bug-hon-i18n-converter at rt.cpan.org>, or through
204 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HON-I18N-Converter>. I will be notified, and then you'll
205 automatically be notified of progress on your bug as I make changes.
206  
207 =head1 SUPPORT
208  
209 You can find documentation for this module with the perldoc command.
210  
211 perldoc HON::I18N::Converter
212  
213  
214 You can also look for information at:
215  
216 =over 4
217  
218 =item * RT: CPAN's request tracker (report bugs here)
219  
220 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HON-I18N-Converter>
221  
222 =item * AnnoCPAN: Annotated CPAN documentation
223  
224 L<http://annocpan.org/dist/HON-I18N-Converter>
225  
226 =item * CPAN Ratings
227  
228 L<http://cpanratings.perl.org/d/HON-I18N-Converter>
229  
230 =item * Search CPAN
231  
232 L<http://search.cpan.org/dist/HON-I18N-Converter/>
233  
234 =back
235  
236  
237 =head1 ACKNOWLEDGEMENTS
238  
239  
240 =head1 LICENSE AND COPYRIGHT
241  
242 Copyright 2013 Samia Chahlal.
243  
244 This program is free software; you can redistribute it and/or modify it
245 under the terms of either: the GNU General Public License as published
246 by the Free Software Foundation; or the Artistic License.
247  
248 See http://dev.perl.org/licenses/ for more information.
249  
250 =cut
251  
252 6 1; # End of HON::I18N::Converter
↑ Summary script/build-properties-INI-file.pl »