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.
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 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.
We are experiencing weird behaviour between a web application and windows service when trying to perform a ToString() on a DateTime value.
See the example below.
DateTime parsedReportDate;
reportDate = DateTime.Now.ToString("yyyyMMdd");
reportDateWithSlash = DateTime.Now.ToString("dd/MM/yyyy");
if (DateTime.TryParse(MyDateValue, out parsedReportDate))
{
reportDate = parsedReportDate.ToString("yyyyMMdd");
reportDateWithSlash = parsedReportDate.ToString("dd/MM/yyyy");
}
--reportDateWithSlash on Web Application: 28/03/2017
--reportDateWithSlash on Windows Service: 28-03-2017
The Windows Service calls the same function as the Web Application does, so why is the formatting different then?
The formatting of dates to strings uses a CultureInfo object to know what format to use.
Each Thread has a Thread.CurrentCulture property.
You can find out what CultureInfo the current Thread is set by getting the current Thread using Thread.CurrentThread and then inspecting it's Thread.CurrentCulture property.
public class Program
{
public static void Main()
{
Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
}
}
https://dotnetfiddle.net/dsA3VT
Output: en-US
You can set the CultureInfo for the the Thread, or pass it with each call to ToString.
Setting Thread.CultureInfo
You can set the Thread.CultureInfo using the same property as you use to read it.
Thread.CurrentCulture = new CultureInfo("en-gb");
Unfortunately .Net Fiddle doesn't support changing thread properties.
I didn't know this, but bradbury9 pointed out that since .net 4.6 you can set the CultureInfo.CurrentCulture property as well.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
Unfortunately .Net Fiddle doesn't support changing the culture this way either.
Passing CultureInfo to ToString
'DateTime.ToString' has overloads which can take an IFormatProvider, and CultureInfo impliments IFormatProvider.
DateTime.Now.ToString(new CultureInfo("en-gb"));
https://dotnetfiddle.net/qkS5HF
public class Program
{
public static void Main()
{
var dateTime = DateTime.Now;
Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
Console.WriteLine(dateTime.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(dateTime.ToString(new CultureInfo("en-us")));
}
}
Output:
en-US
03/28/2017 09:43:49
3/28/2017 9:43:49 AM
The problem must come from having different cultures. Using the DateTime.ToString (String, IFormatProvider) overload with the CultureInfo.InvariantCulture property should solve the problem:
DateTime.Now.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
it may be what is calling the Windows service is formatting the date. the code certainly is clear enough. Try debugging the windows service by attaching to the running process and see what it generates. If your service consumer is a web app, look at F12 developer tools and see what is getting sent back int he response stream.
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.
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.