summaryrefslogtreecommitdiff
path: root/lib/KolabAdmin/include/locale.php
blob: ce7e7fdffe6287874f55199eaf91dedc6368bbb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*
 *  Copyright (c) 2005 Klarälvdalens Datakonsult AB
 *
 *    Written by Romain Pokrzywka <romain@klaralvdalens-datakonsult.se>
 *
 *  This  program is free  software; you can redistribute  it and/or
 *  modify it  under the terms of the GNU  General Public License as
 *  published by the  Free Software Foundation; either version 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

  //require_once("mysmarty.php");

// PENDING(romain,steffen): Clean up so this is not a mix of code and global functions

session_start();

function supported_lang($lang) {

    // REMEMBER TO UPDATE THIS WHEN ADDING NEW LANGUAGES
    $a = array("de"    => "de_DE",
			   "de_de" => "de_DE",
			   "fr"    => "fr_FR",
			   "fr_fr" => "fr_FR",
			   "it"    => "it_IT",
			   "it_it" => "it_IT",
			   "nl"    => "nl_NL",
			   "nl_nl" => "nl_NL",
			   "en"    => "en_US",
			   "en_gb" => "en_US",
			   "en_us" => "en_US",
			   "es"    => "es_ES",
			   "es_es" => "es_ES");

    // Locales must be in the format xx_YY to be recognized by xgettext
    $lang = strtolower(str_replace('-','_',$lang));
    if( !array_key_exists( $lang, $a ) ) return false;
    else return $a[$lang];
}

// This function is called in templates my Smarty
function translate($params) 
{
    $msg = $params["msg"];
    $domain = $params["domain"];
    if(empty($domain)) {
        return gettext($msg);
    } else {
        return dgettext($domain, $msg);
    }
}

# Returns the currently selected language
function getLanguage()
{
    if(empty($_SESSION["lang"])) 
    {
	    $acceptList = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
        if(empty($acceptList)) {
			$lang = "en";
        } else {
			// In case of multiple accept languages, keep the first one
			$acceptList = explode(",", $acceptList);
			foreach($acceptList as $l) {
				$pos = strpos($l, ';' );
				if( $pos !== false ) {
				    $l = explode(';',$l);
					$l = $l[0];
				}
				if( $tmp = supported_lang($l) ) {
				    $lang = $tmp;
				    break;
				}
			}
        }
		if( !$lang ) $lang = "en";
        setLanguage($lang);
    }    
    return supported_lang($_SESSION["lang"]);
}

# Allows languages to be set by users
function setLanguage($lang)
{   
    $lang = supported_lang($lang);
    $_SESSION["lang"] = $lang;
}

// Check if language was changed
if(!empty($_REQUEST["lang"])) {
    setLanguage($_REQUEST["lang"]);
}

// I18N support information
$language = getLanguage();
putenv("LANG=$language"); 
putenv("LANGUAGE=$language"); 
setlocale(LC_ALL, $language);

$domain = "messages";
//$tmpSmarty = new MySmarty();
bindtextdomain($domain, $locale_dir); 
bind_textdomain_codeset($domain, "UTF-8");
textdomain($domain);

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
  vim:encoding=utf-8:
 */
?>