File Coverage

blib/lib/HON/I18N/Converter.pm
Criterion Covered Total %
statement 83 83 100.0
branch 10 12 83.3
condition n/a
subroutine 15 16 93.7
pod 1 6 16.6
total 109 117 93.1


line stmt bran cond sub pod time code
1             package HON::I18N::Converter;
2              
3 6     6   135352 use 5.006;
  6         24  
4 6     6   30 use strict;
  6         10  
  6         121  
5 6     6   29 use warnings;
  6         12  
  6         160  
6              
7 6     6   5288 use Encode;
  6         69587  
  6         563  
8 6     6   7775 use Spreadsheet::ParseExcel;
  6         422222  
  6         208  
9 6     6   6854 use JSON::XS;
  6         42272  
  6         396  
10 6     6   3145 use IO::All -utf8;
  6         46674  
  6         61  
11 6     6   404 use Carp;
  6         13  
  6         456  
12              
13             =head1 NAME
14              
15             HON::I18N::Converter - perl I18N Converter
16              
17             =head1 VERSION
18              
19             Version 0.02
20              
21             =cut
22              
23             our $VERSION = '0.02';
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     6   7635 use Object::InsideOut;
  6         309190  
  6         38  
43              
44             my @workbook : Field : Acc( 'Name' => 'workbook' );
45              
46             #Tableau labels
47             my @labels : Field : Type('Hash') : Acc( 'Name' => 'labels' );
48              
49             #Table de hachage init_args
50             my %init_args : InitArgs = (
51             'EXCEL' => {
52             Regex => qr/^excel$/i,
53             Mandatory => 1,
54             Type => 'Scalar',
55             },
56             );
57              
58             sub init : Init {
59 9     0 0 18896 my ( $self, $args ) = @_;
60              
61 9         268 $self->labels( {} );
62              
63 9         152 my $parser = Spreadsheet::ParseExcel->new();
64 9         1970 $self->workbook( $parser->parse( $args->{EXCEL} ) );
65              
66 9 100       155505 if ( !defined $self->workbook ) {
67 1         12 die $parser->error(), ".\n";
68             }
69 6     6   1559 }
  6         12  
  6         28  
70              
71             #Retourne le tableau contenant la liste des langues
72             sub p_getLanguage {
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     7 0 620 my ($self) = shift;
77              
78             #Declaration tableau vide
79 7         18 my @line = ();
80              
81 7         175 for my $worksheet ( $self->workbook->worksheets() ) {
82 21         188 my ( $col_min, $col_max ) = $worksheet->col_range();
83              
84             #Recuperation des cellules de la premiere ligne (ligne correspondant a la langue)
85 21         177 for my $col ( $col_min .. $col_max ) {
86              
87             #Valeur de la cellule
88 20         121 my $cell = $worksheet->get_cell( 0, $col );
89              
90             #Va a la prochaine cellule sauf si la cellule est vide
91 20 50       222 next unless $cell;
92              
93             #Push permet d'ajouter une liste de valeurs scalaires au tableau @line
94 20         72 push @line, $cell->value();
95             }
96             }
97              
98             #Retourne le tableau contenant la liste des langues
99 7         36 return @line;
100             }
101              
102             sub p_buildHash {
103 7     7 0 24 my ( $self, $languages ) = @_;
104              
105             #Parcours ligne par ligne
106             #Colonne par colonne
107 7         165 for my $worksheet ( $self->workbook->worksheets() ) {
108              
109 21         318 my ( $row_min, $row_max ) = $worksheet->row_range();
110 21         174 my ( $col_min, $col_max ) = $worksheet->col_range();
111              
112 21         180 for my $row ( 1 .. $row_max ) {
113              
114 14         278 my $label;
115              
116 14         29 for my $col ( $col_min .. $col_max ) {
117 40         492 my $cell = $worksheet->get_cell( $row, $col );
118 40 50       436 next unless $cell;
119              
120 40 100       86 if ( $col == 0 ) {
121 14         45 $label = $cell->value();
122             }
123             else {
124 26         70 $self->labels->{ $languages->[$col] }->{$label} = $cell->value();
125             }
126             }
127             }
128             }
129 7         18 return;
130             }
131              
132             #Fonction valable pour le javascript
133             sub p_write_JS_i18n {
134 2     2 0 4 my ( $self, $folder, $header ) = @_;
135              
136             #En tete du fichier jQuery
137 2         8 my $content = $header . "(function(\$){\n";
138              
139             #Pour encodage
140 2         35 my $encoder = JSON::XS->new->ascii->pretty->allow_nonref;
141              
142             #Parcours d'une table de hachage
143 2         4 foreach my $lang ( keys %{ $self->labels } ) {
  2         44  
144              
145 4         92 my $json = $encoder->encode( { strings => $self->labels->{$lang} } );
146              
147             #Intitule de chaque section
148 4         68 $content .= "\$.i18n.$lang = $json;\n";
149             }
150              
151             #Derniere ligne du document jQuery
152 2         4 $content .= '})(jQuery);';
153              
154 2         45 $content > io( $folder . '/jQuery-i18n.js' );
155 2         12728 return;
156             }
157              
158             #Fonction valable pour le.ini
159             sub p_write_INI_i18n {
160 2     2 0 4 my ( $self, $folder, $header ) = @_;
161              
162 2         3 foreach my $lang ( keys %{ $self->labels } ) {
  2         45  
163 4         1846 my $content = $header;
164 4         7 foreach my $LAB ( keys %{ $self->labels->{$lang} } ) {
  4         101  
165 8         222 $content .= ( $LAB . q{=} . $self->labels->{$lang}->{$LAB} . "\n" );
166             }
167 4         76 $content > io( $folder . q{/} . $lang . '.ini' );
168             }
169 2         13601 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             sub build_properties_file {
181 5     5 1 335 my ( $self, $format, $folder, $header ) = @_;
182 5         26 my @languges = $self->p_getLanguage();
183 5         25 $self->p_buildHash( \@languges );
184              
185 5 100       23 if ( $format eq 'JS' ) {
    100          
186 2         8 return $self->p_write_JS_i18n( $folder, $header );
187             }
188             elsif ( $format eq 'INI' ) {
189 2         10 return $self->p_write_INI_i18n( $folder, $header );
190             }
191             else {
192 1         22 croak 'Unknown format';
193             }
194             }
195             }
196              
197             =head1 AUTHOR
198              
199             Samia Chahlal, C<< >>
200              
201             =head1 BUGS AND LIMITATIONS
202              
203             Please report any bugs or feature requests to C, or through
204             the web interface at L. 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
221              
222             =item * AnnoCPAN: Annotated CPAN documentation
223              
224             L
225              
226             =item * CPAN Ratings
227              
228             L
229              
230             =item * Search CPAN
231              
232             L
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             1; # End of HON::I18N::Converter