use Waystone\Workspaces\Engines\Controller\LoadableWithContext;
use Waystone\Workspaces\Engines\Framework\Context;
-/**
- * Gets a specified language expression defined in configuration file
- *
- * @param string $key the configuration key matching the value to get
- * @return string The value in the configuration file
- * @deprecated
- */
-function lang_get ($key) {
- trigger_error("The use of the L10n global functions is deprecated. Call Language::get('$key') instead.", E_USER_DEPRECATED);
- return Language::get($key);
-}
-
/**
* Language services
*/
class Language implements LoadableWithContext {
///
/// Properties
///
/**
* @var
*/
const FALLBACK = 'en';
/**
* @var Smarty the template engine
*/
private $templateEngine;
///
/// Singleton pattern. Constructor.
///
/**
* @var Language The loaded Language instance
*/
private static $instance;
/**
* Loads an instance of the class
*
* @param ?Context $context The context
* @return Language An instance of the Language class
*/
public static function Load (?Context $context = null) {
if (static::$instance === null) {
//Initializes an instance
if ($context === null) {
throw new InvalidArgumentException("A context is required to load this class for the first time.");
}
if ($context->templateEngine === null) {
throw new InvalidArgumentException("A context is required to load this class for the first time. You provided one, but the template engine isn't initiliazed. This is required, as the languages files are managed by the template engine.");
}
static::$instance = new static($context->templateEngine);
}
return static::$instance;
}
/**
* Initializes a new instance of the Language class
*/
public function __construct ($templateEngine) {
$this->templateEngine = $templateEngine;
}
///
/// Static helper methods
///
/**
* Defines the LANG constant, to lang to print
*
* This information is contained in the session, or if not yet defined,
* it's to determine according the user's browser preferences.
* @see findLanguage
*/
public static function initialize () {
//If $_SESSION['lang'] doesn't exist yet, find a common language
if (!array_key_exists('lang', $_SESSION)) {
$lang = static::findLanguage();
$_SESSION['lang'] = $lang ? $lang : '-';
}
if ($_SESSION['lang'] != '-') {
define('LANG', $_SESSION['lang']);
}
}
/**
* Gets a common lang spoken by the site and the user's browser
* @see Language::getHttpAcceptLanguages
*
* @return string the language
*/
public static function findLanguage () {
if (file_exists('lang') && is_dir('lang')) {
//Gets lang/ subdirectories: this is the list of available languages