Wednesday, February 27, 2008

I ran across this style of page localization once before and liked it.

This is the basis for a nice localization pattern for web pages using JavaScript.

The JS code dropped into the script file looks like so..

var FB_Localized = {

feedbackhdr:'Feedback For This Page',

feedbacksaveerror:'Sorry the system is busy, try again later.',

feedbacksent:'Feedback Sent',

feedbackemail:'Your email address:',

feedbackmessage:'Message:',

feedbacksubmit:'Submit',

feedbackclose:'Close',

feedbacklengthTooLong:'(Feedback too long please shorten)',

feedbacktotallen:1900

};

var FB_T = function(t){return FB_Localized[t]||t;}

To pull out any value in your JavaScript code you just put in a placeholder and then use perhaps a simple jQuery call to place it onload.

<script>
$(function(){
$("#feedbackheader").html(FB_T('feedbackhdr'));
});
</script>

<span id=feedbackheader></span>

So to override the language defaults you just reference another script file with language overrides for the above array like so before the final page is loaded..

var FB_Localized = {

feedbackhdr:'Отзыв на эту страницу',

feedbacksaveerror:'Извините, система загружена. Попробуйте позднее.',

feedbacksent:'Отзыв отправлен',

feedbackemail:'Ваш адрес электронной почты:',

feedbackmessage:'Сообщение:',

feedbacksubmit:'Отправить',

feedbackclose:'Закрыть',

feedbacklengthTooLong:'(Отзыв слишком длинный. Пожалуйста, сократите его)',

feedbacktotallen:1900

};


etc for any other language that you want to support..

The nice thing here is that you can drive the JS Script file from easily from a database to .Net Resource files with ease.. Some time back I did a quick video detailing how easy this would be to do in .Net using Resource Files and/ro I think a database driver. The video was titled Zero Code ASP.Net Localization via jQuery & JSON but I am sure it is a bit out of date.

Kevin Pirkl








No comments:

Blog Archive