2016-07-02 9 views
1

私は、.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ファイルは静的なので、あるユーザーが言語を変更すると、他のユーザーの後続の文字列も変更されます。 これを回避する1つの方法は、すべての文字列アクセスの前に明示的にカルチャを設定することですが、これは競合状態と醜いコードにつながります。

どのように私はそれぞれのユーザーがそれぞれの文字列アクセスの前に文化を明示的に設定することなく、好みの言語で回答を得ることができますか?そうする

答えて

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> 
関連する問題