translator.inc.php 3.25 KB
Newer Older
1
<?php
2
3
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR . '../lang');

4
# This file provides support for i18n
eric's avatar
eric committed
5
6

# usage:
7
#   use the __() function for returning translated strings of
8
#   text.  The string can contain escape codes "%s".
eric's avatar
eric committed
9
10
#
# examples:
11
#	print __("%s has %s apples.", "Bill", "5");
Lukas Fleischer's avatar
Lukas Fleischer committed
12
#	print __("This is a %smajor%s problem!", "<strong>", "</strong>");
eric's avatar
eric committed
13

14
include_once("confparser.inc.php");
15
include_once('DB.class.php');
16
17
include_once('gettext.php');
include_once('streams.php');
18

19
global $streamer, $l10n;
20

21
22
# Languages we have translations for
$SUPPORTED_LANGS = array(
Lukas Fleischer's avatar
Lukas Fleischer committed
23
	"ar" => "العربية",
24
	"ast" => "Asturianu",
25
	"ca" => "Català",
26
	"cs" => "Český",
27
28
29
30
31
	"da" => "Dansk",
	"de" => "Deutsch",
	"en" => "English",
	"el" => "Ελληνικά",
	"es" => "Español",
32
	"es_419" => "Español (Latinoamérica)",
33
34
35
36
37
38
	"fi" => "Finnish",
	"fr" => "Français",
	"he" => "עברית",
	"hr" => "Hrvatski",
	"hu" => "Magyar",
	"it" => "Italiano",
39
	"ja" => "日本語",
Lukas Fleischer's avatar
Lukas Fleischer committed
40
	"nb" => "Norsk",
41
	"nl" => "Nederlands",
42
43
	"pl" => "Polski",
	"pt_BR" => "Português (Brasil)",
Lukas Fleischer's avatar
Lukas Fleischer committed
44
	"pt_PT" => "Português (Portugal)",
45
46
	"ro" => "Română",
	"ru" => "Русский",
47
	"sk" => "Slovenčina",
48
49
50
	"sr" => "Srpski",
	"tr" => "Türkçe",
	"uk" => "Українська",
51
52
	"zh_CN" => "简体中文",
	"zh_TW" => "正體中文"
53
54
);

55
function __() {
eric's avatar
eric committed
56
	global $LANG;
57
	global $l10n;
eric's avatar
eric committed
58

59
	# Create the translation.
60
61
62
	$args = func_get_args();

	# First argument is always string to be translated
63
	$tag = array_shift($args);
64

65
66
	# Translate using gettext_reader initialized before.
	$translated = $l10n->translate($tag);
Loui Chang's avatar
Loui Chang committed
67
	$translated = htmlspecialchars($translated, ENT_QUOTES);
68

69
	# Subsequent arguments are strings to be formatted
70
71
	if (count($args) > 0) {
		$translated = vsprintf($translated, $args);
eric's avatar
eric committed
72
	}
73

eric's avatar
eric committed
74
75
76
	return $translated;
}

77
78
79
80
81
82
83
function _n($msgid1, $msgid2, $n) {
	global $l10n;

	$translated = sprintf($l10n->ngettext($msgid1, $msgid2, $n), $n);
	return htmlspecialchars($translated, ENT_QUOTES);
}

84
85
# set up the visitor's language
#
86
function set_lang() {
87
88
89
90
91
	global $LANG;
	global $SUPPORTED_LANGS;
	global $streamer, $l10n;

	$update_cookie = 0;
92
	if (isset($_POST['setlang'])) {
93
94
		# visitor is requesting a language change
		#
95
		$LANG = $_POST['setlang'];
96
97
98
99
100
101
102
103
104
105
		$update_cookie = 1;

	} elseif (isset($_COOKIE['AURLANG'])) {
		# If a cookie is set, use that
		#
		$LANG = $_COOKIE['AURLANG'];

	} elseif (isset($_COOKIE["AURSID"])) {
		# No language but a session; use default lang preference
		#
106
		$dbh = DB::connect();
107
108
		$q = "SELECT LangPreference FROM Users, Sessions ";
		$q.= "WHERE Users.ID = Sessions.UsersID ";
109
		$q.= "AND Sessions.SessionID = ";
canyonknight's avatar
canyonknight committed
110
111
		$q.= $dbh->quote($_COOKIE["AURSID"]);
		$result = $dbh->query($q);
112
113

		if ($result) {
114
115
116
117
			$LANG = $result->fetchColumn(0);
			if (!$LANG) {
				unset($LANG);
			}
118
119
120
121
122
		}
		$update_cookie = 1;
	}

	# Set $LANG to default if nothing is valid.
123
	if (!isset($LANG) || !array_key_exists($LANG, $SUPPORTED_LANGS)) {
124
		$LANG = config_get('options', 'default_lang');
125
126
127
	}

	if ($update_cookie) {
128
129
		$timeout = intval(config_get('options', 'persistent_cookie_timeout'));
		$cookie_time = time() + $timeout;
130
131
132
133
		setcookie("AURLANG", $LANG, $cookie_time, "/");
	}

	$streamer = new FileReader('../locale/' . $LANG .
134
		'/LC_MESSAGES/aurweb.mo');
135
136
137
138
	$l10n = new gettext_reader($streamer, true);

	return;
}