- return self::$lang;
- }
-
- /**
- * Sets the language session variable
- */
- public static function setSessionVariable()
- {
- if (!empty($_SESSION['authenticated']) && empty($_SESSION['language'])) {
- $_SESSION['language'] = self::$lang;
- // we haven't loaded user data yet, but we need user language
- if (!empty($_SESSION['uid'])) {
- $user = DBA::selectFirst('user', ['language'], ['uid' => $_SESSION['uid']]);
- if (DBA::isResult($user)) {
- $_SESSION['language'] = $user['language'];
- }
- }
- }
-
- if (isset($_GET['lang'])) {
- $_SESSION['language'] = $_GET['lang'];
- }
- }
-
- public static function setLangFromSession()
- {
- if (!empty($_SESSION['language']) && $_SESSION['language'] !== self::$lang) {
- self::loadTranslationTable($_SESSION['language']);
- }
- }
-
- /**
- * @brief Returns the preferred language from the HTTP_ACCEPT_LANGUAGE header
- * @return string The two-letter language code
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- public static function detectLanguage()
- {
- $lang_list = [];
-
- if (!empty($_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])) {
- // go through the list of prefered languages and add a generic language
- // for sub-linguas (e.g. de-ch will add de) if not already in array
- for ($i = 0; $i < count($lang_parse[1]); $i++) {
- $lang_list[] = strtolower($lang_parse[1][$i]);
- if (strlen($lang_parse[1][$i])>3) {
- $dashpos = strpos($lang_parse[1][$i], '-');
- if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list)) {
- $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
- }
- }
- }
- }
- }
-
- if (isset($_GET['lang'])) {
- $lang_list = [$_GET['lang']];
- }
-
- // check if we have translations for the preferred languages and pick the 1st that has
- foreach ($lang_list as $lang) {
- if ($lang === 'en' || (file_exists("view/lang/$lang") && is_dir("view/lang/$lang"))) {
- $preferred = $lang;
- break;
- }
- }
- if (isset($preferred)) {
- return $preferred;
- }
-
- // in case none matches, get the system wide configured language, or fall back to English
- return Config::get('system', 'language', 'en');
- }
-
- /**
- * This function should be called before formatting messages in a specific target language
- * different from the current user/system language.
- *
- * It saves the current translation strings in a separate variable and loads new translations strings.
- *
- * If called repeatedly, it won't save the translation strings again, just load the new ones.
- *
- * @see popLang()
- * @brief Stores the current language strings and load a different language.
- * @param string $lang Language code
- * @throws \Exception
- */
- public static function pushLang($lang)
- {
- if (!self::$lang) {
- self::init();
- }
-
- if ($lang === self::$lang) {
- return;
- }
-
- if (!self::$langSave) {
- self::$langSave = self::$lang;
- self::$stringsSave = self::$strings;
- }
-
- self::loadTranslationTable($lang);
- }
-
- /**
- * Restores the original user/system language after having used pushLang()
- */
- public static function popLang()
- {
- if (!self::$langSave) {
- return;
- }
-
- self::$strings = self::$stringsSave;
- self::$lang = self::$langSave;
-
- self::$stringsSave = [];
- self::$langSave = '';