Add Chinese (Taiwan) translations
[aur.git] / web / lib / translator.inc.php
blob29bd1b340e2c700281077f318f3e25e32cdce85f
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 "ca" => "Català",
24 "cs" => "česky",
25 "da" => "Dansk",
26 "de" => "Deutsch",
27 "en" => "English",
28 "el" => "Ελληνικά",
29 "es" => "Español",
30 "fi" => "Finnish",
31 "fr" => "Français",
32 "he" => "עברית",
33 "hr" => "Hrvatski",
34 "hu" => "Magyar",
35 "it" => "Italiano",
36 "ja" => "日本語",
37 "nb" => "Norsk",
38 "nl" => "Nederlands",
39 "pl" => "Polski",
40 "pt_BR" => "Português (Brasil)",
41 "pt_PT" => "Português (Portugal)",
42 "ro" => "Română",
43 "ru" => "Русский",
44 "sk" => "Slovenčina",
45 "sr" => "Srpski",
46 "tr" => "Türkçe",
47 "uk" => "Українська",
48 "zh_CN" => "简体中文",
49 "zh_TW" => "正體中文"
52 function __() {
53 global $LANG;
54 global $l10n;
56 # Create the translation.
57 $args = func_get_args();
59 # First argument is always string to be translated
60 $tag = array_shift($args);
62 # Translate using gettext_reader initialized before.
63 $translated = $l10n->translate($tag);
64 $translated = htmlspecialchars($translated, ENT_QUOTES);
66 # Subsequent arguments are strings to be formatted
67 if (count($args) > 0) {
68 $translated = vsprintf($translated, $args);
71 return $translated;
74 function _n($msgid1, $msgid2, $n) {
75 global $l10n;
77 $translated = sprintf($l10n->ngettext($msgid1, $msgid2, $n), $n);
78 return htmlspecialchars($translated, ENT_QUOTES);
81 # set up the visitor's language
83 function set_lang() {
84 global $LANG;
85 global $SUPPORTED_LANGS;
86 global $streamer, $l10n;
88 $update_cookie = 0;
89 if (isset($_POST['setlang'])) {
90 # visitor is requesting a language change
92 $LANG = $_POST['setlang'];
93 $update_cookie = 1;
95 } elseif (isset($_COOKIE['AURLANG'])) {
96 # If a cookie is set, use that
98 $LANG = $_COOKIE['AURLANG'];
100 } elseif (isset($_COOKIE["AURSID"])) {
101 # No language but a session; use default lang preference
103 $dbh = DB::connect();
104 $q = "SELECT LangPreference FROM Users, Sessions ";
105 $q.= "WHERE Users.ID = Sessions.UsersID ";
106 $q.= "AND Sessions.SessionID = '";
107 $q.= $dbh->quote($_COOKIE["AURSID"]);
108 $result = $dbh->query($q);
110 if ($result) {
111 $row = $result->fetchAll();
112 $LANG = $row[0];
114 $update_cookie = 1;
117 # Set $LANG to default if nothing is valid.
118 if (!array_key_exists($LANG, $SUPPORTED_LANGS)) {
119 $LANG = config_get('options', 'default_lang');
122 if ($update_cookie) {
123 $timeout = intval(config_get('options', 'persistent_cookie_timeout'));
124 $cookie_time = time() + $timeout;
125 setcookie("AURLANG", $LANG, $cookie_time, "/");
128 $streamer = new FileReader('../locale/' . $LANG .
129 '/LC_MESSAGES/aur.mo');
130 $l10n = new gettext_reader($streamer, true);
132 return;