How do I force a culture in asp.net? - c#

I want to be able to set the culture during runtime. For example:
protected void Page_Load(object sender, EventArgs e)
{
Page.Culture = "fr-FR";
Page.UICulture = "fr";
}
But that's not having any effect. I'm using resource files for translation. If I change the language of my browser it works fine, but I want the user to also be able to choose the language as well. So in this case the user wants French as the language.
Any ideas? I'm lost.

If you're creating a site where you are allowing the user to change language for example, then you need to perform this in the Global.asax file in the Application_BeginRequest method.
Each request will then have the culture set.
You simply set the following 2 lines:
Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
The first line will set the number/date/etc formatting.
The second line specifies which resource localization to load - which will contain your translated content.

You can try with this:
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en-US");
Refer this article from MSDN for further details.

If you want to set it for whole application, you can set it in your Global.asax as
Thread.Current.Culture = New System.Globalization.CultureInfo("fr-FR");

Related

How to use Multi language in C#

I'm trying to prepare an application with more languages available.
I prepared the simplest example to learn it, I done a lot of tentative but I'm not able to do it.
CultureInfo cul = new CultureInfo("de-De");
Resources.Culture = new System.Globalization.CultureInfo("de-De");
label1.Text = TestLanguages.Properties.Resources.Saluto;
In my application I have two resources different resources , one for Italian language, one for German.
Italian Resource : Saluto -> Ciao
German Resource : Saluto -> Hallo
But I can't use the German one. How can I do it?
You have to change the UI culture of the currently executing thread.
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-De");
label1.Text = TestLanguages.Properties.Resources.Saluto;
See the documentation for Thread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.

Why are my different language resx files being ignored

I have been following this page to make my site support multiple cultures.
the code all compiles and runs, but it seems to be ignoring my culture specific resx files.
Right now the test is pretty superficial, and I simply have 2 resource files;
Resources.resx and Resources.au.resx
My template is calling #Resources.PageFooter which is the only string defined in both files.
If I browse to http://mysite/index and inspect the page footer it contains my default value. If I change my url to http://mysite/au/index then it still displays the default value. However inspecting CurrentThread.CurrentUICulture and CurrentThread.CurrentCulture shows that both are now set to au, but it is still picking up and using Resources.resx not Resources.au.resx
My Resource files are in a separate folder Resources, as the article suggests.
So I am stuck.
After some investigation, I have found that if I create another resx file (for Italian), then that works.
So it looks like the files that are being ignored are the 'variations' such as American English, Australian English and so on.
First, please change au to en-AU, since this is the correct colture code for Australian English.
In your Global.asax file, set the Application_BeginRequest method like below:
void Application_BeginRequest(Object sender, EventArgs e)
{
string langCode = "en"; //default
if (somecondition)
langCode = "en-AU";
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(langCode);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(langCode);
}
You need the somecondition I mentioned above to check where the user is coming from - by header, cookie, URI or any other way you want.

Always returns the string with the default language from a resource file

I have two resources files, Emails.es.resx (es-ES) and Emails.eu.resx (eu-ES), and I have a problem to retrieve a string from eu-ES file with myLib.Emails.ResourceManager.GetString("textKey", "eu-ES");
At local this works properly but at server, is a web app + IIS, not works, always return a correct value of "textKey" but with default language es-ES.
I cleaned the solution and rebuilded all, but without results.
Does anyone knows what could happen?
Make sure you set the correct culture. Suppose you have given option somewhere at website default page where user can choose language. On language change save the selected language in application state somewhere.. session or catch or whatever suite you. Set culture as following
explained at following link:
protected void Page_PreInit(object sender, System.EventArgs e) {
System.Globalization.CultureInfo lang = null;
lang = new System.Globalization.CultureInfo("zh-CN");
System.Threading.Thread.CurrentThread.CurrentCulture = lang;
System.Threading.Thread.CurrentThread.CurrentUICulture = lang;
}
Now retrieve your resources.. it should return correct one.
Rename your files to Emails.es-ES.resx and Emails.eu-ES.resx

Different DateTimeFormat for dev and test environment

In the Application_BeginRequest() method of global.asax.cs in my ASP.NET MVC project there is code:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(EnCultureKey);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(EnCultureKey);
Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo(EnGBCultureKey).DateTimeFormat;
The variables are
private const string EnCultureKey = "en-US";
private const string EnGBCultureKey = "en-GB";
On the dev environment all the dates are in DD/MM/YYYY format, but on the test environment they are in MM/DD/YYYY format.
Could You please advise me on what could be the cause of this difference?
UPDATE:
Please take a look at Setting Culture for ASP.NET MVC application on VS dev server and IIS
If you do want to override these settings for all your pages (instead of giving the User a choice) then the standard way is a setting in web.config :
<globalization uiCulture="en" culture="en-GB" />
The MSDN page also points you to overriding InitializeCulture() if you want to use code.
InitializeCulture() happens early but I suspect that Application_BeginRequest happens even earlier and that its effects are overridden.
Try using that code on the
Application_Start method of the global.asax, that will ensure that every time you start your application, the culture info is set to your specifications.
Make sure you're using the right format time, for example, to show the date:
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("d"));
Ah I see, sorry.
I think it is because of course in this way you are changing the culture of a single thread, not all thread of the application.
Put your code in a place that is executed by every thread in the application, for example, the page load.
Well, I didn't actually find what IIS setting is responsible, but I've overridden it in Application_PreRequestHandlerExecute() and it finally worked:
var culture = CultureInfo.CreateSpecificCulture(EnCultureKey);
culture.DateTimeFormat = CultureInfo.CreateSpecificCulture(EnGBCultureKey).DateTimeFormat;
Thread.CurrentThread.CurrentCulture = culture;
culture = new CultureInfo(EnCultureKey);
culture.DateTimeFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
Thread.CurrentThread.CurrentUICulture = culture;

How do I tell my webpage to use a specific UI Culture?

I can tell my page to use a certain CultureInfo like
System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
The code above only set's the CultureInfo, not the UICulture, how can I tell the Page to bypass what the browser says and use a specific one, so all GlobalResource's could be applied to the correct culture?
in the code above and having Swedish as my first browser language I get:
System.Globalization.CultureInfo.CurrentUICulture.Name --> sv-SE
System.Globalization.CultureInfo.CurrentCulture.Name --> en-US
I need to set the CurrentUICulture so all localization is made, in this case, in English and not Swedish, like browser is set to:
(source: balexandre.com)
Try
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
I tried it in OnInit of my page and it loaded the resources properly.
EDIT: or you could try setting it in the web.config as shown here:
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
There's an example on the MSDN website that explains how to do this: How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
Essentially you can set the CurrentCulture and CurrentUICulture properties of the currently executing thread (see the article for the full code example, this is an extract):
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

Categories