1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Kalkun
* An open source web based SMS Manager
*
* @copyright 2021 Fab Stz
* @author Fab Stz <fabstz-it@yahoo.fr>
* @license <https://spdx.org/licenses/GPL-2.0-or-later.html> GPL-2.0-or-later
* @link https://kalkun.sourceforge.io/
*/
/**
* Language Class
*/
class MY_Lang extends MX_Lang {
// Default to 'en'
public $locale = 'en';
private $idiom = '';
private $jquery_datepicker_regional;
public static $idiom_to_locale = [
'portuguese-brazilian' => 'pt_BR',
'czech' => 'cs',
'danish' => 'da',
'dutch' => 'nl',
'english' => 'en',
'finnish' => 'fi',
'french' => 'fr',
'german' => 'de',
'hungarian' => 'hu',
'indonesian' => 'in',
'italian' => 'it',
'norwegian' => 'no',
'polish' => 'pl',
'portuguese' => 'pt',
'russian' => 'ru',
'slovak' => 'sk',
'spanish' => 'es',
'turkish' => 'tr',
];
private $locale_matching_browser = NULL;
/**
* Class constructor
*
* @return void
*/
public function __construct()
{
parent::__construct();
if ( ! extension_loaded('intl'))
{
log_message('error', 'please install/enable the intl extension of PHP');
}
}
// --------------------------------------------------------------------
/**
* Load a language file
*
* @param mixed $langfile Language file name
* @param string $idiom Language name (english, etc.)
* @param bool $return Whether to return the loaded array of translations
* @param bool $add_suffix Whether to add suffix to $langfile
* @param string $alt_path Alternative path to look for the language file
* @param string $_module see MX_Loader
*
* @return void|string[] Array containing translations, if $return is set to TRUE
*/
public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '', $_module = '')
{
if ($idiom !== '')
{
$this->idiom = $idiom;
}
$requested_idiom = $this->idiom;
$langfile = str_replace('.php', '', $langfile);
if ($add_suffix === TRUE)
{
$langfile = preg_replace('/_lang$/', '', $langfile).'_lang';
}
$langfile .= '.php';
$found = FALSE;
if (file_exists(BASEPATH.'language/'.$this->idiom.'/'.$langfile))
{
$found = TRUE;
}
if ($alt_path !== '')
{
if (file_exists($alt_path.'language/'.$this->idiom.'/'.$langfile))
{
$found = TRUE;
}
}
else
{
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
{
$package_path .= 'language/'.$this->idiom.'/'.$langfile;
if (file_exists($package_path))
{
$found = TRUE;
break;
}
}
}
// Fallback to english if language file is not found
if ( ! $found)
{
log_message('error', "language file {$langfile} not found. Falling back to english.");
$requested_idiom = 'english';
}
parent::load($langfile, $requested_idiom, $return, $add_suffix, $alt_path, $_module);
if ( ! empty($idiom))
{
$this->locale = MY_LANG::$idiom_to_locale[$idiom];
}
}
// --------------------------------------------------------------------
/**
* Language line
*
* Fetches a single line of text from the language array
*
* @param string $line Language line key
* @param bool $log_errors Whether to log an error message if the line is not found
* @return string Translation
*/
public function line($line, $log_errors = TRUE)
{
return $this->__call('line', func_get_args());
}
/**
* Language line
*
* Fetches a single line of text from the language array
*
* @param string $line Language line key
* @param string $context context of the line (used to search in the nested array of the line)
* @param array $msg_params the arguments to pass to MessageFormatter::formatMessage
* @return string Translation
*/
private function line_kalkun($line, $context = NULL, ...$msg_params)
{
if ($context === NULL)
{
if (isset($this->language[$line]))
{
if (extension_loaded('intl'))
{
$value = MessageFormatter::formatMessage(
$this->locale,
$this->language[$line],
$msg_params
);
}
else
{
$value = parent::line($line);
}
}
else
{
$value = FALSE;
}
}
else
{
if (is_string($context))
{
if (isset($this->language[$line]) && isset($this->language[$line][$context]))
{
if (extension_loaded('intl'))
{
$value = MessageFormatter::formatMessage(
$this->locale,
$this->language[$line][$context],
$msg_params
);
}
else
{
$value = parent::line($line);
$value = $value[$context];
}
}
else
{
$value = FALSE;
}
}
else
{
log_message('error', 'context for message must be either NULL or a string');
}
}
//$value = isset($this->language[$line]) ? $this->language[$line] : FALSE;
// Because killer robots like unicorns!
if ($value === FALSE)
{
if (extension_loaded('intl'))
{
$value = MessageFormatter::formatMessage(
$this->locale,
'🌐 '.$line,
$msg_params
);
}
else
{
$value = '🌐 '.$line;
}
log_message('error', 'Could not find the language line "'.$line.'"');
}
return $value;
}
// https://stackoverflow.com/a/2147799/15401262
public function __call($method, $arguments)
{
if ($method === 'line')
{
if (count($arguments) === 0)
{
return call_user_func_array('parent::line', $arguments);
}
if (count($arguments) === 1)
{
return call_user_func_array(array($this, 'line_kalkun'), $arguments);
}
if (count($arguments) === 2)
{
if (is_bool($arguments[1]))
{
return call_user_func_array('parent::line', $arguments);
}
else
{
return call_user_func_array(array($this, 'line_kalkun'), $arguments);
}
}
else
{
if (count($arguments) >= 3)
{
return call_user_func_array(array($this, 'line_kalkun'), $arguments);
}
}
}
}
public static function locale_to_idiom ($locale)
{
$idiom_to_locale_lc = array_map('strtolower', MY_Lang::$idiom_to_locale);
$idiom = array_search(strtolower($locale), $idiom_to_locale_lc);
if ($idiom === FALSE)
{
$idiom = 'english';
}
return $idiom;
}
static function supported_locales()
{
return array_values(MY_Lang::$idiom_to_locale);
}
// https://www.codingwithjesse.com/blog/use-accept-language-header/
static function browser_accept_language()
{
$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1]))
{
// create a list like "en" => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val)
{
if ($val === '')
{
$langs[$lang] = 1;
}
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
return $langs;
}
function locale_matching_browser()
{
if (isset($this->locale_matching_browser))
{
return $this->locale_matching_browser;
}
$locale = NULL;
$supported_locales = MY_Lang::supported_locales();
//$supported_locales_short = array_map('MY_Lang::locale_language', $supported_locales);
//$supported_locales_all = array_merge($supported_locales, $supported_locales_short);
// look through sorted list and use first one that matches our languages
foreach (MY_Lang::browser_accept_language() as $lang => $val)
{
if (extension_loaded('intl'))
{
$locale = locale_lookup($supported_locales, $lang, TRUE, NULL);
}
else
{
if (in_array($lang, $supported_locales))
{
$locale = $lang;
}
}
if ( ! empty($locale))
{
$this->locale_matching_browser = $locale;
return $this->locale_matching_browser;
}
}
$this->locale_matching_browser = 'en';
return $this->locale_matching_browser;
}
static function locale_language($locale)
{
if (extension_loaded('intl'))
{
return Locale::parseLocale($locale)['language'];
}
else
{
$locale = explode('-', $locale)[0];
return explode('_', $locale)[0];
}
}
function get_idiom()
{
$locale = $this->locale_matching_browser();
$this->idiom = MY_Lang::locale_to_idiom($locale);
return $this->idiom;
}
/**
* Get the region/locale to use for the jquery ui datepicker
* based on the locale of the user settings.
*
* This is used to build the filename of the file containing the
* localized values for datepicker.
*
* Returns empty string if no match found.
*
* @param type $jquery_i18n_path
* @return string
*/
public function get_jquery_datepicker_regional($jquery_i18n_path)
{
if ( ! isset($this->jquery_datepicker_regional))
{
$datepicker_locales = [];
foreach (glob("{$jquery_i18n_path}/datepicker-*.js") as $filename)
{
$res = preg_match('/datepicker-(.*)\.js/', $filename, $matches);
array_push($datepicker_locales, $matches[1]);
}
$regional = Locale::lookup($datepicker_locales, $this->locale, FALSE, '');
$this->jquery_datepicker_regional = $regional;
}
return $this->jquery_datepicker_regional;
}
public function kalkun_supported_languages()
{
$supported_languages = [];
foreach (MY_Lang::$idiom_to_locale as $key => $value)
{
if (extension_loaded('intl'))
{
$supported_languages[$key] = Locale::getDisplayName($value, $value);
}
else
{
$supported_languages[$key] = $key;
}
}
natcasesort($supported_languages);
return $supported_languages;
}
public static function idom_to_region($idiom)
{
$locale = MY_LANG::$idiom_to_locale[$idiom];
if (strlen($locale) === 2)
{
return Locale::getRegion('-'.$locale);
}
else
{
return Locale::getRegion($locale);
}
}
}
|