I have window-7 Ultimate OS.I written below code for get current culture info.
void TestMessage()
{
CultureInfo culture = CultureInfo.CurrentCulture;
CultureInfo culture1 = Thread.CurrentThread.CurrentCulture;
}
it is working fine with window-8,server 2012 Japanese OS. But it is not working in window-7 only. Please look below image of my computer region setting
Can any anyone guide me to get correct culture name?
Thanks,
There's CurrentCulture and CurrentUICulture.
CurrentCulture: formatting of data (numbers, dates), it is configured in Windows using the tab visible in your screenshot
CurrentUICulture: the language to speak/write to your user, it is configured in Windows using one of the other tabs showing in your screenshot. ("Keyboard & Languages" I think)
Your screenshot:
... is showing american numeric notation because CurrentCulture is set to en-US
... is talking Japanese because CurrentUICulture is very likely set to Japanese
So, if you need to know what language to use for localization, as is probably your case, you should be using CurrentUICulture.
Related
I have added one localisation for my resource file. ResGeneral.resx and ResGeneral.cs.resx. My computer has czech environment so even Invariant culture uses the .cs.resx file. The projects default culture is set to Invariant.
Then I added a combo box with options loaded from the SupportedLanguages enum, which changes the selected lang index in config, so next time program is launched, the culture is changed to the selected index.
public enum SupportedLanguages
{
Invariant = 0,
English,
Czech
}
public static void ChangeLang(int lang)
{
CultureInfo cultureInfo = CultureInfo.InvariantCulture;
switch (lang)
{
case 1:
cultureInfo = new CultureInfo("en");
break;
case 2:
cultureInfo = new CultureInfo("cs");
break;
}
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = cultureInfo;
}
How should I approach this?
Can I specify that the clean .resx (en) is to be used with invariant always?
Is there reason to even use Invariant or can I set the default to "en"? Project owner fears that some machines might not have "en" culture installed, but I haven't found any info about that. Is that possible?
Is this even a problem? So what it auto uses Czech, if a German user launches it, I dont have a .de file, it uses the clean .resx and he sees english
What do you think? Thanks for answers.
I'm working on a localized Windows (Phone) 10 UWP app.
I have implemented support for 2 language: en-US and nl-NL (Dutch-Netherlands). This works fine: When the user has selected English in the phone settings the app starts in English and when the user has selected Dutch in the phone settings the app starts in Dutch.
To get this working I had to make some changes in the package.appxmanifest because my language resources are in a different DLL:
<Resources>
<Resource Language="nl-NL"/>
<Resource Language="en-US"/>
</Resources>
But I cannot find any way to get the regional format from the user for date, time and number formatting.
When the user has selected English as language but Dutch (Netherlands) for regional format, my app starts with both
System.Globalization.CultureInfo.CurrentUICulture and System.Globalization.CultureInfo.CurrentCulture set to "en-US", where System.Globalization.CultureInfo.CurrentCulture should be "nl-NL".
I have been searching all of the documentation but I cannot find a way to retrieve the phone regional format (except Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion which is something different).
Is there a way to retrieve the phone regional format?
I only know the following hack mentioned here
var systemLanguage = GlobalizationPreferences.Languages.First();
var regionInfo = new RegionInfo(systemLanguage);
var dtf = new DateTimeFormatter("longdate", new[] { regionInfo.TwoLetterISORegionName });
var regionInfoName = dtf.ResolvedLanguage;
var regionFormatCultureInfo = new CultureInfo(regionInfoName);
Peter Meinl's answer works, but is a little confusing because you do not need the RegionInfo.
Pedro Lamas describes the hack using the ResolvedLanguage of DateTimeFormatter just using "US".
DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "US" });
string regionInfoName = dtf.ResolvedLanguage;
CultureInfo regionFormatCultureInfo = new CultureInfo(regionInfoName);
The ResolvedLanguage property of the DateTimeFormatter will contain the regional format id of the phone, in this case "nl-NL".
Mind that you DO need a language in the constructor of the DateTimeFormatter, just new DateTimeFormatter("longdate") or DateTimeFormatter.LongDate won't work.
I'm working on WPF application and I have localized resources (en,fr,zh) in .resx files. Following test code is used to display the localized string. It works fine for english, french but fails in Chinese. In chinese it shows english text only. I tried using all variants of Chinese culture such as zh-CN, zh-Hans, zh-Hant and the "old" zh-CHS, zh-CHT but no luck.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// CultureInfo culture = CultureInfo.CreateSpecificCulture("zh-CHT");
CultureInfo culture = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
MessageBox.Show(Properties.Resources.address);
}
}
Resource fiels are named as Resources.resx, Resources.fr.resx, Resources.zh.resx
Any help would be appreciated,
Rajesh
It's because zh-CHS is Neutral Culture.
In ASP.NET 2.0 it\s not possible to set CurrentCulture to Neutral Culture like zh-CHS.
But you can set it to a Specific Culture like zh-CN.
If you need to use a Neutral Culture, like zh-CHS, you can set CurrentUICulture property instead. CurrentUICulture is the one that is responsible for getting text from Resource files.
CurrentCulture is responsible for Number, DateTime and Currency formatting.
BTW, in ASP.NET 4.0 you can set CurrentCulture to Neutral Culture, but not in v2.0.
In my code, where the site could run on either .NET 2 or .NET 4 I do it like this:
public static void SetCurrentCulture(string cultureName)
{
try
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
}
catch (Exception)
{
// Ignore if exception happens: In ASP.NET 2.0 setting CurrentCulture = Neutral culture (like zh-CHS) throws an exception:
// Culture 'zh-CHS' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
HttpCookie cultureCookie = new HttpCookie("CultureCookie", cultureName);
HttpContext.Current.Response.Cookies.Set(cultureCookie);
}
I had a similar problem. The current culture was set at some point on start to zh-CN but was overwritten by English. Solution in my case- to install the Chinese language from Windows settings.
I am currently working on a windows phone application that takes some information from the user and returns some other information based on the user input.
The application works great if the specific device has its region settings set to US. If the region settings of the device are set to Greek or German, some problems occur. For example, the US decimal point character "." is considered as "," and vice versa. As a result, all the calculations are false.
What I want to do is internationalize the application so that it works exactly the same no matter what the regional settings are. Is this possible?
If you want your app allways to show number and dates in one specify format you can force the app to allways run in one specify culture like this.
You just have to set the current thread of your app to one specify culture (add to the App.cs file)!
public App()
{
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Set the current thread to US!
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I want to localize my Windows Store app. I have string\cs-CZ\Resources.resw and I load the strings in C# using ResourceLoader. When I have my system set to cs-CZ locale, the string are returned ok. When I set to to another locale, the GetString method returns an empty string. I have my projects neutral solution set to cs-CZ so what is the problem? How do I make the app always take resources from string\cs-CZ\Resources.resw?
The default locale can be programatically set using
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "cs-CZ";
in OnLaunched