0

簡單。我想本地化我的應用程序。 我搜索了幾天,並嘗試了一百萬種不同的方法來獲取我的資源。 我成功的唯一方法是使用標準的asp.net文件夾「App_LocalResource」,公開資源文件並給它們一個Custom Tool Name。在視圖中,我可以導入Custom Tool Name@usingMVC4中的ASP.NET資源處理機制

我的問題是,當我改變文化時,語言/資源項目不會改變。

這是我如何改變它在Global.asax中:

protected void Application_AcquireRequestState(object sender, EventArgs e) 
    { 
     if (HttpContext.Current.Session != null) 
     { 
      CultureInfo ci = (CultureInfo)this.Session["Culture"]; 
      if (ci == null) 
      { 
       string langName = "en"; 
       string autoLang = ""; 
       if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0) 
       { 
        autoLang = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2); 
       } 
       if (autoLang == "da") 
        langName = autoLang; 
       ci = new CultureInfo(langName); 
       this.Session["Culture"] = ci; 
      } 

      Thread.CurrentThread.CurrentUICulture = ci; 
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); 
     } 
    } 

因此,文化或者是daen。但我注意到資源文件的名稱必須具有特定的語法。必須有一個默認(在這種情況下是英語)沒有國家/文化代碼和其他默認已被命名爲reFile.da-DK.resx。它必須有語言和文化代碼。

我擔心資源處理程序可以識別我的文件,因爲文化設置爲「da」而不是「da-DK」。如果我將我的da資源文件命名爲resFile.da.resx,則不能導入我的資源文件Custom Tool Name

我該怎麼做才能解決這個問題?

+0

爲什麼在這裏每個案例都沒有提供完整的文化名稱? – 2013-03-02 21:33:38

+0

@ AdamTuliper-MSFT CurrentUICulture只接受兩個字母的字符串。 – 2013-03-04 11:53:29

+1

CurrentUICulture接受一個CultureInfo,它是一個全名(不是兩個字符),比如新的CultureInfo(「en-US」),我也會在BeginRequest中設置它,它在頁面生命週期的早期出現,通常是這個代碼的地方發現自己。 – 2013-03-04 17:44:23

回答

0

使用完整的文化信息字符串,例如:

var info = new CultureInfo("en-US")

還爲最佳實踐出移動代碼到的Application_BeginRequest方法,那就是你會看到這種類型的代碼的標準位置。