Javascript support in ASP.NET pages
Version 4.7x.0090 contains new support for localizing JavaScript, within ASP.NET pages.
Technically, this is much easier than I had expected. This is how it works.
alert("Hello World");
alert(‹%=ml_j2_string(3,"Hello World")%›);
alert(‹%=MultiLang.ml.ml_j2_string(3,"Hello World")%›);
The original string is replaced by a call to the function ml_j2_string() or ml_j1_string(). This function is called on the server side when the page is created. It is implemented in the module MlString.vb or MlString.cs and is not a JavaScript function.
Depending on which server side language you are using (VB.NET or C#), the code inserted is slightly different. For VB it is implemented in a module and does not use a namespace. For C# it is implemented in a class with static functions and does use a namespace.
This function returns a JavaScript encoded string enclosed in single or double quotes. The function ml_j1_string() encloses the string in single quotes. The function ml_j2_script() encloses the string in double quotes.
These functions are new. If you already have MlString.vb or .cs in your project, you will have to copy a newer version from:
‹Public Documents›\MultiLang\Templates\WebProjects\VB\MlString.vb
‹Public Documents›\MultiLang\Templates\WebProjects\CS\MlString.cs
Under Windows 7 ‹Public Documents› is C:\Users\Public\Documents.
Under XP ‹Public Documents› is something like C:\Documents and Settings\All Users\Documents, but the path itself may be localized.
I think it is fairly clear how this localization works. When the page is loaded, the localized string is inserted into the JavaScript code. There is no client side code to load localized strings. This makes the implementation technically simple, but means - of course - that it will only work in ASP.NET pages.
Limitations
At present, this support is limited to JavaScript within ‹script› tags.
There is another case, which Nick Niebling has mentioned in the thread LinkButton OnClientClick should be localized ? which is not yet supported.
This is the case where a LinkButton has an attribute like this:
Clearly, I could replace this with:
The difficulty is, how do I know which attributes can contain JavaScript code? I could hard code OnClientClick, which I think applies to Button and LinkButton, which would be a start, but is hardly a great solution. What about third party controls and what about non-ASP controls? I am still thinking about how to implement this.
There is one other limitation, which is basically a bug. I don't think the show/hide (sun/moon) function in the grid works. I will try to fix this soon.
Phil