From 00ead7b856a55712a4003fe5b5c5f9148f48ff01 Mon Sep 17 00:00:00 2001 From: Eduardo Robles Date: Thu, 16 Nov 2023 10:40:17 +0100 Subject: [PATCH] =?UTF-8?q?=20=E2=9C=A8=20Select=20languages=20per=20elect?= =?UTF-8?q?ion=20(#369)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parent issue: https://github.com/sequentech/meta/issues/306 --- .../change-lang-directive.js | 15 +++++++++- avUi/i18n-override-service.js | 29 +++++++++++++++---- dist/appCommon-vmaster.js | 12 +++++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/avUi/change-lang-directive/change-lang-directive.js b/avUi/change-lang-directive/change-lang-directive.js index ca7ac143..590e9931 100644 --- a/avUi/change-lang-directive/change-lang-directive.js +++ b/avUi/change-lang-directive/change-lang-directive.js @@ -26,6 +26,7 @@ angular.module('avUi') ipCookie, angularLoad, amMoment, + $rootScope, ConfigService, $window, I18nOverride, @@ -45,6 +46,17 @@ angular.module('avUi') } element.on('click', triggerDropdown); + // detect language changes + $rootScope.$on( + 'i18nextLanguageChange', + function (event, languageCode) + { + scope.deflang = languageCode; + scope.langs = $i18next.options.lngWhitelist; + scope.$apply(); + } + ); + // Changes i18n to a specific language, setting also a cookie for // remembering it, and updating all the translations instantly. // @@ -79,7 +91,8 @@ angular.module('avUi') ipCookie( "lang", lang, - _.extend(cookieConf, ConfigService.i18nextCookieOptions)); + _.extend(cookieConf, ConfigService.i18nextCookieOptions) + ); scope.deflang = lang; angular.element('#ng-app').attr('lang', scope.deflang); diff --git a/avUi/i18n-override-service.js b/avUi/i18n-override-service.js index 79f36f5a..aea184b7 100644 --- a/avUi/i18n-override-service.js +++ b/avUi/i18n-override-service.js @@ -38,7 +38,7 @@ angular 'I18nOverride', function($i18next, $rootScope, $window) { - return function (overrides, force) + return function (overrides, force, languagesConf) { force = angular.isDefined(force) ? force : false; overrides = overrides === null ? $window.i18nOverride : overrides; @@ -53,6 +53,20 @@ angular $window.i18nOverride = overrides; } + if (languagesConf) + { + // For some reason it seems that `$i18next.options.lng` gets desynced + // from `$window.i18n.lng()`. This might result in an unexpected + // language change when the init() function from $i18next gets called + // later in this code. For this reason, we set the correct language in + // `$i18next.options.lng` to ensure that doesn't happen. + $i18next.options.lng = (languagesConf.force_default_language) ? + languagesConf.default_language : $window.i18n.lng(); + + $i18next.options.lngWhitelist = languagesConf.available_languages; + $i18next.options.fallbackLng = [languagesConf.default_language, 'en']; + } + // load i18n_overrides if any if (performOverrides) { @@ -76,11 +90,16 @@ angular ); } ); - $rootScope.$broadcast( - 'i18nextLanguageChange', - $window.i18n.lng() - ); } + + // This will trigget a $i18next's init function to be called and all + // angularjs $i18next translations to be updated accordingly. + // `i18nextLanguageChange` signal is special for $i18next, see its code + // for reference. + $rootScope.$emit( + 'i18nextLanguageChange', + $i18next.options.lng + ); }; } ); diff --git a/dist/appCommon-vmaster.js b/dist/appCommon-vmaster.js index cd28ceb7..143923b8 100644 --- a/dist/appCommon-vmaster.js +++ b/dist/appCommon-vmaster.js @@ -1121,17 +1121,19 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist }); }; } ]), angular.module("avUi").service("I18nOverride", [ "$i18next", "$rootScope", "$window", function($i18next, $rootScope, $window) { - return function(overrides, force) { + return function(overrides, force, languagesConf) { force = !!angular.isDefined(force) && force; var performOverrides = !1; (overrides = null === overrides ? $window.i18nOverride : overrides) && (performOverrides = force || JSON.stringify(overrides) !== JSON.stringify($window.i18nOverride), - $window.i18nOverride = overrides), performOverrides && (_.map($window.i18nOverride, function(i18nOverride, language) { + $window.i18nOverride = overrides), languagesConf && ($i18next.options.lng = languagesConf.force_default_language ? languagesConf.default_language : $window.i18n.lng(), + $i18next.options.lngWhitelist = languagesConf.available_languages, $i18next.options.fallbackLng = [ languagesConf.default_language, "en" ]), + performOverrides && _.map($window.i18nOverride, function(i18nOverride, language) { $window.i18n.addResources(language, "translation", i18nOverride), _.each(_.keys(i18nOverride), function(i18nString) { $i18next(i18nString, {}); }); - }), $rootScope.$broadcast("i18nextLanguageChange", $window.i18n.lng())); + }), $rootScope.$emit("i18nextLanguageChange", $i18next.options.lng); }; -} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "ConfigService", "$window", "I18nOverride", "Authmethod", function($i18next, ipCookie, angularLoad, amMoment, ConfigService, $window, I18nOverride, Authmethod) { +} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "$rootScope", "ConfigService", "$window", "I18nOverride", "Authmethod", function($i18next, ipCookie, angularLoad, amMoment, $rootScope, ConfigService, $window, I18nOverride, Authmethod) { return { restrict: "AE", scope: {}, @@ -1143,6 +1145,8 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist setTimeout(function() { angular.element("#lang-dropdown-toggle").click(); }, 0); + }), $rootScope.$on("i18nextLanguageChange", function(event, languageCode) { + scope.deflang = languageCode, scope.langs = $i18next.options.lngWhitelist, scope.$apply(); }), scope.changeLang = function(lang) { $i18next.options.lng = lang, isAdmin || ($i18next.options.useLocalStorage = !0), angular.isDefined($window.i18nOverride) && $window.i18n.preload([ lang ], function() {