Skip to content

Commit

Permalink
load i18n overrides during language changes (#232)
Browse files Browse the repository at this point in the history
load i18n overrides during language changes (#231)

* load i18n overrides during language changes

* adding i18noverride service
  • Loading branch information
edulix authored Jul 21, 2022
1 parent 8664381 commit 60d1bcd
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 6 deletions.
34 changes: 30 additions & 4 deletions avUi/change-lang-directive/change-lang-directive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* This file is part of common-ui.
* Copyright (C) 2015-2016 Sequent Tech Inc <legal@sequentech.io>
* Copyright (C) 2015-2022 Sequent Tech Inc <legal@sequentech.io>
* 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
Expand All @@ -21,8 +21,17 @@
* <li class="dropdown" av-change-lang></li>
*/
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;
Expand All @@ -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,
Expand Down
92 changes: 92 additions & 0 deletions avUi/i18n-override-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* This file is part of common-ui.
* Copyright (C) 2022 Eduardo Robles <edu@sequentech.io>
* 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 <http://www.gnu.org/licenses/>.
**/

/**
* 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()
);
}
);
}
};
}
);
19 changes: 17 additions & 2 deletions dist/appCommon-vmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "/"
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<script src="avUi/simple-error-directive/simple-error-directive.js" class="app"></script>
<script src="avUi/confirm-modal-controller/confirm-modal-controller.js" class="app"></script>
<script src="avUi/show-versions-modal-service.js" class="app"></script>
<script src="avUi/i18n-override-service.js" class="app"></script>
<script src="avUi/change-lang-directive/change-lang-directive.js" class="app"></script>
<script src="avUi/affix-bottom-directive.js" class="app"></script>
<script src="avUi/auto-height-directive.js" class="app"></script>
Expand Down

0 comments on commit 60d1bcd

Please sign in to comment.