jquery - How to translate JavaScript strings dynamically? -
i need dynamically translate 2 strings spanish in following js, namely "date" , "read more" if html-document's language code set 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 unsure how approach in best way.
getting language code string work this:
var languagecode = $('html').attr('lang');
and implement simple check like:
if (languagecode === 'es') { ... } else { ... }
would appreciate advice how approach this.
if want translate 2 kind of words - translation library might overkill.
i 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)
Comments
Post a Comment