Javascript support in ASP.NET pages

Germany

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.

Original code
alert("Hello World");
Modified code in VB.NET Project
alert(‹%=ml_j2_string(3,"Hello World")%›);
Modified code in C# Project
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.

Note: I don't know whether it is possible to add a using MultiLang; statement which would apply to this code and which would let me remove the namespace from the function call. If you know how to do this let me know.smile

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:

OnClientClick="return confirm('Are you sure you want to...')"

Clearly, I could replace this with:

OnClientClick="return confirm()"
(although I'm not sure whether that would work with the double quotes as shown), but I haven't implemented this yet.


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

Not to be a party killer - but seems like you got an error in your examples?

Germany

Hi Nick,

I think that I have fixed it.

The TikiWiki software seems to be inconsistent in how it handles greater than and less than characters, in the preview and in the posting. I think that the behaviour on wiki pages is also different to in the forum. However, since the current stable release is version 7.0 and I am using version 2.2, I shouldn't complain. These problems have probably been fixed long ago.

Phil