-# SmoothWall CGIs\r
-#\r
-# This code is distributed under the terms of the GPL\r
-#\r
-# (c) The SmoothWall Team\r
-# Copyright (c) 2002/08/23 Mark Wormgoor <mark@wormgoor.com> Split from header.pl\r
-#\r
-# $Id: lang.pl,v 1.1.2.11 2005/09/10 16:22:50 eoberlander Exp $\r
-#\r
-\r
-package Lang;\r
-require 'CONFIG_ROOT/general-functions.pl';\r
-use strict;\r
-\r
-### A cache file to avoid long recalculation\r
-$Lang::CacheLang = '/var/run/cache-lang.pl';\r
-\r
-# When you want to add your own language strings/entries to the ipcop language file,\r
-# you should create a file with <PREFIX>.<LANG>.pl into CONFIG_ROOT/addon-lang dir\r
-# <PREFIX> is free choosable but should be significant. An Example might be "myAddnName"\r
-# <LANG> is a mnemonic of the used language like en, de, it, nl etc.\r
-# You can find a detailed list of possible mnemonic's in the file CONFIG_ROOT/langs/list\r
-# A file could be named "VirtualHttpd.en.pl" for example.\r
-#\r
-# The file content has to start with (of course without the leading #):\r
-# --------- CODE ---------\r
-#%tr = (%tr,\r
-# 'key1' => 'value', # add all your entries key/values here \r
-# 'key2' => 'value' # and end with (of course without the leading #):\r
-#);\r
-# --------- CODE END---------\r
-#\r
-# After you have copied all your files to CONFIG_ROOT/add-lang you have to run the\r
-# script compilation:\r
-# perl -e "require '/CONFIG_ROOT/lang.pl'; &Lang::BuildCacheLang"\r
-\r
-\r
-### Initialize language\r
-%Lang::tr = ();\r
-my %settings = ();\r
-&General::readhash("${General::swroot}/main/settings", \%settings);\r
-reload($settings{'LANGUAGE'});\r
-\r
-# language variable used by makegraphs script\r
-our $language;\r
-$language = $settings{'LANGUAGE'};\r
-\r
-#\r
-# Load requested language file from cachefile. If cachefile doesn't exist, build on the fly.\r
-# (it is a developper options)\r
-#\r
-sub reload {\r
-\r
- my ($LG) = @_;\r
- %Lang::tr = (); # start with a clean array\r
-\r
- # Use CacheLang if present & not empty.\r
- if (-s "$Lang::CacheLang.$LG" ) {\r
- ##fix: need to put a lock_shared on it in case rebuild is active ?\r
- do "$Lang::CacheLang.$LG";\r
- #&General::log ("cachelang file used [$LG]"); \r
- return;\r
- }\r
- \r
- #&General::log("Building on the fly cachelang file for [$LG]");\r
- do "${General::swroot}/langs/en.pl";\r
- do "${General::swroot}/langs/$LG.pl" if ($LG ne 'en');\r
-\r
- my $AddonDir = ${General::swroot}.'/addon-lang';\r
-\r
- opendir (DIR, $AddonDir);\r
- my @files = readdir (DIR);\r
- closedir (DIR);\r
-\r
- # default is to load english first\r
- foreach my $file ( grep (/.*\.en.pl$/,@files)) {\r
- do "$AddonDir/$file";\r
- }\r
-\r
- # read again, overwriting 'en' with choosed lang\r
- if ($LG ne 'en') {\r
- foreach my $file (grep (/.*\.$LG\.pl$/,@files) ) {\r
- do "$AddonDir/$file";\r
- }\r
- }\r
-}\r
-\r
-#\r
-# Assume this procedure is called with enough privileges.\r
-# Merge ipcop langage file + all other extension found in addon-lang\r
-# to build a 'cachefile' for selected language\r
-#\r
-sub BuildUniqueCacheLang {\r
-\r
- my ($LG) = @_;\r
- \r
- # Make CacheLang empty so that it won't be used by Lang::reload\r
- open (FILE, ">$Lang::CacheLang.$LG") or return 1;\r
- flock (FILE, 2) or return 1;\r
- close (FILE);\r
-\r
- # Load languages files\r
- &Lang::reload ($LG);\r
- \r
- # Write the unique %tr=('key'=>'value') array\r
- open (FILE, ">$Lang::CacheLang.$LG") or return 1;\r
- flock (FILE, 2) or return 1;\r
- print FILE '%tr=(';\r
- foreach my $k ( keys %Lang::tr ){\r
- $Lang::tr{$k} =~ s/\'/\\\'/g; # quote ' => \'\r
- print FILE "'$k' => '$Lang::tr{$k}',"; # key => value,\r
- }\r
- print FILE ');';\r
- close (FILE);\r
- \r
- # Make nobody:nobody file's owner\r
- # Will work when called by root/rc.sysinit\r
- chown (0,0,"$Lang::CacheLang.$LG");\r
- chmod (0004,"$Lang::CacheLang.$LG");\r
- return 0;\r
-}\r
-\r
-#\r
-# Switch Ipcop Language for each lang then call build cachelang\r
-#\r
-sub BuildCacheLang {\r
-\r
- my $AddonDir = ${General::swroot}.'/addon-lang';\r
- \r
- # Correct permission in case addon-installer did not do it\r
- opendir (DIR, $AddonDir);\r
- my @files = readdir (DIR);\r
- foreach my $file (@files) {\r
- next if (($file eq '..') || ($file eq '.'));\r
- chown (0,0,"$AddonDir/$file");\r
- chmod (0004,"$AddonDir/$file");\r
- }\r
- closedir (DIR);\r
-\r
- my $selected = '';;\r
- my $missed = '';\r
- my $error = 0;\r
- \r
- open (LANGS, "${General::swroot}/langs/list");\r
- while (<LANGS>) {\r
- ($selected) = split (':');\r
- if (BuildUniqueCacheLang ($selected) == 1) {\r
- $missed = $selected; # will try latter. Can only be the current cachelang file locked\r
- };\r
- }\r
- close (LANGS);\r
-\r
- if ($missed) { # collision with current cache lang being used ?\r
- $error = &BuildUniqueCacheLang ($missed);\r
- }\r
- \r
- &General::log ("WARNING: cannot build cachelang file for [$missed].") if ($error);\r
- return $error;\r
-}\r
-1;\r
+# SmoothWall CGIs
+#
+# This code is distributed under the terms of the GPL
+#
+# (c) The SmoothWall Team
+# Copyright (c) 2002/08/23 Mark Wormgoor <mark@wormgoor.com> Split from header.pl
+#
+# $Id: lang.pl,v 1.1.2.11 2005/09/10 16:22:50 eoberlander Exp $
+#
+
+package Lang;
+require 'CONFIG_ROOT/general-functions.pl';
+use strict;
+
+### A cache file to avoid long recalculation
+$Lang::CacheLang = '/var/ipfire/langs/cache-lang.pl';
+
+# When you want to add your own language strings/entries to the ipcop language file,
+# you should create a file with <PREFIX>.<LANG>.pl into CONFIG_ROOT/addon-lang dir
+# <PREFIX> is free choosable but should be significant. An Example might be "myAddnName"
+# <LANG> is a mnemonic of the used language like en, de, it, nl etc.
+# You can find a detailed list of possible mnemonic's in the file CONFIG_ROOT/langs/list
+# A file could be named "VirtualHttpd.en.pl" for example.
+#
+# The file content has to start with (of course without the leading #):
+# --------- CODE ---------
+#%tr = (%tr,
+# 'key1' => 'value', # add all your entries key/values here
+# 'key2' => 'value' # and end with (of course without the leading #):
+#);
+# --------- CODE END---------
+#
+# After you have copied all your files to CONFIG_ROOT/add-lang you have to run the
+# script compilation:
+# perl -e "require '/CONFIG_ROOT/lang.pl'; &Lang::BuildCacheLang"
+
+
+### Initialize language
+%Lang::tr = ();
+my %settings = ();
+&General::readhash("${General::swroot}/main/settings", \%settings);
+reload($settings{'LANGUAGE'});
+
+# language variable used by makegraphs script
+our $language;
+$language = $settings{'LANGUAGE'};
+
+#
+# Load requested language file from cachefile. If cachefile doesn't exist, build on the fly.
+# (it is a developper options)
+#
+sub reload {
+ my $LG = &FindWebLanguage(shift);
+
+ %Lang::tr = (); # start with a clean array
+
+ # Use CacheLang if present & not empty.
+ if (-s "$Lang::CacheLang.$LG" ) {
+ ##fix: need to put a lock_shared on it in case rebuild is active ?
+ do "$Lang::CacheLang.$LG";
+ #&General::log ("cachelang file used [$LG]");
+ return;
+ }
+
+ #&General::log("Building on the fly cachelang file for [$LG]");
+ do "${General::swroot}/langs/en.pl";
+ do "${General::swroot}/langs/$LG.pl" if ($LG ne 'en');
+
+ my $AddonDir = ${General::swroot}.'/addon-lang';
+
+ opendir (DIR, $AddonDir);
+ my @files = readdir (DIR);
+ closedir (DIR);
+
+ # default is to load english first
+ foreach my $file ( grep (/.*\.en.pl$/,@files)) {
+ do "$AddonDir/$file";
+ }
+
+ # read again, overwriting 'en' with choosed lang
+ if ($LG ne 'en') {
+ foreach my $file (grep (/.*\.$LG\.pl$/,@files) ) {
+ do "$AddonDir/$file";
+ }
+ }
+}
+
+#
+# Assume this procedure is called with enough privileges.
+# Merge ipcop langage file + all other extension found in addon-lang
+# to build a 'cachefile' for selected language
+#
+sub BuildUniqueCacheLang {
+
+ my ($LG) = @_;
+
+ # Make CacheLang empty so that it won't be used by Lang::reload
+ open (FILE, ">$Lang::CacheLang.$LG") or return 1;
+ flock (FILE, 2) or return 1;
+ close (FILE);
+
+ # Load languages files
+ &Lang::reload ($LG);
+
+ # Write the unique %tr=('key'=>'value') array
+ open (FILE, ">$Lang::CacheLang.$LG") or return 1;
+ flock (FILE, 2) or return 1;
+ print FILE '%tr=(';
+ foreach my $k ( keys %Lang::tr ){
+ $Lang::tr{$k} =~ s/\'/\\\'/g; # quote ' => \'
+ print FILE "'$k' => '$Lang::tr{$k}',"; # key => value,
+ }
+ print FILE ');';
+ close (FILE);
+
+ # Make nobody:nobody file's owner
+ # Will work when called by root/rc.sysinit
+ chown (0,0,"$Lang::CacheLang.$LG");
+ chmod (0004,"$Lang::CacheLang.$LG");
+ return 0;
+}
+
+#
+# Switch Ipcop Language for each lang then call build cachelang
+#
+sub BuildCacheLang {
+
+ my $AddonDir = ${General::swroot}.'/addon-lang';
+
+ # Correct permission in case addon-installer did not do it
+ opendir (DIR, $AddonDir);
+ my @files = readdir (DIR);
+ foreach my $file (@files) {
+ next if (($file eq '..') || ($file eq '.'));
+ chown (0,0,"$AddonDir/$file");
+ chmod (0004,"$AddonDir/$file");
+ }
+ closedir (DIR);
+
+ my $selected = '';;
+ my $missed = '';
+ my $error = 0;
+
+ open (LANGS, "${General::swroot}/langs/list");
+ while (<LANGS>) {
+ ($selected) = split (':');
+ if (BuildUniqueCacheLang ($selected) == 1) {
+ $missed = $selected; # will try latter. Can only be the current cachelang file locked
+ };
+ }
+ close (LANGS);
+
+ if ($missed) { # collision with current cache lang being used ?
+ $error = &BuildUniqueCacheLang ($missed);
+ }
+
+ &General::log ("WARNING: cannot build cachelang file for [$missed].") if ($error);
+ return $error;
+}
+
+sub FindWebLanguage() {
+ my $lang = shift;
+
+ my @options = ($lang);
+
+ my ($shortlang, $encoding) = split(/\./, $lang);
+ push(@options, $shortlang);
+
+ my ($language, $country) = split(/_/, $shortlang);
+ push(@options, $language);
+
+ # Add English as fallback
+ push(@options, "en");
+
+ foreach my $option (@options) {
+ return $option if (-e "${General::swroot}/langs/$option.pl");
+ }
+
+ return undef;
+}
+
+1;