load-lang.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2006, 2008, 2009 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('load-lang.pl', 'Language Browser Preferences');
  18. our ($q, %CookieParameters, $ConfigFile, $DataDir, $ModuleDir, $NamespaceCurrent, @MyInitVariables);
  19. our $CurrentLanguage;
  20. our $LoadLanguageDir = "$ModuleDir/translations"; # by default same as in git
  21. $CookieParameters{interface} = '';
  22. our %TranslationsLibrary = (
  23. 'bg' => 'bulgarian-utf8.pl',
  24. 'ca' => 'catalan-utf8.pl',
  25. 'de' => 'german-utf8.pl',
  26. 'et' => 'estonian-utf8.pl',
  27. 'es' => 'spanish-utf8.pl',
  28. 'fi' => 'finnish-utf8.pl',
  29. 'fr' => 'french-utf8.pl',
  30. 'gr' => 'greek-utf8.pl',
  31. 'he' => 'hebrew-utf8.pl',
  32. 'it' => 'italian-utf8.pl',
  33. 'ja' => 'japanese-utf8.pl',
  34. 'ko' => 'korean-utf8.pl',
  35. 'nl' => 'dutch-utf8.pl',
  36. 'pl' => 'polish-utf8.pl',
  37. 'pt' => 'portuguese-utf8.pl',
  38. 'pt-br' => 'brazilian-portuguese-utf8.pl',
  39. 'ro' => 'romanian-utf8.pl',
  40. 'ru' => 'russian-utf8.pl',
  41. 'se' => 'swedish-utf8.pl',
  42. 'sr' => 'serbian-utf8.pl',
  43. 'uk' => 'ukrainian-utf8.pl',
  44. 'zh' => 'chinese-utf8.pl',
  45. 'zh-cn' => 'chinese_cn-utf8.pl',
  46. 'zh-tw' => 'chinese-utf8.pl',
  47. );
  48. sub LoadLanguage {
  49. # my $requested_language = "da, en-gb;q=0.8, en;q=0.7";
  50. my $requested_language = $q->http('Accept-language');
  51. my @languages = split(/ *, */, $requested_language);
  52. my %Lang = ();
  53. foreach (@languages) {
  54. my $qual = 1;
  55. $qual = $1 if (/q=([0-9.]+)/);
  56. $Lang{$qual} = $1 if (/^([-a-z]+)/);
  57. }
  58. my $lang = GetParam('interface', '');
  59. $Lang{2} = $lang if $lang;
  60. my @prefs = sort { $b <=> $a } keys %Lang;
  61. # print ($q->header . $q->start_html
  62. # . $q->pre("input: $requested_language\n"
  63. # . "Result: "
  64. # . join(', ', map { "$_ ($Lang{$_})" } @prefs))
  65. # . $q->end_html) && exit if GetParam('debug', '');
  66. foreach (@prefs) {
  67. last if $Lang{$_} eq 'en'; # the default
  68. my $file = $TranslationsLibrary{$Lang{$_}};
  69. next unless $file; # file is not listed, eg. there is no file for "de-ch"
  70. $file = "$LoadLanguageDir/$file" if defined $LoadLanguageDir;
  71. if (IsFile($file)) {
  72. do $file;
  73. do "$ConfigFile-$Lang{$_}" if IsFile("$ConfigFile-$Lang{$_}");
  74. $CurrentLanguage = $Lang{$_};
  75. last;
  76. }
  77. }
  78. }
  79. # Must load language dependent config files before running init code for
  80. # gotobar.pl and similar extensions.
  81. unshift(@MyInitVariables, \&LoadLanguage);