From 9bec1d0a8f5e99e60c3502a0f4829328279dfd38 Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Mon, 16 Mar 2015 17:54:56 +0400 Subject: [PATCH] Language popup otimizations --- dev/Stores/Language.js | 9 ++- dev/View/Popup/Languages.js | 32 +++++++++ .../0.0.0/app/libraries/RainLoop/Actions.php | 68 ++++++++++++++----- .../libraries/RainLoop/Config/Application.php | 1 + .../v/0.0.0/app/libraries/RainLoop/Utils.php | 8 +-- .../Views/Common/PopupsLanguages.html | 14 +++- 6 files changed, 109 insertions(+), 23 deletions(-) diff --git a/dev/Stores/Language.js b/dev/Stores/Language.js index fa521c456..028813c8a 100644 --- a/dev/Stores/Language.js +++ b/dev/Stores/Language.js @@ -4,6 +4,7 @@ 'use strict'; var + _ = require('_'), ko = require('ko'), Utils = require('Common/Utils'), @@ -17,6 +18,7 @@ function LanguageStore() { this.languages = ko.observableArray([]); + this.languagesTop = ko.observableArray([]); this.language = ko.observable('') .extend({'limitedList': this.languages}); @@ -24,9 +26,14 @@ LanguageStore.prototype.populate = function () { - var aLanguages = Settings.settingsGet('Languages'); + var + aLanguages = Settings.settingsGet('Languages'), + aLanguagesTop = Settings.settingsGet('LanguagesTop') + ; this.languages(Utils.isArray(aLanguages) ? aLanguages : []); + this.languagesTop(Utils.isArray(aLanguagesTop) ? aLanguagesTop : []); + this.language(Settings.settingsGet('Language')); }; diff --git a/dev/View/Popup/Languages.js b/dev/View/Popup/Languages.js index b8f23dda0..95ee506b6 100644 --- a/dev/View/Popup/Languages.js +++ b/dev/View/Popup/Languages.js @@ -35,6 +35,38 @@ }); }, this); + this.languagesTop = ko.computed(function () { + + var + aTop = this.LanguageStore.languagesTop(), + aLangs = this.languages() + ; + + return 0 < aTop.length ? _.compact(_.map(aTop, function (sLang) { + return _.find(aLangs, function (aItem) { + return aItem && sLang === aItem.key; + }); + })) : []; + }, this); + + this.languagesBottom = ko.computed(function () { + + var + aTop = this.languagesTop(), + aLangs = this.languages() + ; + + if (0 < aTop.length) + { + return _.filter(aLangs, function (aItem) { + return -1 === Utils.inArray(aItem, aTop); + }); + } + + return aLangs; + + }, this); + this.LanguageStore.language.subscribe(function () { this.resetMainLanguage(); }, this); diff --git a/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php b/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php index f177834f7..5b55403c9 100644 --- a/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php +++ b/rainloop/v/0.0.0/app/libraries/RainLoop/Actions.php @@ -1550,6 +1550,7 @@ class Actions $aResult['Themes'] = $this->GetThemes(); $aResult['Languages'] = $this->GetLanguages(); + $aResult['LanguagesTop'] = $this->GetLanguagesTop(); $aResult['AllowLanguagesOnSettings'] = (bool) $oConfig->Get('webmail', 'allow_languages_on_settings', true); $aResult['AllowLanguagesOnLogin'] = (bool) $oConfig->Get('login', 'allow_languages_on_login', true); $aResult['AttachmentLimit'] = ((int) $oConfig->Get('webmail', 'attachment_size_limit', 10)) * 1024 * 1024; @@ -1693,16 +1694,29 @@ class Actions return $aResult; } - private function detectUserLanguage() + /** + * @return string + */ + private function getUserLanguageFromHeader() { $sLang = ''; $sAcceptLang = $this->Http()->GetServer('HTTP_ACCEPT_LANGUAGE', 'en'); if (false !== \strpos($sAcceptLang, ',')) { $aParts = \explode(',', $sAcceptLang, 2); - $sLang = !empty($aParts[0]) ? \strtolower($aParts[0]) : ''; + $sLang = !empty($aParts[0]) ? \trim(\strtolower($aParts[0])) : ''; } + return $sLang; + } + + /** + * @return string + */ + private function detectUserLanguage() + { + $sLang = $this->getUserLanguageFromHeader(); + if (!empty($sLang)) { $sLang = \preg_replace('/[^a-zA-Z0-9]+/', '-', $sLang); @@ -8384,8 +8398,8 @@ class Actions return $aCache; } - $bEn = false; - $sList = array(); + $aList = array(); + $sDir = APP_VERSION_ROOT_PATH.'langs/'; if (@\is_dir($sDir)) { @@ -8399,14 +8413,7 @@ class Actions $sLang = \strtolower(\substr($sFile, 0, -4)); if (0 < \strlen($sLang) && 'always' !== $sLang) { - if (\in_array($sLang, array('en'))) - { - $bEn = true; - } - else - { - \array_push($sList, $sLang); - } + \array_push($aList, $sLang); } } } @@ -8415,14 +8422,41 @@ class Actions } } - \sort($sList); - if ($bEn) + \sort($aList); + + $aCache = $aList; + return $aCache; + } + + public function GetLanguagesTop() + { + $sUserLang = $this->getUserLanguageFromHeader(); + if (2 < \strlen($sUserLang)) { - \array_unshift($sList, 'en'); + $sUserLang = \substr($sUserLang, 0, 2); } - $aCache = $sList; - return $sList; + $self = $this; + $aResult = array(); + + foreach ($this->GetLanguages() as $sLang) + { + if ($sUserLang === \substr($sLang, 0, 2)) + { + $aResult[] = $sLang; + } + } + + $aTopLangs = \array_map('trim', \explode(',', $this->Config()->Get('labs', 'top_langs', 'en'))); + + $aResult = \array_merge($aResult, $aTopLangs); + $aResult = \array_unique($aResult); + + $aResult = \array_values(\array_filter($aResult, function ($sLang) use ($self) { + return $sLang === $self->ValidateLanguage($sLang); + })); + + return $aResult; } /** diff --git a/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php b/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php index 0ef1d88cd..5dedf790b 100644 --- a/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php +++ b/rainloop/v/0.0.0/app/libraries/RainLoop/Config/Application.php @@ -299,6 +299,7 @@ Enables caching in the system'), 'sieve_utf8_folder_name' => array(true), 'curl_proxy' => array(''), 'curl_proxy_auth' => array(''), + 'top_langs' => array('en'), 'in_iframe' => array(false), 'force_https' => array(false), 'custom_login_link' => array(''), diff --git a/rainloop/v/0.0.0/app/libraries/RainLoop/Utils.php b/rainloop/v/0.0.0/app/libraries/RainLoop/Utils.php index 12e49f1da..b85f33c99 100644 --- a/rainloop/v/0.0.0/app/libraries/RainLoop/Utils.php +++ b/rainloop/v/0.0.0/app/libraries/RainLoop/Utils.php @@ -366,10 +366,10 @@ class Utils */ public static function CustomParseIniFile($sFileName, $bProcessSections = false) { - if (\MailSo\Base\Utils::FunctionExistsAndEnabled('parse_ini_file')) - { - return @\parse_ini_file($sFileName, !!$bProcessSections); - } +// if (\MailSo\Base\Utils::FunctionExistsAndEnabled('parse_ini_file')) +// { +// return @\parse_ini_file($sFileName, !!$bProcessSections); +// } $sData = @\file_get_contents($sFileName); return \is_string($sData) ? @\parse_ini_string($sData, !!$bProcessSections) : null; diff --git a/rainloop/v/0.0.0/app/templates/Views/Common/PopupsLanguages.html b/rainloop/v/0.0.0/app/templates/Views/Common/PopupsLanguages.html index e825ba690..fc5418c6b 100644 --- a/rainloop/v/0.0.0/app/templates/Views/Common/PopupsLanguages.html +++ b/rainloop/v/0.0.0/app/templates/Views/Common/PopupsLanguages.html @@ -7,7 +7,19 @@