fixing help feature with new template structure
[koha.git] / C4 / Languages.pm
1 package C4::Languages;
2
3 # Copyright 2006 (C) LibLime
4 # Joshua Ferraro <jmf@liblime.com>
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 # $Id$
22
23 use strict; use warnings; #FIXME: turn off warnings before release
24 require Exporter;
25 use C4::Context;
26 use vars qw($VERSION @ISA @EXPORT);
27
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
29
30 =head1 NAME
31
32 C4::Languages - Perl Module containing language list functions for Koha 
33
34 =head1 SYNOPSIS
35
36 use C4::Languages;
37
38 =head1 DESCRIPTION
39
40 =head1 FUNCTIONS
41
42 =cut
43
44 @ISA    = qw(Exporter);
45 @EXPORT = qw(
46   &getFrameworkLanguages
47   &getTranslatedLanguages
48   &getAllLanguages
49   );
50
51 my $DEBUG = 0;
52
53 =head2 getFrameworkLanguages
54
55 Returns a reference to an array of hashes:
56
57  my $languages = getFrameworkLanguages();
58  for my $language(@$languages) {
59     print "$language->{language_code}\n"; # language code in iso 639-2
60     print "$language->{language_name}\n"; # language name in native script
61     print "$language->{language_locale_name}\n"; # language name in current locale
62  }
63
64 =cut
65
66 sub getFrameworkLanguages {
67     # get a hash with all language codes, names, and locale names
68     my $all_languages = getAllLanguages();
69     my @languages;
70     
71     # find the available directory names
72     my $dir=C4::Context->config('intranetdir')."/installer/data/";
73     opendir (MYDIR,$dir);
74     my @listdir= grep { !/^\.|CVS/ && -d "$dir/$_"} readdir(MYDIR);    
75     closedir MYDIR;
76
77     # pull out all data for the dir names that exist
78     for my $dirname (@listdir) {
79         for my $language_set (@$all_languages) {
80             my $language_name = $language_set->{language_name};
81             my $language_locale_name = $language_set->{language_locale_name};
82
83             if ($dirname eq $language_set->{language_code}) {
84                 push @languages, {'language_code'=>$dirname, 'language_name'=>$language_name, 'language_locale_name'=>$language_locale_name}
85             }
86         }
87     }
88     return \@languages;
89 }
90
91 =head2 getTranslatedLanguages
92
93 Returns a reference to an array of hashes:
94
95  my $languages = getTranslatedLanguages();
96  print "Available translated langauges:\n";
97  for my $language(@$trlanguages) {
98     print "$language->{language_code}\n"; # language code in iso 639-2
99     print "$language->{language_name}\n"; # language name in native script
100     print "$language->{language_locale_name}\n"; # language name in current locale
101  }
102
103 =cut
104
105 sub getTranslatedLanguages {
106     my ($interface, $theme) = @_;
107     my $htdocs;
108     my $all_languages = getAllLanguages();
109     my @languages;
110     my $lang;
111     
112     if ( $interface eq 'opac' ) {
113         $htdocs = C4::Context->config('opachtdocs');
114         if ( $theme and -d "$htdocs/$theme" ) {
115             (@languages) = _get_language_dirs($htdocs,$theme);
116             return _get_final_languages($all_languages,@languages);
117         }
118         else {
119             for my $theme ( _get_themes('opac') ) {
120                 push @languages, _get_language_dirs($htdocs,$theme);
121             }
122             return _get_final_languages($all_languages,@languages);
123         }
124     }
125     elsif ( $interface eq 'intranet' ) {
126         $htdocs = C4::Context->config('intrahtdocs');
127         if ( $theme and -d "$htdocs/$theme" ) {
128             @languages = _get_language_dirs($htdocs,$theme);
129             return _get_final_languages($all_languages,@languages);
130         }
131         else {
132             foreach my $theme ( _get_themes('opac') ) {
133                 push @languages, _get_language_dirs($htdocs,$theme);
134             }
135             return _get_final_languages($all_languages,@languages);
136         }
137     }
138     else {
139         my $htdocs = C4::Context->config('intrahtdocs');
140         foreach my $theme ( _get_themes('intranet') ) {
141             push @languages, _get_language_dirs($htdocs,$theme);
142         }
143         $htdocs = C4::Context->config('opachtdocs');
144         foreach my $theme ( _get_themes('opac') ) {
145             push @languages, _get_language_dirs($htdocs,$theme);
146         }
147         return _get_final_languages($all_languages,@languages);
148     }
149 }
150
151 =head2 getAllLanguages
152
153 Returns a reference to an array of hashes:
154
155  my $alllanguages = getAllLanguages();
156  print "Available translated langauges:\n";
157  for my $language(@$alllanguages) {
158     print "$language->{language_code}\n";
159     print "$language->{language_name}\n";
160     print "$language->{language_locale_name}\n";
161  }
162
163 =cut
164
165 sub getAllLanguages {
166     my $languages_loop = [
167         {
168             language_code          => "",
169             language_name => "No Limit",
170             language_locale_name   => "",
171             selected       => "selected",
172         },
173         {
174             language_code          => "ara",
175             language_name =>
176               "&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;",
177             language_locale_name => "Arabic",
178             ,
179         },
180         {
181             language_code          => "bul",
182             language_name =>
183               "&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;",
184             language_locale_name => "Bulgarian",
185             ,
186         },
187         {
188             language_code          => "chi",
189             language_name => "&#20013;&#25991;",
190             language_locale_name   => "Chinese",
191             ,
192         },
193         {
194             language_code          => "scr",
195             language_name => "Hrvatski",
196             language_locale_name   => "Croatian",
197             ,
198         },
199         {
200             language_code          => "cze",
201             language_name => "&#x010D;e&#353;tina",
202             language_locale_name   => "Czech",
203             ,
204         },
205         {
206             language_code          => "dan",
207             language_name => "D&aelig;nsk",
208             language_locale_name   => "Danish",
209             ,
210         },
211         {
212             language_code          => "dut",
213             language_name => "ned&#601;rl&#593;ns",
214             language_locale_name   => "Dutch",
215             ,
216         },
217         {
218             language_code          => "en",
219             language_name => "English",
220             language_locale_name   => "English",
221             ,
222         },
223         {
224             language_code          => "fr",
225             language_name => "Fran&ccedil;ais",
226             language_locale_name   => "French",
227             ,
228         },
229         {
230             language_code          => "ger",
231             language_name => "Deutsch",
232             language_locale_name   => "German",
233             ,
234         },
235         {
236             language_code          => "gre",
237             language_name =>
238               "&#949;&#955;&#955;&#951;&#957;&#953;&#954;&#940;",
239             language_locale_name => "Greek, Modern [1453- ]",
240             ,
241         },
242         {
243             language_code          => "heb",
244             language_name => "&#1506;&#1489;&#1512;&#1497;&#1514;",
245             language_locale_name   => "Hebrew",
246             ,
247         },
248         {
249             language_code          => "hin",
250             language_name => "&#2361;&#2367;&#2344;&#2381;&#2342;&#2368;",
251             language_locale_name   => "Hindi",
252             ,
253         },
254         {
255             language_code          => "hun",
256             language_name => "Magyar",
257             language_locale_name   => "Hungarian",
258             ,
259         },
260         {
261             language_code          => "ind",
262             language_name => "",
263             language_locale_name   => "Indonesian",
264             ,
265         },
266         {
267             language_code          => "ita",
268             language_name => "Italiano",
269             language_locale_name   => "Italian",
270             ,
271         },
272         {
273             language_code          => "jpn",
274             language_name => "&#26085;&#26412;&#35486;",
275             language_locale_name   => "Japanese",
276             ,
277         },
278         {
279             language_code          => "kor",
280             language_name => "&#54620;&#44397;&#50612;",
281             language_locale_name   => "Korean",
282             ,
283         },
284         {
285             language_code          => "lat",
286             language_name => "Latina",
287             language_locale_name   => "Latin",
288             ,
289         },
290         {
291             language_code          => "nor",
292             language_name => "Norsk",
293             language_locale_name   => "Norwegian",
294             ,
295         },
296         {
297             language_code          => "per",
298             language_name => "&#1601;&#1575;&#1585;&#1587;&#1609;",
299             language_locale_name   => "Persian",
300             ,
301         },
302         {
303             language_code          => "pol",
304             language_name => "Polski",
305             language_locale_name   => "Polish",
306             ,
307         },
308         {
309             language_code          => "por",
310             language_name => "Portugu&ecirc;s",
311             language_locale_name   => "Portuguese",
312             ,
313         },
314         {
315             language_code          => "rum",
316             language_name => "Rom&acirc;n&#259;",
317             language_locale_name   => "Romanian",
318             ,
319         },
320         {
321             language_code          => "rus",
322             language_name =>
323               "&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;",
324             language_locale_name => "Russian",
325             ,
326         },
327         {
328             language_code          => "spa",
329             language_name => "Espa&ntilde;ol",
330             language_locale_name   => "Spanish",
331             ,
332         },
333         {
334             language_code          => "swe",
335             language_name => "Svenska",
336             language_locale_name   => "Swedish",
337             ,
338         },
339         {
340             language_code          => "tha",
341             language_name =>
342               "&#3616;&#3634;&#3625;&#3634;&#3652;&#3607;&#3618;",
343             language_locale_name => "Thai",
344             ,
345         },
346         {
347             language_code          => "tur",
348             language_name => "T&uuml;rk&ccedil;e",
349             language_locale_name   => "Turkish",
350             ,
351         },
352         {
353             language_code          => "ukr",
354             language_name =>
355 "&#1059;&#1082;&#1088;&#1072;&#1111;&#1085;&#1089;&#1100;&#1082;&#1072;",
356             language_locale_name => "Ukrainian",
357             ,
358         },
359
360     ];
361     return $languages_loop;
362 }
363
364 =head2 _get_themes
365
366 Internal function, returns an array of all available themes.
367
368   (@themes) = &_get_themes('opac');
369   (@themes) = &_get_themes('intranet');
370
371 =cut
372
373 sub _get_themes {
374     my $interface = shift;
375     my $htdocs;
376     my @themes;
377     if ( $interface eq 'intranet' ) {
378         $htdocs = C4::Context->config('intrahtdocs');
379     }
380     else {
381         $htdocs = C4::Context->config('opachtdocs');
382     }
383     opendir D, "$htdocs";
384     my @dirlist = readdir D;
385     foreach my $directory (@dirlist) {
386         # if there's an en dir, it's a valid theme
387         -d "$htdocs/$directory/en" and push @themes, $directory;
388     }
389     return @themes;
390 }
391
392 =head2 _get_language_dirs
393
394 Internal function, returns an array of directory names, excluding non-language directories
395
396 =cut
397
398 sub _get_language_dirs {
399     my ($htdocs,$theme) = @_;
400     my @languages;
401     opendir D, "$htdocs/$theme";
402     for my $language ( readdir D ) {
403         next if $language =~/^\./;
404         next if $language eq 'all';
405         next if $language =~/png$/;
406         next if $language =~/css$/;
407         next if $language =~/CVS$/;
408         next if $language =~/itemtypeimg$/;
409         next if $language =~/\.txt$/i;     #Don't read the readme.txt !
410         next if $language eq 'images';
411         push @languages, $language;
412     }
413         return (@languages);
414 }
415
416 =head2 _get_final_languages 
417
418 Internal function for building the ref to array of hashes
419
420 FIXME: this could be rewritten and simplified using map
421
422 =cut
423
424 sub _get_final_languages {
425         my ($all_languages,@languages) = @_;
426         my @final_languages;
427         my %seen_languages;
428         for my $language (@languages) {
429             unless ($seen_languages{$language}) {
430                 for my $language_code (@$all_languages) {
431                     if ($language eq $language_code->{'language_code'}) {
432                         push @final_languages, $language_code;
433                     }
434                 }
435                 $seen_languages{$language}++;
436             }
437         }
438         return \@final_languages;
439 }
440
441 1;
442
443 __END__
444
445 =head1 AUTHOR
446
447 Joshua Ferraro
448
449 =cut