Reputation:
I need to dynamically translate 2 strings to Spanish in the following JS, namely "Date" and "Read more" if the html-document's language code is set to Spanish (html lang="es"):
$.each(data,function(post, postInfo) {
jsonArray.push( postEntry + '<a href="' + postInfo.link + '" class="preview-title"><h2>' + postInfo.title + '</h2></a><div class="preview-meta">Date: ' + postInfo.date + '</div><p>' + postInfo.preview + '...</p><div class="read-more"><a href="' + postInfo.link + '" class="link-button">Read more</a></div>' + postFooter);
});
I am unsure how to approach this in the best way.
Getting the language code as a string would probably work with this:
var languageCode = $('html').attr('lang');
And then you could implement a simple check like:
if (languageCode === 'es') {
...
} else {
...
}
Would appreciate your advice how to approach this.
Upvotes: 0
Views: 5706
Reputation: 2089
if you only want to translate 2 kind of words - translation library might be overkill.
I would do something like
lang = {
es : {
readmore : 'Read More In Spanish',
date : 'Date in spanish'
},
en : {
readmore : 'Read More',
date : 'Date'
}
}
var languageCode = $('html').attr('lang');
console.log(lang[languageCode].readmore)
console.log(lang[languageCode].date)
Upvotes: 1