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:
<?php
namespace Core;
use Core\Providers\Service;
/**
* Loads and sets the language <b>*.ini</b> files.
* @package Language
*/
class Language
{
/**
* @var mixed $instance A class instance
* @internal
*/
private static $instance;
/**
* @var string The "accept_lang" property of the client's browser
*/
protected $acceptLang;
/**
* @var bool (and equals !$acceptLang) whether the user is a bot or not.
* @note All browsers send the accept_lang parameter, so a lack of such property will mean a lack of browser.
*/
protected $isBot;
/**
* @var array The translations loaded for a given language
*/
protected $translations = array();
/**
* @var array The configuration setting for languages
*/
protected $languages = [
'default' => 'en',
'languages' => ['es', 'en']
];
/**
* @var Service The service model
*/
protected $service;
/**
*
* /**
* Returns the Language instance, creating it if it did not exist.
* @return Language
*/
public static function singleton()
{
if (!self::$instance) {
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
/**
* Populates the default properties of Language class
*/
public function __construct()
{
$this->acceptLang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : false;
$this->isBot = !$this->acceptLang;
$this->service = Service::singleton();
}
public function getModel() {
}
/**
* Sets the language for the user, by populating <code>$this->translations</code> for later use.
* @see function t()
*/
public function set() {
/* If it's a bot, we use the default language */
if ($this->isBot) {
$this->get_user_language(false);
/* If the language is already set in $_SESSION but we're NOT setting a new one through $_GET: */
} elseif (isset($_SESSION['lang']) && !isset($_GET['lang'])) { /* Only if we're not asking for a different language! */
$this->translations = Service::getLanguage($_SESSION['lang']);
/* Else: we set the language based on $_GET (if set) or based on the browser language */
} else {
$preferredLanguage = isset($_GET['lang']) ? $_GET['lang'] : $this->acceptLang;
$lang = $this->get_user_language($preferredLanguage);
$_SESSION['lang'] = $lang;
$uri = explode('?', $_SERVER['REQUEST_URI']);
if (isset($_GET['noredirect'])) {
$uri[0] = substr($uri[0],0,strlen($uri[0])-3);
}
$this->translations = Service::getLanguage($lang);
header ("Location: " . $uri[0]);
}
}
/**
* Searches and retrieves the translation associated to the $key. Returns $key if it doesn't exist
* (useful for the default language)
*
* @param string $key The translation key
*
* @return string|bool
*/
public function getTranslation($key) {
return array_key_exists($key, $this->translations) ? $this->translations[$key] : $key;
}
/**
* @param string|bool $preferredLanguage The language we will show to the user. Coincides with their browser
* language if it's translated
*
* @return string The user language (if it's translated) or the default (if it's not)
*/
protected function get_user_language($preferredLanguage = false) {
if (in_array($preferredLanguage, $this->languages['languages'])) {
return $preferredLanguage;
} else {
return $this->languages['default'];
}
}
}