访问特定于用户的本地化资源

本文关键字:本地化 资源 用户 于用户 访问 | 更新日期: 2023-09-27 17:53:57

我正在构建一个。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应用程序有选项让用户选择他们的语言,我有一个机制来存储这个首选项。但是,由于Strings文件是静态的,如果一个用户更改了语言,那么其他用户的后续字符串也会被更改。绕过这个问题的一种方法是在每个字符串访问之前显式地设置区域性,但这会导致竞争条件和丑陋的代码。

如何让每个用户在他们首选的语言中获得响应,而无需在每个字符串访问之前显式设置区域性?

访问特定于用户的本地化资源

一种方法是创建一个新的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>