2016-07-02 3 views
1

저는 Multilingual App Toolkit에서 지역화 된 문자열을 사용하여 .NET Web App을 제작하고 있습니다. 리소스 파일에 정의 된 이름을 가진 속성을 사용하여 정적 클래스를 생성합니다.사용자 별 지역화 된 리소스에 액세스

내가 문자열에 액세스 코드에서
/// <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); 
     } 
    } 
} 

, 나는 문화를 설정할 수 있으며, 문자열 속성이 제대로 지역화 된 문자열을 반환에 후속 액세스 : 그것은 다음과 같습니다.

내 웹 앱에는 사용자가 언어를 선택할 수있는 옵션이 있으며이 기본 설정을 저장하는 메커니즘이 있습니다. 그러나 Strings 파일은 정적이므로 한 사용자가 언어를 변경하면 다른 사용자의 후속 문자열도 변경됩니다. 이것을 무시하는 한 가지 방법은 모든 문자열 액세스 전에 문화권을 명시 적으로 설정하는 것이지만 경쟁 조건과 추한 코드로 이어질 수 있습니다.

각 사용자가 각 문자열 액세스 전에 명시 적으로 문화권을 설정하지 않고 원하는 언어로 응답을 받도록하려면 어떻게해야합니까? 이렇게하려면

답변

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> 
관련 문제