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
|
2024-02-11 09:31:11 -05:00
|
|
|
* @version 1.7.1
|
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
|
|
|
|
2023-12-09 05:39:53 -05:00
|
|
|
use AppendIterator;
|
|
|
|
use GlobIterator;
|
|
|
|
|
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
|
2024-03-10 13:01:46 -04:00
|
|
|
* @param string|array $messageId
|
2015-09-04 20:24:56 -04:00
|
|
|
* @param mixed $args one or multiple parameters injected into placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-10 12:07:10 -04:00
|
|
|
public static function _($messageId, ...$args)
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
2022-10-25 01:15:09 -04:00
|
|
|
return forward_static_call_array('PrivateBin\I18n::translate', func_get_args());
|
2015-09-04 20:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* translate a string
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2024-03-10 13:01:46 -04:00
|
|
|
* @param string|array $messageId
|
2015-09-04 20:24:56 -04:00
|
|
|
* @param mixed $args one or multiple parameters injected into placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-03-10 12:07:10 -04:00
|
|
|
public static function translate($messageId, ...$args)
|
2015-09-04 20:24:56 -04:00
|
|
|
{
|
2016-07-26 02:19:35 -04:00
|
|
|
if (empty($messageId)) {
|
|
|
|
return $messageId;
|
|
|
|
}
|
2024-03-10 13:01:46 -04:00
|
|
|
if (empty(self::$_translations)) {
|
2016-07-26 02:19:35 -04:00
|
|
|
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
|
|
|
}
|
2024-03-10 12:07:10 -04:00
|
|
|
array_unshift($args, $messageId);
|
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);
|
2024-03-10 12:07:10 -04:00
|
|
|
for ($i = 0; $i < $argsCount; ++$i) {
|
|
|
|
if ($i > 0 ? !is_int($args[$i]) : strpos($args[0], '<a') === false) {
|
|
|
|
$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) {
|
2023-12-09 05:39:53 -05:00
|
|
|
self::$_availableLanguages[] = 'en'; // en.json is not part of the release archive
|
2023-12-09 06:00:57 -05:00
|
|
|
$languageIterator = new AppendIterator();
|
2023-12-09 05:39:53 -05:00
|
|
|
$languageIterator->append(new GlobIterator(self::_getPath('??.json')));
|
|
|
|
$languageIterator->append(new GlobIterator(self::_getPath('???.json'))); // for jbo
|
|
|
|
foreach ($languageIterator as $file) {
|
|
|
|
$language = $file->getBasename('.json');
|
|
|
|
if ($language != 'en') {
|
|
|
|
self::$_availableLanguages[] = $language;
|
2015-09-19 05:21:13 -04:00
|
|
|
}
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
|
2023-09-19 01:29:00 -04:00
|
|
|
/**
|
|
|
|
* determines if the current language is written right-to-left (RTL)
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isRtl()
|
|
|
|
{
|
|
|
|
return in_array(self::$_language, array('ar', 'he'));
|
|
|
|
}
|
|
|
|
|
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 = '')
|
|
|
|
{
|
2023-12-18 15:45:26 -05:00
|
|
|
if (empty(self::$_path)) {
|
2015-09-19 05:21:13 -04:00
|
|
|
self::$_path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'i18n';
|
|
|
|
}
|
2023-12-18 15:45:26 -05:00
|
|
|
return self::$_path . (empty($file) ? '' : DIRECTORY_SEPARATOR . $file);
|
2015-09-19 05:21:13 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:21:17 -04:00
|
|
|
/**
|
|
|
|
* determines the plural form to use based on current language and given number
|
|
|
|
*
|
2022-04-05 01:22:07 -04:00
|
|
|
* From: https://docs.translatehouse.org/projects/localization-guide/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) {
|
2023-08-01 08:00:45 -04:00
|
|
|
case 'ar':
|
|
|
|
return $n === 0 ? 0 : ($n === 1 ? 1 : ($n === 2 ? 2 : ($n % 100 >= 3 && $n % 100 <= 10 ? 3 : ($n % 100 >= 11 ? 4 : 5))));
|
2019-06-23 06:06:36 -04:00
|
|
|
case 'cs':
|
2022-09-28 23:34:49 -04:00
|
|
|
case 'sk':
|
|
|
|
return $n === 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
|
2022-02-24 14:05:19 -05:00
|
|
|
case 'co':
|
2015-09-06 13:21:17 -04:00
|
|
|
case 'fr':
|
2017-01-08 01:56:56 -05:00
|
|
|
case 'oc':
|
2022-04-28 14:05:57 -04:00
|
|
|
case 'tr':
|
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':
|
2023-08-01 08:00:45 -04:00
|
|
|
case 'ja':
|
2022-02-12 10:17:09 -05:00
|
|
|
case 'jbo':
|
2022-11-07 01:12:40 -05:00
|
|
|
case 'th':
|
2021-03-08 23:54:06 -05:00
|
|
|
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':
|
2022-09-28 23:34:49 -04:00
|
|
|
return $n === 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
2024-01-27 13:15:40 -05:00
|
|
|
case 'ro':
|
|
|
|
return $n === 1 ? 0 : (($n === 0 || ($n % 100 > 0 && $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':
|
2022-09-28 23:34:49 -04:00
|
|
|
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':
|
2022-09-28 23:34:49 -04:00
|
|
|
return $n % 100 === 1 ? 1 : ($n % 100 === 2 ? 2 : ($n % 100 === 3 || $n % 100 === 4 ? 3 : 0));
|
2015-09-06 13:21:17 -04:00
|
|
|
default:
|
2023-06-18 06:54:22 -04:00
|
|
|
// bg, ca, de, el, en, es, et, fi, hu, it, nl, no, pt
|
2022-09-28 23:34:49 -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);
|
|
|
|
}
|
|
|
|
}
|