2016-03-03 62 views
0

我有一個很奇怪的問題。我正在對我的項目實施本地化,但是當我嘗試獲取當前的語言環境Windows正在運行時,它會忽略國家/地區信息。這是一個示例代碼:Thread.CurrentThread.CurrentUICulture不能正確返回國家

using System; 
using System.Globalization; 

public class Example 
{ 
    public static void Main() 
    { 
     CultureInfo culture = CultureInfo.CurrentUICulture; 
     Console.WriteLine("The current UI culture is {0} [{1}]", 
         culture.NativeName, culture.Name); 
    } 
} 

當我用最常見的語言(En-US,FR-fr)運行它時,它會正確返回。但是,例如,當我從比利時選擇法語時,即使我從語言偏好選項中刪除了法國的法語,它也會檢索FR-FR而不是FR-be。

我不知道我怎麼能得到我所選的國家,無論我的軟件位於哪個國家。

ps:使用CurrentCulture並不是我正在尋找的答案,因爲我想與我在UI中使用的顯示語言匹配,而不是日期/時間/數字格式(它們可以完全不同)。

+0

我剛剛嘗試卸載法文語言包(FR-fr),然後從法文比利時安裝。我仍然得到FR-fr。這是Windows的問題嗎? –

+0

水晶球表示您選擇了另一個鍵盤佈局,而不是更改操作系統語言。您必須使用「高級」按鈕。非常不清楚你實際做了什麼,可能最好先在superuser.com上ping這個。一定要提及Windows版本和版本,他們在每個版本中都對此進行了更改,並且您安裝的語言包有很多。 –

+0

我在Windows 8和10中測試過。它們都不起作用。我改變了OS語言安裝的語言包和類似的東西(你只能在高級會話中這樣做),就像我在前面的評論中提到的一樣。對於CultureInfo類,需要System.Globalization爲 –

回答

0

我覺得比你有錯在標題中使用。

MS使用system.thread而不是system.globalization https://msdn.microsoft.com/it-it/library/system.globalization.cultureinfo.currentuiculture(v=vs.110).aspx 其中有些編譯錯誤。

正確的和編譯的代碼是這樣的:

(請注意,當CultureInfo.CurrentCulture是隻讀的,而不是我用System.Threading.Thread.CurrentThread.CurrentCulture有二傳手訪問)

public static void Main(string[] args) 
{ 
    // Display the name of the current thread culture. 
    Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name); 

    // Change the current culture to th-TH. 
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH", false); 
    Console.WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name); 

    // Display the name of the current UI culture. 
    Console.WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name); 

    // Change the current UI culture to ja-JP. 
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP", false); 
    Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name); 

} 
+0

。並且您發佈的鏈接上的所有示例都使用System.Globalization。 –

+0

對不起,我在手機上閱讀過,從未嘗試過。我發現了一些例子的錯誤。我已經添加了正確的信息 –

+0

對不起,我不想以編程方式更改當前的文化,也不想改變當前的UI文化。我只想從操作系統中獲得正確的語言/國家信息 - 如果是比利時法語,則不正確。您發佈的代碼不會從我發佈的代碼中添加任何新內容,但不會更改文化請求。 –