Skip to content

Commit

Permalink
🐞 default translation appears when switching language for the first t…
Browse files Browse the repository at this point in the history
…ime (#375)

Parent issue: sequentech/meta#315

We use now i18next latest version now
  • Loading branch information
edulix committed Feb 8, 2024
1 parent 7fc61d5 commit 2c60743
Show file tree
Hide file tree
Showing 10 changed files with 803 additions and 1,133 deletions.
10 changes: 8 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ module.exports = function (grunt) {
'dist/SequentThemes-vmaster.js': ['SequentThemes.js'],
'dist/SequentPlugins-vmaster.js': ['plugins/**/*.js']
}
}
},
postUglify: {
files: {
'dist/libCommon-v10.1.0.js': ['dist/libCommon-v10.1.0.js', 'node_modules/i18next/dist/umd/i18next.js']
}
},
},
"merge-json": {
main: {
Expand Down Expand Up @@ -385,10 +390,11 @@ module.exports = function (grunt) {
'dom_munger',
'ngtemplates',
'cssmin',
'concat',
'concat:main',
'merge-json',
'ngAnnotate',
'uglify',
'concat:postUglify',
'copy:main',
'htmlmin',
'imagemin',
Expand Down
19 changes: 0 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ angular.module(
'ng-autofocus'
]);

angular.module('jm.i18next').config(function ($i18nextProvider, ConfigServiceProvider) {
// note that we do not send the language: by default, it will try the language
// supported by the web browser
$("#no-js").hide();

$i18nextProvider.options = _.extend(
{
useCookie: true,
useLocalStorage: false,
fallbackLng: 'en',
cookieName: 'lang',
detectLngQS: 'lang',
lngWhitelist: ['en', 'es', 'gl', 'ca'],
resGetPath: '/locales/__lng__.json',
defaultLoadingValue: '' // ng-i18next option, *NOT* directly supported by i18next
},
ConfigServiceProvider.i18nextInitOptions);
});

angular.module('common-ui').run(function($http, $rootScope) {

$rootScope.safeApply = function(fn) {
Expand Down
31 changes: 9 additions & 22 deletions avUi/change-lang-directive/change-lang-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ angular.module('avUi')
$rootScope,
ConfigService,
$window,
I18nOverride,
Authmethod
) {
function link(scope, element, attrs)
{
scope.deflang = window.i18n.lng();
scope.deflang = $window.i18next.resolvedLanguage;
angular.element('#ng-app').attr('lang', scope.deflang);
scope.langs = $i18next.options.lngWhitelist;
scope.langs = $window.i18next.options.lngWhitelist;
var isAdmin = Authmethod.isAdmin();
function triggerDropdown()
{
Expand All @@ -61,27 +60,15 @@ angular.module('avUi')
// remembering it, and updating all the translations instantly.
//
// Triggered when the user clicks and selects a language.
scope.changeLang = function(lang)
$window.i18next.changeAppLang = scope.changeLang = function(lang)
{
$i18next.options.lng = lang;
if (!isAdmin) {
$i18next.options.useLocalStorage = true;
}
$window.i18next
.changeLanguage(lang)
.then(function () {
console.log("changeLang: broadcast i18nextLanguageChange");
$rootScope.$broadcast('i18nextLanguageChange', $window.i18next.resolvedLanguage);

// load i18n_overrides if any
if (angular.isDefined($window.i18nOverride))
{
$window.i18n.preload(
[lang],
function ()
{
I18nOverride(
/* overrides = */ $window.i18nOverride, // set to use the default, $window.i18nOverride
/* force = */ true
);
}
);
}
});

console.log("setting cookie");
var cookieConf = {
Expand Down
8 changes: 5 additions & 3 deletions avUi/custom-i18n-filter/custom-i18n-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ angular
'customI18n',
function()
{
return function(data, key)
function customI18nFilter(data, key)
{
var suffix = "_i18n";
var lang = window.i18n.lng();
var lang = window.i18next.resolvedLanguage;
var value = '';
if (_.isString(key) && _.isObject(data) && _.isString(lang)) {
value = data[key + suffix] && data[key + suffix][lang] || data[key] || value;
}
return value;
};
}
customI18nFilter.$stateful = true;
return customI18nFilter;
}
);

64 changes: 23 additions & 41 deletions avUi/i18n-override-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,65 +41,47 @@ angular
return function (overrides, force, languagesConf)
{
force = angular.isDefined(force) ? force : false;
overrides = overrides === null ? $window.i18nOverride : overrides;

// reset $window.i18nOverride
var performOverrides = false;
if (overrides) {
if (overrides !== null) {
performOverrides = (
force ||
JSON.stringify(overrides) !== JSON.stringify($window.i18nOverride)
);
$window.i18nOverride = overrides;
);
if (performOverrides) {
$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
// from `$window.i18next.resolvedLanguage`. 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();
languagesConf.default_language : $window.i18next.resolvedLanguage;

$i18next.options.lngWhitelist = languagesConf.available_languages;
$i18next.options.preload = languagesConf.available_languages;
$i18next.options.fallbackLng = [languagesConf.default_language, 'en'];
}

// load i18n_overrides if any
if (performOverrides)
{
_.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, {});
}
);
console.log("calling $window.i18next.reloadResources()..");
$window.i18next
.reloadResources($i18next.options.preload)
.then(function () {
if (
languagesConf &&
languagesConf.force_default_language &&
$window.i18next.changeAppLang
) {
console.log("reloadResources: successful. force-changing default lang to=" + languagesConf.default_language);
$window.i18next.changeAppLang(languagesConf.default_language);
} else {
console.log("reloadResources: successful. broadcast i18nextLanguageChange signal");
$rootScope.$broadcast('i18nextLanguageChange', $i18next.options.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
);
});
};
}
);
51 changes: 20 additions & 31 deletions dist/appCommon-vmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,34 +1123,33 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist
} ]), angular.module("avUi").service("I18nOverride", [ "$i18next", "$rootScope", "$window", function($i18next, $rootScope, $window) {
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), 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.$emit("i18nextLanguageChange", $i18next.options.lng);
null === overrides || !force && JSON.stringify(overrides) === JSON.stringify($window.i18nOverride) || ($window.i18nOverride = overrides),
languagesConf && ($i18next.options.lng = languagesConf.force_default_language ? languagesConf.default_language : $window.i18next.resolvedLanguage,
$i18next.options.lngWhitelist = languagesConf.available_languages, $i18next.options.preload = languagesConf.available_languages,
$i18next.options.fallbackLng = [ languagesConf.default_language, "en" ]), console.log("calling $window.i18next.reloadResources().."),
$window.i18next.reloadResources($i18next.options.preload).then(function() {
languagesConf && languagesConf.force_default_language && $window.i18next.changeAppLang ? (console.log("reloadResources: successful. force-changing default lang to=" + languagesConf.default_language),
$window.i18next.changeAppLang(languagesConf.default_language)) : (console.log("reloadResources: successful. broadcast i18nextLanguageChange signal"),
$rootScope.$broadcast("i18nextLanguageChange", $i18next.options.lng));
});
};
} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "$rootScope", "ConfigService", "$window", "I18nOverride", "Authmethod", function($i18next, ipCookie, angularLoad, amMoment, $rootScope, ConfigService, $window, I18nOverride, Authmethod) {
} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "$rootScope", "ConfigService", "$window", "Authmethod", function($i18next, ipCookie, angularLoad, amMoment, $rootScope, ConfigService, $window, Authmethod) {
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.deflang = $window.i18next.resolvedLanguage, angular.element("#ng-app").attr("lang", scope.deflang),
scope.langs = $window.i18next.options.lngWhitelist;
var isAdmin = Authmethod.isAdmin();
element.on("click", function() {
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() {
I18nOverride($window.i18nOverride, !0);
}), $window.i18next.changeAppLang = scope.changeLang = function(lang) {
$window.i18next.changeLanguage(lang).then(function() {
console.log("changeLang: broadcast i18nextLanguageChange"), $rootScope.$broadcast("i18nextLanguageChange", $window.i18next.resolvedLanguage);
}), console.log("setting cookie");
ipCookie("lang", lang, _.extend({
expires: 360,
Expand Down Expand Up @@ -1718,23 +1717,13 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist
templateUrl: "avUi/foot-directive/foot-directive.html"
};
} ]), angular.module("avUi").filter("customI18n", function() {
return function(data, key) {
var lang = window.i18n.lng(), value = "";
function customI18nFilter(data, key) {
var lang = window.i18next.resolvedLanguage, value = "";
return value = _.isString(key) && _.isObject(data) && _.isString(lang) ? data[key + "_i18n"] && data[key + "_i18n"][lang] || data[key] || value : value;
};
}
return customI18nFilter.$stateful = !0, customI18nFilter;
}), angular.module("common-ui", [ "ui.bootstrap", "ui.utils", "ui.router", "ngAnimate", "ngResource", "ngCookies", "ipCookie", "ngSanitize", "infinite-scroll", "angularMoment", "SequentConfig", "jm.i18next", "avRegistration", "avUi", "avTest", "angularFileUpload", "dndLists", "angularLoad", "ng-autofocus" ]),
angular.module("jm.i18next").config([ "$i18nextProvider", "ConfigServiceProvider", function($i18nextProvider, ConfigServiceProvider) {
$("#no-js").hide(), $i18nextProvider.options = _.extend({
useCookie: !0,
useLocalStorage: !1,
fallbackLng: "en",
cookieName: "lang",
detectLngQS: "lang",
lngWhitelist: [ "en", "es", "gl", "ca" ],
resGetPath: "/locales/__lng__.json",
defaultLoadingValue: ""
}, ConfigServiceProvider.i18nextInitOptions);
} ]), angular.module("common-ui").run([ "$http", "$rootScope", function($http, $rootScope) {
angular.module("common-ui").run([ "$http", "$rootScope", function($http, $rootScope) {
$rootScope.safeApply = function(fn) {
var phase = $rootScope.$$phase;
"$apply" === phase || "$digest" === phase ? fn && "function" == typeof fn && fn() : this.$apply(fn);
Expand Down
Loading

0 comments on commit 2c60743

Please sign in to comment.