【问题标题】:Access user-specific localized resources访问用户特定的本地化资源
【发布时间】:2016-07-02 13:06:25
【问题描述】:

我正在构建一个 .NET Web 应用程序,其本地化字符串由多语言应用程序工具包提供。它生成一个静态类,其属性的名称与资源文件中定义的相同。它看起来像这样:

/// <summary>
///   A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Strings {

    private static global::System.Resources.ResourceManager resourceMan;

    private static global::System.Globalization.CultureInfo resourceCulture;

    [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    internal Strings() {
    }

    /// <summary>
    ///   Returns the cached ResourceManager instance used by this class.
    /// </summary>
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    internal static global::System.Resources.ResourceManager ResourceManager {
        get {
            if (object.ReferenceEquals(resourceMan, null)) {
                global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("FaqBot.Resources.Strings", typeof(Strings).Assembly);
                resourceMan = temp;
            }
            return resourceMan;
        }
    }

    /// <summary>
    ///   Overrides the current thread's CurrentUICulture property for all
    ///   resource lookups using this strongly typed resource class.
    /// </summary>
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    internal static global::System.Globalization.CultureInfo Culture {
        get {
            return resourceCulture;
        }
        set {
            resourceCulture = value;
        }
    }

    /// <summary>
    ///   Looks up a localized string similar to Good Morning.
    /// </summary>
    internal static string Greeting {
        get {
            return ResourceManager.GetString("Greeting", resourceCulture);
        }
    }

    /// <summary>
    ///   Looks up a localized string similar to Welcome.
    /// </summary>
    internal static string Welcome {
        get {
            return ResourceManager.GetString("Welcome", resourceCulture);
        }
    }
}

在我访问字符串的代码中,我可以设置文化,随后对字符串属性的访问会返回正确本地化的字符串。

现在,我的 Web 应用程序可以让用户选择他们的语言,并且我有一个机制来存储这个偏好。但是,由于字符串文件是静态的,如果一个用户更改了语言,其他用户的后续字符串也会随之更改。 绕过这一点的一种方法是在每次字符串访问之前明确设置文化,但这会导致竞争条件和丑陋的代码。

如何让每个用户以他们的首选语言获得响应,而不在每个字符串访问之前明确设置文化?

【问题讨论】:

    标签: c# asp.net web-applications localization


    【解决方案1】:

    这样做的一种方法是创建一个新的HttpModule:

    public class LocalizationModule : IHttpModule
    {
        public void Dispose()
        {
        }
    
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            // check if user is authenticated
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                var username = HttpContext.User.Identity.Name;
                /* 
                   Your code to read user's culture name from the profile and
                   put it in "lang" variable
                */
                var culture = new System.Globalization.CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = culture;
                Thread.CurrentThread.CurrentUICulture = culture;
            }
        }
    }
    

    并通过在web.config 文件中注册它来运行它:

    <configuration>
      <system.web>
        <httpModules>
          <add name="LocalizationModule " type="LocalizationModule"/> <!-- put the full namespace and class name in type attribute eg. MyApp.MyNamespace.LocalizationModule -->
         </httpModules>
      </system.web>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多