lang.pl: Always fall back to English
[ipfire-2.x.git] / config / cfgroot / lang.pl
1 # SmoothWall CGIs
2 #
3 # This code is distributed under the terms of the GPL
4 #
5 # (c) The SmoothWall Team
6 # Copyright (c) 2002/08/23 Mark Wormgoor <mark@wormgoor.com> Split from header.pl
7 #
8 # $Id: lang.pl,v 1.1.2.11 2005/09/10 16:22:50 eoberlander Exp $
9 #
10
11 package Lang;
12 require 'CONFIG_ROOT/general-functions.pl';
13 use strict;
14
15 ### A cache file to avoid long recalculation
16 $Lang::CacheLang = '/var/ipfire/langs/cache-lang.pl';
17
18 # When you want to add your own language strings/entries to the ipcop language file,
19 # you should create a file with <PREFIX>.<LANG>.pl into CONFIG_ROOT/addon-lang dir
20 # <PREFIX> is free choosable but should be significant. An Example might be "myAddnName"
21 # <LANG> is a mnemonic of the used language like en, de, it, nl etc.
22 # You can find a detailed list of possible mnemonic's in the file CONFIG_ROOT/langs/list
23 # A file could be named "VirtualHttpd.en.pl" for example.
24 #
25 # The file content has to start with (of course without the leading #):
26 # --------- CODE ---------
27 #%tr = (%tr,
28 # 'key1' => 'value', # add all your entries key/values here
29 # 'key2' => 'value' # and end with (of course without the leading #):
30 #);
31 # --------- CODE END---------
32 #
33 # After you have copied all your files to CONFIG_ROOT/add-lang you have to run the
34 # script compilation:
35 # perl -e "require '/CONFIG_ROOT/lang.pl'; &Lang::BuildCacheLang"
36
37
38 ### Initialize language
39 %Lang::tr = ();
40 my %settings = ();
41 &General::readhash("${General::swroot}/main/settings", \%settings);
42 reload($settings{'LANGUAGE'});
43
44 # language variable used by makegraphs script
45 our $language;
46 $language = $settings{'LANGUAGE'};
47
48 #
49 # Load requested language file from cachefile. If cachefile doesn't exist, build on the fly.
50 # (it is a developper options)
51 #
52 sub reload {
53 my $LG = &FindWebLanguage(shift);
54
55 %Lang::tr = (); # start with a clean array
56
57 # Use CacheLang if present & not empty.
58 if (-s "$Lang::CacheLang.$LG" ) {
59 ##fix: need to put a lock_shared on it in case rebuild is active ?
60 do "$Lang::CacheLang.$LG";
61 #&General::log ("cachelang file used [$LG]");
62 return;
63 }
64
65 #&General::log("Building on the fly cachelang file for [$LG]");
66 do "${General::swroot}/langs/en.pl";
67 do "${General::swroot}/langs/$LG.pl" if ($LG ne 'en');
68
69 my $AddonDir = ${General::swroot}.'/addon-lang';
70
71 opendir (DIR, $AddonDir);
72 my @files = readdir (DIR);
73 closedir (DIR);
74
75 # default is to load english first
76 foreach my $file ( grep (/.*\.en.pl$/,@files)) {
77 do "$AddonDir/$file";
78 }
79
80 # read again, overwriting 'en' with choosed lang
81 if ($LG ne 'en') {
82 foreach my $file (grep (/.*\.$LG\.pl$/,@files) ) {
83 do "$AddonDir/$file";
84 }
85 }
86 }
87
88 #
89 # Assume this procedure is called with enough privileges.
90 # Merge ipcop langage file + all other extension found in addon-lang
91 # to build a 'cachefile' for selected language
92 #
93 sub BuildUniqueCacheLang {
94
95 my ($LG) = @_;
96
97 # Make CacheLang empty so that it won't be used by Lang::reload
98 open (FILE, ">$Lang::CacheLang.$LG") or return 1;
99 flock (FILE, 2) or return 1;
100 close (FILE);
101
102 # Load languages files
103 &Lang::reload ($LG);
104
105 # Write the unique %tr=('key'=>'value') array
106 open (FILE, ">$Lang::CacheLang.$LG") or return 1;
107 flock (FILE, 2) or return 1;
108 print FILE '%tr=(';
109 foreach my $k ( keys %Lang::tr ){
110 $Lang::tr{$k} =~ s/\'/\\\'/g; # quote ' => \'
111 print FILE "'$k' => '$Lang::tr{$k}',"; # key => value,
112 }
113 print FILE ');';
114 close (FILE);
115
116 # Make nobody:nobody file's owner
117 # Will work when called by root/rc.sysinit
118 chown (0,0,"$Lang::CacheLang.$LG");
119 chmod (0004,"$Lang::CacheLang.$LG");
120 return 0;
121 }
122
123 #
124 # Switch Ipcop Language for each lang then call build cachelang
125 #
126 sub BuildCacheLang {
127
128 my $AddonDir = ${General::swroot}.'/addon-lang';
129
130 # Correct permission in case addon-installer did not do it
131 opendir (DIR, $AddonDir);
132 my @files = readdir (DIR);
133 foreach my $file (@files) {
134 next if (($file eq '..') || ($file eq '.'));
135 chown (0,0,"$AddonDir/$file");
136 chmod (0004,"$AddonDir/$file");
137 }
138 closedir (DIR);
139
140 my $selected = '';;
141 my $missed = '';
142 my $error = 0;
143
144 open (LANGS, "${General::swroot}/langs/list");
145 while (<LANGS>) {
146 ($selected) = split (':');
147 if (BuildUniqueCacheLang ($selected) == 1) {
148 $missed = $selected; # will try latter. Can only be the current cachelang file locked
149 };
150 }
151 close (LANGS);
152
153 if ($missed) { # collision with current cache lang being used ?
154 $error = &BuildUniqueCacheLang ($missed);
155 }
156
157 &General::log ("WARNING: cannot build cachelang file for [$missed].") if ($error);
158 return $error;
159 }
160
161 sub FindWebLanguage() {
162 my $lang = shift;
163
164 my @options = ($lang);
165
166 my ($shortlang, $encoding) = split(/\./, $lang);
167 push(@options, $shortlang);
168
169 my ($language, $country) = split(/_/, $shortlang);
170 push(@options, $language);
171
172 # Add English as fallback
173 push(@options, "en");
174
175 foreach my $option (@options) {
176 return $option if (-e "${General::swroot}/langs/$option.pl");
177 }
178
179 return undef;
180 }
181
182 1;