git-interface: Add test suite and basic tests
[aur.git] / web / lib / translator.inc.php
blobd53bd53031a73efbeb245f43618acfee5d456b44
1 <?php
2 set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR . '../lang');
4 # This file provides support for i18n
6 # usage:
7 # use the __() function for returning translated strings of
8 # text. The string can contain escape codes "%s".
10 # examples:
11 # print __("%s has %s apples.", "Bill", "5");
12 # print __("This is a %smajor%s problem!", "<strong>", "</strong>");
14 include_once("confparser.inc.php");
15 include_once('DB.class.php');
16 include_once('gettext.php');
17 include_once('streams.php');
19 global $streamer, $l10n;
21 # Languages we have translations for
22 $SUPPORTED_LANGS = array(
23 "ar" => "العربية",
24 "ast" => "Asturianu",
25 "ca" => "Català",
26 "cs" => "Český",
27 "da" => "Dansk",
28 "de" => "Deutsch",
29 "en" => "English",
30 "el" => "Ελληνικά",
31 "es" => "Español",
32 "es_419" => "Español (Latinoamérica)",
33 "fi" => "Finnish",
34 "fr" => "Français",
35 "he" => "עברית",
36 "hr" => "Hrvatski",
37 "hu" => "Magyar",
38 "it" => "Italiano",
39 "ja" => "日本語",
40 "nb" => "Norsk",
41 "nl" => "Nederlands",
42 "pl" => "Polski",
43 "pt_BR" => "Português (Brasil)",
44 "pt_PT" => "Português (Portugal)",
45 "ro" => "Română",
46 "ru" => "Русский",
47 "sk" => "Slovenčina",
48 "sr" => "Srpski",
49 "tr" => "Türkçe",
50 "uk" => "Українська",
51 "zh_CN" => "简体中文",
52 "zh_TW" => "正體中文"
55 function __() {
56 global $LANG;
57 global $l10n;
59 # Create the translation.
60 $args = func_get_args();
62 # First argument is always string to be translated
63 $tag = array_shift($args);
65 # Translate using gettext_reader initialized before.
66 $translated = $l10n->translate($tag);
67 $translated = htmlspecialchars($translated, ENT_QUOTES);
69 # Subsequent arguments are strings to be formatted
70 if (count($args) > 0) {
71 $translated = vsprintf($translated, $args);
74 return $translated;
77 function _n($msgid1, $msgid2, $n) {
78 global $l10n;
80 $translated = sprintf($l10n->ngettext($msgid1, $msgid2, $n), $n);
81 return htmlspecialchars($translated, ENT_QUOTES);
84 # set up the visitor's language
86 function set_lang() {
87 global $LANG;
88 global $SUPPORTED_LANGS;
89 global $streamer, $l10n;
91 $update_cookie = 0;
92 if (isset($_POST['setlang'])) {
93 # visitor is requesting a language change
95 $LANG = $_POST['setlang'];
96 $update_cookie = 1;
98 } elseif (isset($_COOKIE['AURLANG'])) {
99 # If a cookie is set, use that
101 $LANG = $_COOKIE['AURLANG'];
103 } elseif (isset($_COOKIE["AURSID"])) {
104 # No language but a session; use default lang preference
106 $dbh = DB::connect();
107 $q = "SELECT LangPreference FROM Users, Sessions ";
108 $q.= "WHERE Users.ID = Sessions.UsersID ";
109 $q.= "AND Sessions.SessionID = '";
110 $q.= $dbh->quote($_COOKIE["AURSID"]);
111 $result = $dbh->query($q);
113 if ($result) {
114 $row = $result->fetchAll();
115 $LANG = $row[0];
117 $update_cookie = 1;
120 # Set $LANG to default if nothing is valid.
121 if (!array_key_exists($LANG, $SUPPORTED_LANGS)) {
122 $LANG = config_get('options', 'default_lang');
125 if ($update_cookie) {
126 $timeout = intval(config_get('options', 'persistent_cookie_timeout'));
127 $cookie_time = time() + $timeout;
128 setcookie("AURLANG", $LANG, $cookie_time, "/");
131 $streamer = new FileReader('../locale/' . $LANG .
132 '/LC_MESSAGES/aur.mo');
133 $l10n = new gettext_reader($streamer, true);
135 return;