Localize Strings in Javascript

前端 未结 12 1584
长发绾君心
长发绾君心 2020-11-28 05:52

I\'m currently using .resx files to manage my server side resources for .NET.

the application that I am dealing with also allows developers to plugin Ja

12条回答
  •  清歌不尽
    2020-11-28 06:06

    After Googling a lot and not satisfied with the majority of solutions presented, I have just found an amazing/generic solution that uses T4 templates. The complete post by Jochen van Wylick you can read here:

    Using T4 for localizing JavaScript resources based on .resx files

    Main advantages are:

    1. Having only 1 place where resources are managed ( namely the .resx files )
    2. Support for multiple cultures
    3. Leverage IntelliSense - allow for code completion

    Disadvantages:

    The shortcomings of this solution are of course that the size of the .js file might become quite large. However, since it's cached by the browser, we don't consider this a problem for our application. However - this caching can also result in the browser not finding the resource called from code.


    How this works?

    Basically he defined a T4 template that points to your .resx files. With some C# code he traverses each and every resource string and add it to JavaScript pure key value properties that then are output in a single JavaScript file called Resources.js (you can tweak the names if you wish).


    T4 template [ change accordingly to point to your .resx files location ]

    <#@ template language="C#" debug="false" hostspecific="true"#>
    <#@ assembly name="System.Windows.Forms" #>
    <#@ import namespace="System.Resources" #>
    <#@ import namespace="System.Collections" #>
    <#@ import namespace="System.IO" #>
    <#@ output extension=".js"#>
    <#
     var path = Path.GetDirectoryName(Host.TemplateFile) + "/../App_GlobalResources/";
     var resourceNames = new string[1]
     {
      "Common"
     };
    
    #>
    /**
    * Resources
    * ---------
    * This file is auto-generated by a tool
    * 2012 Jochen van Wylick
    **/
    var Resources = {
     <# foreach (var name in resourceNames) { #>
     <#=name #>: {},
     <# } #>
    };
    <# foreach (var name in resourceNames) {
     var nlFile = Host.ResolvePath(path + name + ".nl.resx" );
     var enFile = Host.ResolvePath(path + name + ".resx" );
     ResXResourceSet nlResxSet = new ResXResourceSet(nlFile);
     ResXResourceSet enResxSet = new ResXResourceSet(enFile);
    #>
    
    <# foreach (DictionaryEntry item in nlResxSet) { #>
    Resources.<#=name#>.<#=item.Key.ToString()#> = {
     'nl-NL': '<#= ("" + item.Value).Replace("\r\n", string.Empty).Replace("'","\\'")#>',
     'en-GB': '<#= ("" + enResxSet.GetString(item.Key.ToString())).Replace("\r\n", string.Empty).Replace("'","\\'")#>'
     };
    <# } #>
    <# } #>
    

    In the Form/View side

    To have the correct translation picked up, add this in your master if you're using WebForms:

    
    
    
    

    If you're using ASP.NET MVC (like me), you can do this:

    
    
    
    

    The MetaAcceptLanguage helper I got from this awesome post by Scott Hanselman:

    Globalization, Internationalization and Localization in ASP.NET MVC 3, JavaScript and jQuery - Part 1

    public static IHtmlString MetaAcceptLanguage(this HtmlHelper html)
    {
         var acceptLanguage =
             HttpUtility.HtmlAttributeEncode(
                         Thread.CurrentThread.CurrentUICulture.ToString());
    
          return new HtmlString(
          String.Format("", "accept-language",
                        acceptLanguage));
     }
    

    Use it

    var msg = Resources.Common.Greeting[locale];
    alert(msg);
    

提交回复
热议问题