开发者

Javascript localization in .net

I'm trying to determine the best way to implement localization into one of our web apps. This app is going to have a large number of javascript files, which will need to be localized as well.

Localizing the .net code is straight forward enough. We have a file called WebResources.resx which contains all strings in english (our fall back language). Then we just add additional files with alternative localized information (eg: WebResources.es-mx.resx). Then, bam! .Net pretty much takes care of the rest.

Pretty sweet. But when it comes to javascript, not so fast. Reading on MSDN they recommend:

You create a separate script file for each supported language and culture. In each script file, you include an object in JSON format that contains the localized resources values for that language and culture.

This seems like a maintenance nightmare that I'd like to avoid. Plus I'd like to avoid having to use the asp.net ScriptManager. So I got the bright idea of trying to use the resource files in my .js files. EG foobar.js:

function showGenericError(){
     alert('<% =Resources.WebResources.JsGenericError %>');
}

This unfortunately does not work as the .NET does not seem to do any processing on .js files. So the next idea I got was from the answer on this thread. It recommended having a javascript file which contained all your language strings. This feels like a waist of resources since at run time I only need one language, not all of them.

This leads me to the solution I'm planning on implementing. I plan to have a generic handler that writes out JSON that is localized for the language the current user is in need of. Here is a sample of what the .ashx page will look like:

public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";

        StringBuilder json = new StringBuilder();
        using (StringWriter jsonStringWriter = new StringWriter(json))
        {
            using (JsonTextWriter jsonWriter = new JsonTextWriter(jsonStringWriter))
            {
                jsonWriter.WriteStartObjec开发者_JS百科t();                

                jsonWriter.WritePropertyName("genericErrorMessage");
                jsonWriter.WriteValue(Resources.WebResources.GenericErrorMessage);

                jsonWriter.WriteEndObject();
            }
        }
        context.Response.Write("var webResources = " + json.ToString());
}

In the head of my pages I will have:

<script type="text/javascript" src="js/webResources.js.ashx"></script>

Then my js file will look like:

function showGenericError(){
     alert(webResources.genericErrorMessage);
}

Almost seems too easy, right? So my question is, does this make sense? Am I missing a "gotcha" somewhere? What are the downsides? Is there a better way to do this?


I posted a similar question a while ago, and this is what I came up with:

Localize javascript messages and validation text

The advantage here is that you can share resources used in regular .net pages.

Your approach looks fine as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜