Skip to content

Commit

Permalink
✨ Select languages per election (#369)
Browse files Browse the repository at this point in the history
Parent issue: sequentech/meta#306
  • Loading branch information
edulix committed Nov 16, 2023
1 parent 0c6874b commit 00ead7b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
15 changes: 14 additions & 1 deletion avUi/change-lang-directive/change-lang-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ angular.module('avUi')
ipCookie,
angularLoad,
amMoment,
$rootScope,
ConfigService,
$window,
I18nOverride,
Expand All @@ -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.
//
Expand Down Expand Up @@ -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);

Expand Down
29 changes: 24 additions & 5 deletions avUi/i18n-override-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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
);
};
}
);
12 changes: 8 additions & 4 deletions dist/appCommon-vmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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() {
Expand Down

0 comments on commit 00ead7b

Please sign in to comment.