-
Notifications
You must be signed in to change notification settings - Fork 164
Fix translation loading issue and ensure translations are applied before updating UI #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,50 @@ | ||
| function updateText() { | ||
| 'use strict'; | ||
| var i18n = $.i18n(), language, person, kittens, message, gender; | ||
| // Assuming i18n is some object and its methods need to be handled manually, | ||
| // if you are using a library for i18n, the load mechanism might be similar | ||
| function loadTranslations(callback) { | ||
| // Assuming i18n is an object with a load method | ||
| const i18n = { | ||
| locale: 'en', // Replace with dynamic locale if needed | ||
| load: function(url, locale) { | ||
| return fetch(url) | ||
| .then(response => response.json()) | ||
| .then(data => { | ||
| this.translations = data; | ||
| }); | ||
| }, | ||
| get: function(key) { | ||
| return this.translations[key] || key; // Return the translation or the key itself if not found | ||
| } | ||
| }; | ||
|
|
||
| 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(); | ||
| // Load the translation file | ||
| i18n.load('../assets/lang/ui_' + i18n.locale + '.json', i18n.locale) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we return the promise from |
||
| .then(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'; | ||
| const i18n = { translations: {} }; // Assuming i18n object | ||
| return i18n.get(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 ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this now? The translated string should update when the number of kittens, the person or the language is changed. |
||
| } ); | ||
| document.addEventListener('DOMContentLoaded', function () { | ||
| loadTranslations(function() { | ||
| // Now that translations are loaded, populate the formats object | ||
| const formats = { | ||
| portrait: _T('text-portrait'), | ||
| landscape: _T('text-landscape'), | ||
| square: _T('text-square') | ||
| }; | ||
|
|
||
| // Instead of $.each(), use Object.keys() and forEach() in vanilla JS | ||
| Object.keys(formats).forEach(function(key) { | ||
| console.log('appending ' + key); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stray |
||
| const option = document.createElement('option'); | ||
| option.value = key; | ||
| option.textContent = formats[key]; | ||
| document.getElementById('format').appendChild(option); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The locale should be determined based on the value selected in the dropdown