aurweb/web/lib/translator.inc.php
Eli Schwartz c8d99bac8e Fix regression in translating anything at all
In commit 840ee20 (Rename translation resources from aur to aurweb,
2018-07-07) the translations file was renamed but we never actually
switched to using the renamed translations.

As a result, every single push to the AUR contains the following
traceback:

    remote: Traceback (most recent call last):
    remote:   File "/usr/bin/aurweb-notify", line 11, in <module>
    remote:     load_entry_point('aurweb==4.7.0', 'console_scripts', 'aurweb-notify')()
    remote:   File "/usr/lib/python3.6/site-packages/aurweb-4.7.0-py3.6.egg/aurweb/scripts/notify.py", line 541, in main
    remote:   File "/usr/lib/python3.6/site-packages/aurweb-4.7.0-py3.6.egg/aurweb/scripts/notify.py", line 69, in send
    remote:   File "/usr/lib/python3.6/site-packages/aurweb-4.7.0-py3.6.egg/aurweb/scripts/notify.py", line 56, in get_body_fmt
    remote:   File "/usr/lib/python3.6/site-packages/aurweb-4.7.0-py3.6.egg/aurweb/scripts/notify.py", line 192, in get_body
    remote:   File "/usr/lib/python3.6/site-packages/aurweb-4.7.0-py3.6.egg/aurweb/l10n.py", line 14, in translate
    remote:   File "/usr/lib/python3.6/gettext.py", line 514, in translation
    remote:     raise OSError(ENOENT, 'No translation file found for domain', domain)
    remote: FileNotFoundError: [Errno 2] No translation file found for domain: 'aur'

Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
2018-07-09 16:43:31 +02:00

138 lines
3.2 KiB
PHP

<?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("confparser.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(
"ar" => "العربية",
"ast" => "Asturianu",
"ca" => "Català",
"cs" => "Český",
"da" => "Dansk",
"de" => "Deutsch",
"en" => "English",
"el" => "Ελληνικά",
"es" => "Español",
"es_419" => "Español (Latinoamérica)",
"fi" => "Finnish",
"fr" => "Français",
"he" => "עברית",
"hr" => "Hrvatski",
"hu" => "Magyar",
"it" => "Italiano",
"ja" => "日本語",
"nb" => "Norsk",
"nl" => "Nederlands",
"pl" => "Polski",
"pt_BR" => "Português (Brasil)",
"pt_PT" => "Português (Portugal)",
"ro" => "Română",
"ru" => "Русский",
"sk" => "Slovenčina",
"sr" => "Srpski",
"tr" => "Türkçe",
"uk" => "Українська",
"zh_CN" => "简体中文",
"zh_TW" => "正體中文"
);
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;
}
function _n($msgid1, $msgid2, $n) {
global $l10n;
$translated = sprintf($l10n->ngettext($msgid1, $msgid2, $n), $n);
return htmlspecialchars($translated, ENT_QUOTES);
}
# set up the visitor's language
#
function set_lang() {
global $LANG;
global $SUPPORTED_LANGS;
global $streamer, $l10n;
$update_cookie = 0;
if (isset($_POST['setlang'])) {
# visitor is requesting a language change
#
$LANG = $_POST['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) {
$LANG = $result->fetchColumn(0);
if (!$LANG) {
unset($LANG);
}
}
$update_cookie = 1;
}
# Set $LANG to default if nothing is valid.
if (!isset($LANG) || !array_key_exists($LANG, $SUPPORTED_LANGS)) {
$LANG = config_get('options', 'default_lang');
}
if ($update_cookie) {
$timeout = intval(config_get('options', 'persistent_cookie_timeout'));
$cookie_time = time() + $timeout;
setcookie("AURLANG", $LANG, $cookie_time, "/");
}
$streamer = new FileReader('../locale/' . $LANG .
'/LC_MESSAGES/aurweb.mo');
$l10n = new gettext_reader($streamer, true);
return;
}