CultureInfo InvariantCulture necessary? - c#

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.

Related

Why C# CultureInfo.CurrentCulture give en_US in Window-7 Japanese culture?

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.

Get user culture, not thread culture

When my application starts, it sets its own culture to CultureInfo.InvariantCulture but in some places, I want to use localized number formatting according to what the user has set in Windows. How can I do this?
System.Globalization.CultureInfo.CurrentCulture only returns the thread's culture which is no longer the user's default.
I'm hoping for a more elegant way than storing the thread's default culture before changing it or creating a new thread just to read the culture out of it.
Maybe there's a built-in .Net wrapper for the Windows function GetUserDefaultLocaleName?
System.Globalization.CultureInfo has an internal property named UserDefaultCulture which is the equivalence of the Win32 GetUserDefaultLCID() as commented in .NET Source code:
//
// This is the equivalence of the Win32 GetUserDefaultLCID()
//
internal static CultureInfo UserDefaultCulture
So you can use it to get user default culture this way:
var property = typeof(System.Globalization.CultureInfo).GetProperty("UserDefaultCulture",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
var culture = (System.Globalization.CultureInfo)property.GetValue(null);
Also as another option, you can create a new thread and use its CurrentCulture property:
var culture = new System.Threading.Thread(() => { }).CurrentCulture;

CultureInfo not changing at design time

I have used resource files for localization in design time support. The localized string will be retrieved for designer action items to be added in designer of a control. Here is the code which i used to get the current culture from the resource manager in design mode.
if (resourceManager != null)
{
CultureInfo currentUICulture = CultureInfo.CurrentUICulture;
if (resourceManager.GetResourceSet(currentUICulture, true, true) != null)
{
ResourceManager result = resourceManager;
return result;
}
}
and i have changed the current culture using below code in form level.
public Form1()
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
CultureInfo.CurrentUICulture = new CultureInfo("fr-FR");
InitializeComponent();
}
My Issue - Changed culture for French is not retrieved at design time. Culture got is en-US all the time.
Does anyone know, how to change the culture to reflect at runtime in order to solve the above mentioned issue?
Regards,
Amal Raj
Consider these facts:
CultureInfo.CurrentUICulture returns current thread UI culture.
Your design-time in visual studio, is visual studio's run-time.
Constructor of your Form1 will not execute at design time. Its InitializeComponent methis will be just deserialized to initialize a an instance of its base class Form which is shown in VS designer.
Since your current thread is Visual Studio UI thread and it uses en-US in your system, so you will receive en-US in design time as current UI thread.

Cannot get current server culture

I am working on multi Lingual application in c# asp.net MVC, in which server, client or both may have different languages other than English. My development machine has English as default language, and using language bar in windows, I have set my current language to French (belgium), my client end browser language is Dutch(belgium). For every request I used to change Thread Current Culture to Dutch(client end browser language) using globalization techniques as follows.
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie userCookie = Request.Cookies["Culture"];
string cultureCode = "en-US";
if (userCookie != null)
{
cultureCode = userCookie.Value;
}
CultureInfo culture = new CultureInfo(cultureCode);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
I wanted to find OS current culture, therefore, I used
CultureInfo.InstalledUICulture
to find OS current culture, which should be French(Belgium) FR-BE, but every time it is returning me en-US English(united states). I already know that problem is with windows, it always returns default windows culture instead of current Culture as explained in this question. I wanted to know, is there any way to find current OS culture which is French(Belgium) FR-BE on my server.
I believe you might want to check:
public static CultureInfo CurrentCulture { get; set; }
public static CultureInfo CurrentUICulture { get; set; }
This should contain the default OS culture when your application starts.

Chinese localization does'nt work, shows english always

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.

Categories

Resources