From 0e6b3378087d28a6bf070e4c7a076461823b2e7e Mon Sep 17 00:00:00 2001 From: Dhanush Date: Sat, 21 Dec 2024 01:57:27 +0530 Subject: [PATCH 1/2] Fix translation loading issue and ensure translations are applied before updating UI --- demo/js/demo.js | 53 ++++++++++++++++++++++++++--------------------- package-lock.json | 2 +- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/demo/js/demo.js b/demo/js/demo.js index a35d1de..064485a 100644 --- a/demo/js/demo.js +++ b/demo/js/demo.js @@ -1,27 +1,32 @@ -function updateText() { - 'use strict'; - var i18n = $.i18n(), language, person, kittens, message, gender; - - message = '$1 has $2 {{plural:$2|kitten|kittens}}. ' + - '{{gender:$3|He|She}} loves to play with {{plural:$2|it|them}}.'; - language = $( '.language option:selected' ).val(); - person = $( '.person option:selected' ).text(); - gender = $( '.person option:selected' ).val(); - kittens = $( '.kittens' ).val(); +function loadTranslations(callback) { + var i18n = $.i18n(); + // Load the translation file + i18n.load('../assets/lang/ui_' + i18n.locale + '.json', i18n.locale).done(function() { + callback(); // Once translations are loaded, call the callback function + }); +} - i18n.locale = language; - i18n.load( 'i18n/demo-' + i18n.locale + '.json', i18n.locale ).done( - function () { - var personName = $.i18n( person ), localizedMessage = $.i18n( message, personName, - kittens, gender ); - $( '.result' ).text( localizedMessage ).prop( 'title', i18n.localize( message ) ); - } ); +function _T(key) { + 'use strict'; + var i18n = $.i18n(); + return i18n(key); // Directly return the translation after it's loaded } -// Enable debug -$.i18n.debug = true; -$( document ).ready( function ( $ ) { - 'use strict'; - updateText(); - $( '.kittens, .person, .language' ).on( 'change keyup', updateText ); -} ); +$(document).ready(function () { + loadTranslations(function() { + // Now that translations are loaded, populate the formats object + var formats = { + portrait: _T('text-portrait'), + landscape: _T('text-landscape'), + square: _T('text-square') + }; + + $.each(formats, function (key, format) { + console.log('appending ' + key); + $('#format').append($('