2015-09-04 20:24:56 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-11 05:58:15 -04:00
|
|
|
* PrivateBin
|
2015-09-04 20:24:56 -04:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 05:58:15 -04:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2015-09-04 20:24:56 -04:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 07:56:52 -04:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2021-04-05 10:44:12 -04:00
|
|
|
* @version 1.3.5
|
2015-09-04 20:24:56 -04:00
|
|
|
*/
|
2016-12-12 12:43:23 -05:00
|
|
|
|
2016-12-12 12:50:00 -05:00
|
|
|
namespace PrivateBin;
|
2016-07-21 11:09:48 -04:00
|
|
|
|
2015-09-04 20:24:56 -04:00
|
|
|
/**
|
2016-08-09 05:54:42 -04:00
|
|
|
* I18n
|
2015-09-04 20:24:56 -04:00
|
|
|
*
|
|
|
|
* provides internationalization tools like translation, browser language detection, etc.
|
|
|
|
*/
|
2016-08-09 05:54:42 -04:00
|
|
|
class I18n
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
2015-09-06 13:21:17 -04:00
|
|
|
/**
|
|
|
|
* language
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_language = 'en';
|
|
|
|
|
2015-10-18 14:38:07 -04:00
|
|
|
/**
|
|
|
|
* language fallback
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_languageFallback = 'en';
|
|
|
|
|
2015-09-19 05:21:13 -04:00
|
|
|
/**
|
|
|
|
* language labels
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_languageLabels = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* available languages
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $_availableLanguages = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* path to language files
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $_path = '';
|
|
|
|
|
2015-09-04 20:24:56 -04:00
|
|
|
/**
|
|
|
|
* translation cache
|
|
|
|
*
|
2015-09-06 13:21:17 -04:00
|
|
|
* @access protected
|
2015-09-04 20:24:56 -04:00
|
|
|
* @static
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-09-06 13:21:17 -04:00
|
|
|
protected static $_translations = array();
|
2015-09-04 20:24:56 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* translate a string, alias for translate()
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $messageId
|
|
|
|
* @param mixed $args one or multiple parameters injected into placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function _($messageId)
|
|
|
|
{
|
2016-08-09 05:54:42 -04:00
|
|
|
return forward_static_call_array('self::translate', func_get_args());
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* translate a string
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $messageId
|
|
|
|
* @param mixed $args one or multiple parameters injected into placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function translate($messageId)
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (empty($messageId)) {
|
|
|
|
return $messageId;
|
|
|
|
}
|
|
|
|
if (count(self::$_translations) === 0) {
|
|
|
|
self::loadTranslations();
|
|
|
|
}
|
2015-09-06 13:21:17 -04:00
|
|
|
$messages = $messageId;
|
2016-07-26 02:19:35 -04:00
|
|
|
if (is_array($messageId)) {
|
2015-09-06 13:21:17 -04:00
|
|
|
$messageId = count($messageId) > 1 ? $messageId[1] : $messageId[0];
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!array_key_exists($messageId, self::$_translations)) {
|
2015-09-06 13:21:17 -04:00
|
|
|
self::$_translations[$messageId] = $messages;
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
$args = func_get_args();
|
2016-07-26 02:19:35 -04:00
|
|
|
if (is_array(self::$_translations[$messageId])) {
|
2015-09-06 13:21:17 -04:00
|
|
|
$number = (int) $args[1];
|
2016-08-15 10:45:47 -04:00
|
|
|
$key = self::_getPluralForm($number);
|
|
|
|
$max = count(self::$_translations[$messageId]) - 1;
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($key > $max) {
|
|
|
|
$key = $max;
|
|
|
|
}
|
2015-09-06 13:21:17 -04:00
|
|
|
|
|
|
|
$args[0] = self::$_translations[$messageId][$key];
|
|
|
|
$args[1] = $number;
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2015-09-06 13:21:17 -04:00
|
|
|
$args[0] = self::$_translations[$messageId];
|
|
|
|
}
|
2020-02-01 02:46:59 -05:00
|
|
|
// encode any non-integer arguments and the message ID, if it doesn't contain a link
|
|
|
|
$argsCount = count($args);
|
|
|
|
if ($argsCount > 1) {
|
|
|
|
for ($i = 0; $i < $argsCount; ++$i) {
|
|
|
|
if (($i > 0 && !is_int($args[$i])) || strpos($args[0], '<a') === false) {
|
2020-02-01 03:15:14 -05:00
|
|
|
$args[$i] = self::encode($args[$i]);
|
2020-02-01 02:46:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 20:24:56 -04:00
|
|
|
return call_user_func_array('sprintf', $args);
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:15:14 -05:00
|
|
|
/**
|
|
|
|
* encode HTML entities for output into an HTML5 document
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function encode($string)
|
|
|
|
{
|
|
|
|
return htmlspecialchars($string, ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED, 'UTF-8', false);
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:24:56 -04:00
|
|
|
/**
|
|
|
|
* loads translations
|
|
|
|
*
|
2016-07-19 07:56:52 -04:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-04 20:24:56 -04:00
|
|
|
*
|
2015-09-19 05:21:13 -04:00
|
|
|
* @access public
|
2015-09-04 20:24:56 -04:00
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
public static function loadTranslations()
|
|
|
|
{
|
2015-09-19 05:21:13 -04:00
|
|
|
$availableLanguages = self::getAvailableLanguages();
|
|
|
|
|
|
|
|
// check if the lang cookie was set and that language exists
|
2017-03-24 19:58:59 -04:00
|
|
|
if (
|
|
|
|
array_key_exists('lang', $_COOKIE) &&
|
|
|
|
($key = array_search($_COOKIE['lang'], $availableLanguages)) !== false
|
|
|
|
) {
|
|
|
|
$match = $availableLanguages[$key];
|
2015-09-19 05:21:13 -04:00
|
|
|
}
|
|
|
|
// find a translation file matching the browsers language preferences
|
2016-07-26 02:19:35 -04:00
|
|
|
else {
|
2015-09-19 05:21:13 -04:00
|
|
|
$match = self::_getMatchingLanguage(
|
|
|
|
self::getBrowserLanguages(), $availableLanguages
|
|
|
|
);
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// load translations
|
2016-08-15 10:45:47 -04:00
|
|
|
self::$_language = $match;
|
2019-05-13 16:31:52 -04:00
|
|
|
self::$_translations = ($match == 'en') ? array() : Json::decode(
|
|
|
|
file_get_contents(self::_getPath($match . '.json'))
|
2015-09-19 05:21:13 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get list of available translations based on files found
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getAvailableLanguages()
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count(self::$_availableLanguages) == 0) {
|
2015-09-19 05:21:13 -04:00
|
|
|
$i18n = dir(self::_getPath());
|
2016-07-26 02:19:35 -04:00
|
|
|
while (false !== ($file = $i18n->read())) {
|
|
|
|
if (preg_match('/^([a-z]{2}).json$/', $file, $match) === 1) {
|
2015-09-19 05:21:13 -04:00
|
|
|
self::$_availableLanguages[] = $match[1];
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
2015-09-19 05:21:13 -04:00
|
|
|
return self::$_availableLanguages;
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* detect the clients supported languages and return them ordered by preference
|
|
|
|
*
|
2016-07-19 07:56:52 -04:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-04 20:24:56 -04:00
|
|
|
*
|
2015-09-19 05:21:13 -04:00
|
|
|
* @access public
|
|
|
|
* @static
|
2015-09-04 20:24:56 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getBrowserLanguages()
|
|
|
|
{
|
|
|
|
$languages = array();
|
2016-07-26 02:19:35 -04:00
|
|
|
if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$languageRanges = explode(',', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']));
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach ($languageRanges as $languageRange) {
|
2015-09-04 20:24:56 -04:00
|
|
|
if (preg_match(
|
|
|
|
'/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/',
|
|
|
|
trim($languageRange), $match
|
2016-07-26 02:19:35 -04:00
|
|
|
)) {
|
|
|
|
if (!isset($match[2])) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$match[2] = '1.0';
|
2016-07-26 02:19:35 -04:00
|
|
|
} else {
|
2015-09-04 20:24:56 -04:00
|
|
|
$match[2] = (string) floatval($match[2]);
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!isset($languages[$match[2]])) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$languages[$match[2]] = array();
|
|
|
|
}
|
|
|
|
$languages[$match[2]][] = strtolower($match[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
krsort($languages);
|
|
|
|
}
|
|
|
|
return $languages;
|
|
|
|
}
|
|
|
|
|
2015-09-19 05:21:13 -04:00
|
|
|
/**
|
|
|
|
* get currently loaded language
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getLanguage()
|
|
|
|
{
|
|
|
|
return self::$_language;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get list of language labels
|
|
|
|
*
|
|
|
|
* Only for given language codes, otherwise all labels.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param array $languages
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getLanguageLabels($languages = array())
|
|
|
|
{
|
|
|
|
$file = self::_getPath('languages.json');
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count(self::$_languageLabels) == 0 && is_readable($file)) {
|
2019-05-13 16:31:52 -04:00
|
|
|
self::$_languageLabels = Json::decode(file_get_contents($file));
|
2015-09-19 05:21:13 -04:00
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count($languages) == 0) {
|
|
|
|
return self::$_languageLabels;
|
|
|
|
}
|
2015-09-19 05:21:13 -04:00
|
|
|
return array_intersect_key(self::$_languageLabels, array_flip($languages));
|
|
|
|
}
|
|
|
|
|
2015-10-18 14:38:07 -04:00
|
|
|
/**
|
|
|
|
* set the default language
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $lang
|
|
|
|
*/
|
|
|
|
public static function setLanguageFallback($lang)
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (in_array($lang, self::getAvailableLanguages())) {
|
2015-10-18 14:38:07 -04:00
|
|
|
self::$_languageFallback = $lang;
|
2016-07-26 02:19:35 -04:00
|
|
|
}
|
2015-10-18 14:38:07 -04:00
|
|
|
}
|
|
|
|
|
2015-09-19 05:21:13 -04:00
|
|
|
/**
|
|
|
|
* get language file path
|
|
|
|
*
|
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $file
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function _getPath($file = '')
|
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (strlen(self::$_path) == 0) {
|
2015-09-19 05:21:13 -04:00
|
|
|
self::$_path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'i18n';
|
|
|
|
}
|
|
|
|
return self::$_path . (strlen($file) ? DIRECTORY_SEPARATOR . $file : '');
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:21:17 -04:00
|
|
|
/**
|
|
|
|
* determines the plural form to use based on current language and given number
|
|
|
|
*
|
2021-04-16 12:27:12 -04:00
|
|
|
* From: https://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
|
2015-09-06 13:21:17 -04:00
|
|
|
*
|
2015-09-19 05:21:13 -04:00
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param int $n
|
2015-09-06 13:21:17 -04:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected static function _getPluralForm($n)
|
|
|
|
{
|
|
|
|
switch (self::$_language) {
|
2019-06-23 06:06:36 -04:00
|
|
|
case 'cs':
|
2019-06-23 10:10:05 -04:00
|
|
|
return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
|
2015-09-06 13:21:17 -04:00
|
|
|
case 'fr':
|
2017-01-08 01:56:56 -05:00
|
|
|
case 'oc':
|
2016-04-26 14:21:30 -04:00
|
|
|
case 'zh':
|
2016-10-29 04:24:08 -04:00
|
|
|
return $n > 1 ? 1 : 0;
|
2021-01-07 15:16:03 -05:00
|
|
|
case 'he':
|
|
|
|
return $n === 1 ? 0 : ($n === 2 ? 1 : (($n < 0 || $n > 10) && ($n % 10 === 0) ? 2 : 3));
|
2021-03-08 23:54:06 -05:00
|
|
|
case 'id':
|
|
|
|
return 0;
|
2021-01-07 15:16:03 -05:00
|
|
|
case 'lt':
|
|
|
|
return $n % 10 === 1 && $n % 100 !== 11 ? 0 : (($n % 10 >= 2 && $n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2015-09-06 13:21:17 -04:00
|
|
|
case 'pl':
|
2016-12-25 05:37:45 -05:00
|
|
|
return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2016-12-16 04:15:37 -05:00
|
|
|
case 'ru':
|
2019-10-18 05:31:40 -04:00
|
|
|
case 'uk':
|
|
|
|
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2017-01-01 08:35:39 -05:00
|
|
|
case 'sl':
|
|
|
|
return $n % 100 == 1 ? 1 : ($n % 100 == 2 ? 2 : ($n % 100 == 3 || $n % 100 == 4 ? 3 : 0));
|
2021-04-16 12:27:12 -04:00
|
|
|
// bg, ca, de, en, es, et, hu, it, nl, no, pt
|
2015-09-06 13:21:17 -04:00
|
|
|
default:
|
2016-10-29 04:24:08 -04:00
|
|
|
return $n != 1 ? 1 : 0;
|
2015-09-06 13:21:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:24:56 -04:00
|
|
|
/**
|
|
|
|
* compares two language preference arrays and returns the preferred match
|
|
|
|
*
|
2016-07-19 07:56:52 -04:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-04 20:24:56 -04:00
|
|
|
*
|
2015-09-19 05:21:13 -04:00
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param array $acceptedLanguages
|
|
|
|
* @param array $availableLanguages
|
2015-09-04 20:24:56 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-26 02:19:35 -04:00
|
|
|
protected static function _getMatchingLanguage($acceptedLanguages, $availableLanguages)
|
|
|
|
{
|
2015-09-04 20:24:56 -04:00
|
|
|
$matches = array();
|
2016-08-15 10:45:47 -04:00
|
|
|
$any = false;
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach ($acceptedLanguages as $acceptedQuality => $acceptedValues) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$acceptedQuality = floatval($acceptedQuality);
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($acceptedQuality === 0.0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($availableLanguages as $availableValue) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$availableQuality = 1.0;
|
2016-07-26 02:19:35 -04:00
|
|
|
foreach ($acceptedValues as $acceptedValue) {
|
|
|
|
if ($acceptedValue === '*') {
|
2015-09-04 20:24:56 -04:00
|
|
|
$any = true;
|
|
|
|
}
|
|
|
|
$matchingGrade = self::_matchLanguage($acceptedValue, $availableValue);
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($matchingGrade > 0) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$q = (string) ($acceptedQuality * $availableQuality * $matchingGrade);
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!isset($matches[$q])) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$matches[$q] = array();
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if (!in_array($availableValue, $matches[$q])) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$matches[$q][] = $availableValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count($matches) === 0 && $any) {
|
|
|
|
if (count($availableLanguages) > 0) {
|
2015-09-04 20:24:56 -04:00
|
|
|
$matches['1.0'] = $availableLanguages;
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 02:19:35 -04:00
|
|
|
if (count($matches) === 0) {
|
2015-10-18 14:38:07 -04:00
|
|
|
return self::$_languageFallback;
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
krsort($matches);
|
|
|
|
$topmatches = current($matches);
|
|
|
|
return current($topmatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compare two language IDs and return the degree they match
|
|
|
|
*
|
2016-07-19 07:56:52 -04:00
|
|
|
* From: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
|
2015-09-04 20:24:56 -04:00
|
|
|
*
|
2015-09-19 05:21:13 -04:00
|
|
|
* @access protected
|
|
|
|
* @static
|
|
|
|
* @param string $a
|
|
|
|
* @param string $b
|
2015-09-04 20:24:56 -04:00
|
|
|
* @return float
|
|
|
|
*/
|
2016-07-26 02:19:35 -04:00
|
|
|
protected static function _matchLanguage($a, $b)
|
|
|
|
{
|
2015-09-04 20:24:56 -04:00
|
|
|
$a = explode('-', $a);
|
|
|
|
$b = explode('-', $b);
|
2016-08-15 10:45:47 -04:00
|
|
|
for ($i = 0, $n = min(count($a), count($b)); $i < $n; ++$i) {
|
2016-07-26 02:19:35 -04:00
|
|
|
if ($a[$i] !== $b[$i]) {
|
|
|
|
break;
|
|
|
|
}
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
return $i === 0 ? 0 : (float) $i / count($a);
|
|
|
|
}
|
|
|
|
}
|