Saturday, September 8, 2007

JSON AJAX Localization

It may be annoying when you need to have several languages in your web sites..

There is several way... to implement localization your web content in the client..

you either create a control and attached those assembly to your web site...
or just use... JSON object...

My preference is using JSON ( Javascipt Object Notation)....

* Create.. JSON Resource JS file for each.. languange...
Resource.js
MyReourceText={
"HeaderTitle":"Hello World",
"FooterText":"CopyRight By Kurniawan"
}

Resource.fr.js
MyReourceText={
"HeaderTitle":"Header Title in France",
"FooterText":"CopyRight By Kurniawan in france"
}


* Create.. UpdateResource js - this script will update your content use specific localize language.
Note:
-saperate this file from Resource.js...so you will not duplicate your code


UpdateResource.js

Sys.Application.add_load(SetupForm);
function SetupForm()
{
$get('HeaderTitle').innerText = MyReourceText.HeaderTitle;
$get('FooterText').innerText = MyReourceText.FooterText;
}



*
In your aspx... add enable EnableScriptLocalization="true" in your ScriptManager
This will enable the


*
Add those script reference into your script Manager
<asp:ScriptReference Path="Resource.js" ResourceUICulture="fr" />
<asp:ScriptReference Path="UpdateResource.js" />


**** THAT'S ALL ****

You may change your language from the browser or
from the your own control.... by using this...
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.CreateSpecificCulture(cmbLang.SelectedValue);

and you can get the current language from your browser when not postback
cmbLang.Items.FindByValue(System.Threading.Thread.CurrentThread.
CurrentUICulture.TwoLetterISOLanguageName).Selected = true;

****

You may enable globalization.. to formating your date / currency into specific languange../ culture...

by activate EnableScriptGlobalization = "true" in Script Manager...
You can use
d.localeFormat("dddd, dd MMM yyyy");

****

No comments: