diff --git a/avUi/change-lang-directive/change-lang-directive.js b/avUi/change-lang-directive/change-lang-directive.js index ffe87967..d006d8c6 100644 --- a/avUi/change-lang-directive/change-lang-directive.js +++ b/avUi/change-lang-directive/change-lang-directive.js @@ -1,6 +1,6 @@ /** * This file is part of common-ui. - * Copyright (C) 2015-2016 Sequent Tech Inc + * Copyright (C) 2015-2022 Sequent Tech Inc * common-ui is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -21,8 +21,17 @@ * */ angular.module('avUi') - .directive('avChangeLang', function($i18next, ipCookie, angularLoad, amMoment, ConfigService) { - function link(scope, element, attrs) { + .directive('avChangeLang', function( + $i18next, + ipCookie, + angularLoad, + amMoment, + ConfigService, + $window, + I18nOverride + ) { + function link(scope, element, attrs) + { scope.deflang = window.i18n.lng(); angular.element('#ng-app').attr('lang', scope.deflang); scope.langs = $i18next.options.lngWhitelist; @@ -31,8 +40,25 @@ angular.module('avUi') // remembering it, and updating all the translations instantly. // // Triggered when the user clicks and selects a language. - scope.changeLang = function(lang) { + scope.changeLang = function(lang) + { $i18next.options.lng = lang; + + // load i18n_overrides if any + if (angular.isDefined($window.i18nOverride)) + { + $window.i18n.preload( + [lang], + function () + { + I18nOverride( + /* overrides = */ null, // set to use the default, $window.i18nOverride + /* force = */ true + ); + } + ); + } + console.log("setting cookie"); var cookieConf = { expires: 360, diff --git a/avUi/i18n-override-service.js b/avUi/i18n-override-service.js new file mode 100644 index 00000000..c5b8b0a6 --- /dev/null +++ b/avUi/i18n-override-service.js @@ -0,0 +1,92 @@ +/** + * This file is part of common-ui. + * Copyright (C) 2022 Eduardo Robles + + * common-ui is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License. + + * common-ui is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with common-ui. If not, see . +**/ + +/** + * Overrides i18next translations. Example: + * + * ```javascript + * var override = {"en": {"avBooth.castTheBallot": "Cast thy ballot"}}; + * I18nOverride(override); + * ``` + * + * It has two optional parameters: + * + * - overrides: dictionary with the overrides (see example above). If it's empty + * it will just use the overrides saved in $window.i18nOverride. + * + * - force: false by default. This means that overrides will not be applied if + * the provided overrides are the same as before (i.e. same as + * $window.i18nOverride). + */ +angular + .module('avUi') + .service( + 'I18nOverride', + function($i18next, $rootScope, $window) + { + return function (overrides, force) + { + force = angular.isDefined(force) ? force : false; + overrides = overrides === null ? $window.i18nOverride : overrides; + + // reset $window.i18nOverride + var performOverrides = false; + if (overrides) { + performOverrides = ( + force || + JSON.stringify(overrides) !== JSON.stringify($window.i18nOverride) + ); + $window.i18nOverride = overrides; + } + + // load i18n_overrides if any + if (performOverrides) + { + $window.i18n.preload( + _.keys($window.i18nOverride), + function () + { + _.map( + $window.i18nOverride, + function (i18nOverride, language) + { + $window.i18n.addResources( + /* lng = */ language, + /* ns = */ "translation", + /* resources = */ i18nOverride + ); + + // force-refresh cached translations to override + _.each( + _.keys(i18nOverride), + function (i18nString) + { + $i18next(i18nString, {}); + } + ); + } + ); + $rootScope.$broadcast( + 'i18nextLanguageChange', + $window.i18n.lng() + ); + } + ); + } + }; + } + ); diff --git a/dist/appCommon-vmaster.js b/dist/appCommon-vmaster.js index fd07a40a..5fce2fd1 100644 --- a/dist/appCommon-vmaster.js +++ b/dist/appCommon-vmaster.js @@ -1007,14 +1007,29 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist } }); }; -} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "ConfigService", function($i18next, ipCookie, angularLoad, amMoment, ConfigService) { +} ]), angular.module("avUi").service("I18nOverride", [ "$i18next", "$rootScope", "$window", function($i18next, $rootScope, $window) { + return function(overrides, force) { + 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 && $window.i18n.preload(_.keys($window.i18nOverride), function() { + _.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()); + }); + }; +} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "ConfigService", "$window", "I18nOverride", function($i18next, ipCookie, angularLoad, amMoment, ConfigService, $window, I18nOverride) { return { restrict: "AE", scope: {}, link: function(scope, element, attrs) { scope.deflang = window.i18n.lng(), angular.element("#ng-app").attr("lang", scope.deflang), scope.langs = $i18next.options.lngWhitelist, scope.changeLang = function(lang) { - $i18next.options.lng = lang, console.log("setting cookie"); + $i18next.options.lng = lang, angular.isDefined($window.i18nOverride) && $window.i18n.preload([ lang ], function() { + I18nOverride(null, !0); + }), console.log("setting cookie"); ipCookie("lang", lang, _.extend({ expires: 360, path: "/" diff --git a/index.html b/index.html index 6e2530e1..1b102601 100755 --- a/index.html +++ b/index.html @@ -94,6 +94,7 @@ +