Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions demo/js/demo.js
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
Copy link
Member

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

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we return the promise from loadTranslations object and simple use .then in the caller to run code after the translations are loaded.

.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 );
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray console.log

const option = document.createElement('option');
option.value = key;
option.textContent = formats[key];
document.getElementById('format').appendChild(option);
});
});
});