2013-03-26 59 views
1

我有幾個資源文件,例如從語言特定的資源文件中獲取價值,而無需更改網站語言

爲Default.aspx.resx,default.aspx.nl.resx,default.aspx.en.resx

現在,當我在default.aspx.nl.resx加載的荷蘭域名。 但是現在我想訪問default.aspx.en.resx中的值並獲取屬於名稱「title」的英文值。

我現在就可以通過改變當前區域性服務器端實現這一點,訪問值,然後改回來,像這樣:

Dim culture As CultureInfo = New CultureInfo("en") 
Threading.Thread.CurrentThread.CurrentCulture = culture 
Threading.Thread.CurrentThread.CurrentUICulture = culture 
Dim title as String = GetLocalResourceObject("title") 
culture = New CultureInfo("nl") 
Threading.Thread.CurrentThread.CurrentCulture = culture 
Threading.Thread.CurrentThread.CurrentUICulture = culture 

但有一個更好/更快的方法?最好不必改變當前線程的文化,所以我可以定義我想訪問哪個資源文件以及哪種語言?

回答

2

您可以在您的參數文化圓盾加

GetLocalResourceObject("title","YourCulture"); 

鏈接:http://msdn.microsoft.com/fr-fr/library/ms149953.aspx

+0

謝謝你......你可能會提供有關如何使用它的樣本?我沒有在您提供的頁面上看到任何示例。再次感謝! – Flo 2013-03-26 17:36:13

+0

我嘗試了你的建議:GetLocalResourceObject(「title」,「en」),但這不起作用。我正在使用ASP.NET 4.5 btw – Flo 2013-03-26 17:52:59

+0

嘗試GetLocalResourceObject(「title」,「en-EN」) – 2013-03-27 09:47:50

2

編輯:(對不起,我不知道你想要的另一種方法與此不同,但是這是唯一的方法我設法做:)

我設法做要做到這一點:

var userLanguage = "en-US"; 

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage); 
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage); 

HttpContext.GetGlobalResourceObject("MyAppResource", "KeyThatIWantToGet"); 

其中MyAppResource是您的.resx文件的命名方式,而KeyThatIWantToGet則是自我解釋。

0

當不使用的HttpContext(普通.NET應用程序)我用下面的幫助:

/// <summary> 
/// Disposable class that lets us assume a specific culture while executing 
/// a certain block of code. You'd typically use it like this: 
/// 
/// using (new CultureContext("de")) 
/// { 
///  // Will return the German translation of "Please click here" 
///  string text = SharedResource.Please_click_here; 
/// } 
/// </summary> 
public class CultureContext : IDisposable 
{ 
    private readonly CultureInfo _previousCulture; 
    private readonly CultureInfo _previousUiCulture; 

    public CultureContext(CultureInfo culture) 
    { 
     // Save off the previous culture (we'll restore this on disposal) 
     _previousCulture = Thread.CurrentThread.CurrentCulture; 
     _previousUiCulture = Thread.CurrentThread.CurrentUICulture; 
     Thread.CurrentThread.CurrentCulture = culture; 
     Thread.CurrentThread.CurrentUICulture = culture; 
    } 

    public CultureContext(string cultureCode) 
     : this(new CultureInfo(cultureCode)) 
    { 

    } 

    /// <summary> 
    /// Syntactic sugar so that we can switch to a culture context as follows: 
    /// 
    /// using (CultureContext.For("de")) 
    /// { 
    ///  // Will return the German translation of "Please click here" 
    ///  string text = SharedResource.Please_click_here; 
    /// } 
    /// </summary> 
    public static CultureContext For(string cultureCode) 
    { 
     return new CultureContext(cultureCode); 
    } 

    public void Dispose() 
    { 
     // Restore the culture settings that were in place before switching 
     // to this context 
     Thread.CurrentThread.CurrentCulture = _previousCulture; 
     Thread.CurrentThread.CurrentUICulture = _previousUiCulture; 
    } 
}