From 184d3fe7d6b5d7a85203487a37ae02864e399929 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 13 Dec 2023 10:26:03 +0530 Subject: [PATCH 1/4] nextcloud snapppy lang fix --- plugins/nextcloud/index.php | 44 ++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/plugins/nextcloud/index.php b/plugins/nextcloud/index.php index 0a9a4bc7a..620d9b571 100644 --- a/plugins/nextcloud/index.php +++ b/plugins/nextcloud/index.php @@ -248,10 +248,52 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin public function FilterLanguage(&$sLanguage, $bAdmin) : void { if (!\RainLoop\Api::Config()->Get('webmail', 'allow_languages_on_settings', true)) { - $sLanguage = \strtr(\OC::$server->getL10N('core')->getLocaleCode(), '_', '-'); + $aResultLang = \json_decode(\file_get_contents(APP_VERSION_ROOT_PATH . 'app/localization/langs.json'), true); + if ($aResultLang === null) { + throw new \Exception('Error decoding JSON content.'); + } + $user = \OC::$server->getUserSession()->getUser(); + $userId = $user->getUID(); + $userLang = \OC::$server->getConfig()->getUserValue($userId, 'core', 'lang'); + $sLanguage = $this->determineLocale($langCode,$aResultLang['LANGS_NAMES_EN']); + // Check if $sLanguage is null + if ($sLanguage === null) { + $sLanguage = 'en'; // Assign 'en' if $sLanguage is null + } } } + /** + * Determine locale from user language. + * + * @param string $langCode The name of the input. + * @param array $languagesArray The value of the array. + * + * @return string return locale + */ + private function determineLocale (string $langCode, array $languagesArray) : string { + // Direct check for the language code + if (isset($languagesArray[$langCode])) { + return $langCode; + } + + // Check with uppercase country code + $langCodeWithUpperCase = $langCode . '-' . strtoupper($langCode); + if (isset($languagesArray[$langCodeWithUpperCase])) { + return $langCodeWithUpperCase; + } + + // Iterating to find a match starting with langCode + foreach ($languagesArray as $localeKey => $localeValue) { + if (strpos($localeKey, $langCode) === 0) { + return $localeKey; + } + } + + // If no match is found + return null; + } + /** * @param mixed $mResult */ From 3a46916ad918748401f964ba17c40ae52761a112 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 13 Dec 2023 15:39:53 +0530 Subject: [PATCH 2/4] set default lang --- plugins/nextcloud/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/nextcloud/index.php b/plugins/nextcloud/index.php index 620d9b571..2a65ee18d 100644 --- a/plugins/nextcloud/index.php +++ b/plugins/nextcloud/index.php @@ -254,8 +254,8 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin } $user = \OC::$server->getUserSession()->getUser(); $userId = $user->getUID(); - $userLang = \OC::$server->getConfig()->getUserValue($userId, 'core', 'lang'); - $sLanguage = $this->determineLocale($langCode,$aResultLang['LANGS_NAMES_EN']); + $userLang = \OC::$server->getConfig()->getUserValue($userId, 'core', 'lang','en'); + $sLanguage = $this->determineLocale($userLang,$aResultLang['LANGS_NAMES_EN']); // Check if $sLanguage is null if ($sLanguage === null) { $sLanguage = 'en'; // Assign 'en' if $sLanguage is null From d1e6ea379e2fa41c2b2847a1e52efb9271f8e86c Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 13 Dec 2023 16:07:44 +0530 Subject: [PATCH 3/4] use getLanguages --- plugins/nextcloud/index.php | 17 +++++++---------- .../v/0.0.0/app/libraries/snappymail/l10n.php | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/plugins/nextcloud/index.php b/plugins/nextcloud/index.php index 2a65ee18d..2fdc144e6 100644 --- a/plugins/nextcloud/index.php +++ b/plugins/nextcloud/index.php @@ -248,14 +248,11 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin public function FilterLanguage(&$sLanguage, $bAdmin) : void { if (!\RainLoop\Api::Config()->Get('webmail', 'allow_languages_on_settings', true)) { - $aResultLang = \json_decode(\file_get_contents(APP_VERSION_ROOT_PATH . 'app/localization/langs.json'), true); - if ($aResultLang === null) { - throw new \Exception('Error decoding JSON content.'); - } + $aResultLang = \SnappyMail\L10n::getLanguages(false); $user = \OC::$server->getUserSession()->getUser(); $userId = $user->getUID(); $userLang = \OC::$server->getConfig()->getUserValue($userId, 'core', 'lang','en'); - $sLanguage = $this->determineLocale($userLang,$aResultLang['LANGS_NAMES_EN']); + $sLanguage = $this->determineLocale($userLang,$aResultLang); // Check if $sLanguage is null if ($sLanguage === null) { $sLanguage = 'en'; // Assign 'en' if $sLanguage is null @@ -273,20 +270,20 @@ class NextcloudPlugin extends \RainLoop\Plugins\AbstractPlugin */ private function determineLocale (string $langCode, array $languagesArray) : string { // Direct check for the language code - if (isset($languagesArray[$langCode])) { + if (in_array($langCode, $languagesArray)) { return $langCode; } // Check with uppercase country code $langCodeWithUpperCase = $langCode . '-' . strtoupper($langCode); - if (isset($languagesArray[$langCodeWithUpperCase])) { + if (in_array($langCodeWithUpperCase, $languagesArray)) { return $langCodeWithUpperCase; } // Iterating to find a match starting with langCode - foreach ($languagesArray as $localeKey => $localeValue) { - if (strpos($localeKey, $langCode) === 0) { - return $localeKey; + foreach ($languagesArray as $localeValue) { + if (strpos($localeValue, $langCode) === 0) { + return $localeValue; } } diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php b/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php index ed056f0a9..637318e85 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php @@ -8,7 +8,7 @@ abstract class L10n /** * @staticvar array $aCache */ - public static function getLanguages(bool $bAdmin = false) : array + public static function (bool $bAdmin = false) : array { static $aCache = array(); From 0a4f83f5071bbe53412cff8a9aac6b147e2a3f5f Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 13 Dec 2023 16:28:25 +0530 Subject: [PATCH 4/4] use getLanguages --- snappymail/v/0.0.0/app/libraries/snappymail/l10n.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php b/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php index 637318e85..ed056f0a9 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/l10n.php @@ -8,7 +8,7 @@ abstract class L10n /** * @staticvar array $aCache */ - public static function (bool $bAdmin = false) : array + public static function getLanguages(bool $bAdmin = false) : array { static $aCache = array();