Question

I'm building in ASP.NET MVC 4 a site. While sometimes I return a full View and sometimes I call Ajax from a View, get some data and build a new page with JavaScript.

for example :

 myDiv.append("<div class=\"test\">some text </div>");

I want to support in multi language, if it was only on client side without ASP.NET views I would be using JavaScript to load files by the language

 english.js
 -------------------------
 var home = 'home';


 russian.js
 ------------------------
 var home = 'дом';

How can I support multi language so it will work with JavaScript on client side and while rendering a View in ASP.NET page?

Was it helpful?

Solution

Why can't you render localized strings right in a view? smth like:

<script>
    var home = '@SomeResource.SomeStringValue'
</script>

or even include js file depending on client language?

E.g. You have some resource file named LocalizedStrings.resx with default language. For different languages you have files named LocalizedStrings.en-US.resx, LocalizedStrings.kk-KZ.resx etc...

In a view you do following:

@Html.RenderPartial('MyLocalizedView')

and MyLocalizedView itself looks like:

<script>
    var home = '@LocalizedStrings.Home';
    var street = '@LocalizedStrings.Street';
    var wtf = '@LocalizedStrings.Wtf';
    ...
</script>

So when the main view is rendered you already have all js variables initialized with proper localized values. Of course you need to make sure your current culture is set to proper value, like:

var culture = CultureInfo.GetCultureInfo("ru-RU");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

I don't know how you control it, I'd do it in global.asax depending on domain extension

OTHER TIPS

I'm using a custom http handler for this which dynamically generates all my Resources as an js object.

The resulting object for the resource named as JS_PageTitle may look like:

var resources = { PageTitle: "page title en" }.

The http handler implementation:

            public class JavascriptResourceHandler : IHttpHandler
            {
                #region IHttpHandler Members

                public bool IsReusable
                {
                    // Return false in case your Managed Handler cannot be reused for another request.
                    // Usually this would be false in case you have some state information preserved per request.
                    get { return true; }
                }

                public void ProcessRequest(HttpContext context)
                {
                    var sb = new StringBuilder();            
                    sb.Append(" var resources = { ");
                    var properties = typeof (WebRes).GetProperties(BindingFlags.Static | BindingFlags.Public).Where(p => p.Name.StartsWith("JS_")).ToList();

                    for (int i=0; i<properties.Count; i++)
                    {
                        var property = properties[i];

                        if (property.PropertyType == typeof(string))
                        {
                            sb.Append(property.Name.Replace("JS_", string.Empty));
                            sb.Append(":");
                            sb.Append("'");
                            sb.Append(HttpUtility.JavaScriptStringEncode((string)property.GetValue(null, null)));
                            sb.Append("'");
                            if (i != properties.Count - 1)
                                sb.Append(",");
                        }
                    }

                    sb.Append("};");

                    context.Response.Clear();
                    context.Response.ContentType = "text/javascript";
                    context.Response.Write(sb.ToString());
                }

                #endregion
            }

The configuration entry in the web.config

  <add name="JavascriptResourceHandler" verb="GET" path="JavascriptResourceHandler.axd" type="Web.Infrastructure.HttpHandlers.JavascriptResourceHandler, Web, Version=1.0.*, Culture=neutral" />

Then reference it from the _Layout.cshtml:

<script src="~/JavascriptResourceHandler.axd"></script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top