Example #1
0
 /**
  * Returns a singleton reference of this class.
  *
  * @author Stefan Herndler
  * @since 1.0.0
  * @return WPT_Storage
  */
 public static function singleton()
 {
     // initialize singleton if first call
     if (!self::$a_obj_Singleton instanceof WPT_Storage) {
         self::$a_obj_Singleton = new self();
     }
     // return the singleton of this class
     return self::$a_obj_Singleton;
 }
 /**
  * Loads and initializes the internal storage.
  *
  * @author Stefan Herndler
  * @since 1.0.0
  */
 private function loadInternalStorage()
 {
     // require the class which handles the internal storage
     require_once dirname(__FILE__) . '/Storage.php';
     // get the singleton once to call the constructor and initialize the class
     WBR_Storage::singleton();
 }
 /**
  * Returns the language code (2 characters) in all lowercase used for translations.
  * The language code is determined using the following priority:
  *  1) internal storage (manually set by the visitor itself)
  *  2) specified by WPML plugin if installed and activated
  *  3) using the browser's locale
  *
  * @author Stefan Herndler
  * @since 1.0.0
  * @return string
  */
 private function determineLanguageCode()
 {
     // check if language code is specified in the internal storage (manually switched by the visitor)
     $l_str_LanguageCode = WBR_Storage::singleton()->readLanguageCode();
     if (strlen($l_str_LanguageCode) === 2) {
         return $l_str_LanguageCode;
     }
     // get the language code of the optional WordPress plugin 'WPML' if plugin is activated
     if (defined('ICL_LANGUAGE_CODE') && strlen(ICL_LANGUAGE_CODE) === 2) {
         return strtolower(strval(ICL_LANGUAGE_CODE));
     }
     // extract the locale specified by the http header (browser)
     $l_arr_Locale = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
     // check if locale is specified in the browser
     if (count($l_arr_Locale) === 0) {
         // no locale specified, return the default value
         return WPT_CONFIG_DEFAULT_LANGUAGE_CODE;
     }
     // extract the locale from the browser variable
     $l_str_Locale = $l_arr_Locale[0];
     // check if locale is split by an underscore
     if (strlen($l_str_Locale) === 5 && strpos($l_str_Locale, "_") !== false) {
         // extract the language code out of the locale
         return strtolower(explode('_', $l_str_Locale)[0]);
     }
     // check if locale is split by a minus
     if (strlen($l_str_Locale) === 5 && strpos($l_str_Locale, "-") !== false) {
         // extract the language code out of the locale
         return strtolower(explode('-', $l_str_Locale)[0]);
     }
     // unknown separator of language code and country code inside the locale
     // either the browser's locale is already the language code (only 2 characters) otherwise the default language will be used
     return strlen($l_str_Locale) === 2 ? strtolower($l_str_Locale) : WBR_CONFIG_DEFAULT_LANGUAGE_CODE;
 }