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

jayasurya_j

我正在构建一个.NET Web App,其中由Multilingual App Toolkit给出了本地化的字符串。它会生成一个静态类,其属性具有在资源文件中定义的名称。看起来像这样:

/// <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文件是静态的,因此,如果一个用户更改语言,则其他用户的后续字符串也将更改。绕过此方法的一种方法是在每次访问字符串之前显式设置区域性,但这会导致争用条件和丑陋的代码。

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

akazemis

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档