Skip to content

Commit

Permalink
🐞 default translation appears when switching language for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
edulix committed Jan 25, 2024
1 parent 8a7dbfc commit f6b0762
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
30 changes: 29 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,35 @@ angular
cookieName: 'lang',
detectLngQS: 'lang',
lngWhitelist: ['en', 'es', 'gl', 'ca'],
resGetPath: '/booth/locales/__lng__.json',
ns: {namespaces: ['override', 'locales'], defaultNs: 'override'},
fallbackNS: 'locales',
resGetPath: '/booth/__ns__/__lng__.json',
customLoad: function (lngValue, nsValue, options, loadComplete) {
if (nsValue === 'locales') {
var url = '/booth/' + nsValue + '/' + lngValue + '.json';
var req = new XMLHttpRequest();
// Configure it: GET-request for the URL /your/api/endpoint
req.open('GET', url, true);
req.onload = function() {
if (req.status >= 200 && req.status < 300) {
var data = JSON.parse(req.responseText);
var error = null;
loadComplete(/*err*/ null, data);
} else {
loadComplete(/*err*/ "Error loading locale at url=" + url, null);
}
};
req.send("");
} else if (nsValue === 'override') {
if (window.i18nOverride) {
loadComplete(/*err*/ null, window.i18nOverride[lngValue]);
} else {
loadComplete(/*err*/ "Error loading overrides, not found", null);
}
} else {
console.log("unhandled customLoad case for i18next");
}
},
defaultLoadingValue: '' // ng-i18next option, *NOT* directly supported by i18next
},
ConfigServiceProvider.i18nextInitOptions
Expand Down
80 changes: 51 additions & 29 deletions avBooth/booth-directive/booth-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,58 @@ angular.module('avBooth')

// This is used to enable custom css overriding
scope.allowCustomElectionThemeCss = ConfigService.allowCustomElectionThemeCss;
scope.alreadyReloaded = null;

function reloadTranslations(force, ms) {
setTimeout(
function () {
var election = (
scope.state === stateEnum.electionChooserScreen
) ? scope.parentElection : scope.election;

// reset $window.i18nOverride
var overrides = (
election &&
election.presentation &&
election.presentation.i18n_override
) ? election.presentation.i18n_override : null;

var languagesConf = (
election &&
election.presentation &&
election.presentation.i18n_languages_conf
) ? election.presentation.i18n_languages_conf : null;

$i18next.options.useLocalStorage = true;
I18nOverride(
/* overrides = */ overrides,
/* force = */ force,
/* languagesConf = */ languagesConf
);
},
ms || 0
);
function reloadTranslations(force) {
function reloadInner() {
var election = (
scope.state === stateEnum.electionChooserScreen
) ? scope.parentElection : scope.election;

if (scope.alreadyReloaded === election.id) {
$i18next.reInit();
return;
} else {
scope.alreadyReloaded = election.id;
}

// reset $window.i18nOverride
var overrides = (
election &&
election.presentation &&
election.presentation.i18n_override
) ? election.presentation.i18n_override : null;

var languagesConf = (
election &&
election.presentation &&
election.presentation.i18n_languages_conf
) ? election.presentation.i18n_languages_conf : null;

$i18next.options.useLocalStorage = true;
I18nOverride(
/* overrides = */ overrides,
/* force = */ force,
/* languagesConf = */ languagesConf
);
}
function timeoutWrap() {
console.log("timeoutWrap");
var election = (
scope.state === stateEnum.electionChooserScreen
) ? scope.parentElection : scope.election;

if (election && scope.alreadyReloaded === election.id) {
return;
}
if (!election) {
console.log("timeoutWrap: delaying..");
setTimeout(timeoutWrap, 200);
} else {
reloadInner();
}
}
timeoutWrap();
}

function updateWidth() {
Expand Down

0 comments on commit f6b0762

Please sign in to comment.