summaryrefslogtreecommitdiff
path: root/web/lib/translator.inc.php
blob: 67693e07d5473b67073f14bf4a046915a48eaad5 (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
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR . '../lang');

# This file provides support for i18n

# usage:
#   use the __() function for returning translated strings of
#   text.  The string can contain escape codes "%s".
#
# examples:
#	print __("%s has %s apples.", "Bill", "5");
#	print __("This is a %smajor%s problem!", "<strong>", "</strong>");

include_once('config.inc.php');
include_once('DB.class.php');
include_once('gettext.php');
include_once('streams.php');

global $streamer, $l10n;

# Languages we have translations for
$SUPPORTED_LANGS = array(
	"ca" => "Català",
	"cs" => "česky",
	"da" => "Dansk",
	"de" => "Deutsch",
	"en" => "English",
	"el" => "Ελληνικά",
	"es" => "Español",
	"fi" => "Finnish",
	"fr" => "Français",
	"he" => "עברית",
	"hr" => "Hrvatski",
	"hu" => "Magyar",
	"it" => "Italiano",
	"nb" => "Norsk",
	"nl" => "Nederlands",
	"pl" => "Polski",
	"pt_BR" => "Português (Brasil)",
	"pt_PT" => "Português (Portugal)",
	"ro" => "Română",
	"ru" => "Русский",
	"sr" => "Srpski",
	"tr" => "Türkçe",
	"uk" => "Українська",
	"zh_CN" => "简体中文"
);

function __() {
	global $LANG;
	global $l10n;

	# Create the translation.
	$args = func_get_args();

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

	# Translate using gettext_reader initialized before.
	$translated = $l10n->translate($tag);
	$translated = htmlspecialchars($translated, ENT_QUOTES);

	# Subsequent arguments are strings to be formatted
	if (count($args) > 0) {
		$translated = vsprintf($translated, $args);
	}

	return $translated;
}

# set up the visitor's language
#
function set_lang() {
	global $LANG;
	global $SUPPORTED_LANGS;
	global $PERSISTENT_COOKIE_TIMEOUT;
	global $streamer, $l10n;

	$update_cookie = 0;
	if (isset($_REQUEST['setlang'])) {
		# visitor is requesting a language change
		#
		$LANG = $_REQUEST['setlang'];
		$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
		#
		$dbh = DB::connect();
		$q = "SELECT LangPreference FROM Users, Sessions ";
		$q.= "WHERE Users.ID = Sessions.UsersID ";
		$q.= "AND Sessions.SessionID = '";
		$q.= $dbh->quote($_COOKIE["AURSID"]);
		$result = $dbh->query($q);

		if ($result) {
			$row = $result->fetchAll();
			$LANG = $row[0];
		}
		$update_cookie = 1;
	}

	# Set $LANG to default if nothing is valid.
	if (!array_key_exists($LANG, $SUPPORTED_LANGS)) {
		$LANG = DEFAULT_LANG;
	}

	if ($update_cookie) {
		$cookie_time = time() + $PERSISTENT_COOKIE_TIMEOUT;
		setcookie("AURLANG", $LANG, $cookie_time, "/");
	}

	$streamer = new FileReader('../locale/' . $LANG .
		'/LC_MESSAGES/aur.mo');
	$l10n = new gettext_reader($streamer, true);

	return;
}