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

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);

Related

How can I get english language specific file information strings with GetVersionInfo?

I'm trying to use FileVersionInfo.GetVersionInfo() for some Windows(C) executables, such as rasapi32.dll. System redirects such requests to my language specific resources files (systemRoot\ru-Ru\rasapi32.dll.mui). If I can avoid this and get info from real executable (rasapi32.dll) with english copyright, product name and so on strings?
Setting System.Threading.Thread.CurrentThread.CurrentUICulture and System.Threading.Thread.CurrentThread.CurrentCulture to "en-US" doesn't helps. Also setting CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture to "en-US" doesnt't works too. What have I do to avoid redirection calling GetFileVersionInfo() (used to call it directley fron version.dll too) to mui files, but read original?
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new
System.Globalization.CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo("en-US");
FileVersionInfo fileVersionInfoUS =
FileVersionInfo.GetVersionInfo("C:\\WINDOWS\\system32\\rasapi32.dll");
}
fileVersionInfoUS.FileDescription will contain "API удаленного доступа", instead of english "Remote Access API", other fields etc.
Thanks for advices.

Windows Form Culture not setting up

I am trying to get right string values from culture resource file but it's not working, always returning english resources,
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
but
Resources.Resource1.myResource;
still getting english resources, I have two files Resource1.resx and Resource1.fr-FR.resx
I think that you need
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR")
Thread.CurrentThread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look
up culture-specific resources at run time.

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;

Problem with changing language using CultureInfo

Language is not getting changed when we are giving specific culture like "fr-FR" through Resource File.please help me out of it if any one knows ,Thanks in advance.
CultureInfo cinfo = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = cinfo;
Thread.CurrentThread.CurrentUICulture = cinfo;
The below code indicate that accessing the value through resource1 file according to the culture .
_inboxpage.Text = Resource1.Ready;
The resource files need to have a special naming convention to work transparently with different culture information.
You create a separate resource file for each language that you want to support or for a language and culture. Have one separate neutral resource file for the application to fall back upon in case the required key/value pair is not found.
ex:
Resources.resx //neutral resource file
Resources.fr.resx //french specific file
Resources.fr-FR.resx //French language for France
and so on.
You can get more details here:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
Finally i got a solution that while doing Localization in Plugin Application we need to copy that culture folders like "fr-FR" to corresponding Main Application then it will works fine.

Culture of thread for method render of LocalReport

I'm using a localreport object into an asp.net application.
This report is fed by an array of object. So on the render of the report, some properties of the classe are called.
Class ClassForReport
{
string Date
{
get{return _aDate.ToshortDateString();}
}
}
Now the code for rendering and the problem:
//first of all, I change de culture for taking in account the choice of the user
CultureInfo ci = CultureInfo.CreateSpecificCulture(isoLanguageName_);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
//Here, my culture is now: FR-be
MyLocalReport.render(...) // in this method, the property Date above is called. And when debugging I see that the culture is EN !!!
...
//and here, my culture is still Fr-be
So it seems that when the method render is called, it launch a thread and take the culture of the server and not the culture of the process.
The only workarround I see is changing my report to contains a date and then giving a parameter of culture and formating all my date in all my reports to the given culture...
So I realy hope there is a way to tell the report to take the curent culture of the asp thread and not taking some other culture comming from nowhere.
thx in advance
In ".rdlc" Designer on your ReportFile, set on the Report in Language property "=User!Language".
<Report>
...
<Language>=User!Language</Language>
...
</Report>
then your System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
will work on the values in report.Render(...); like dates,etc.
Cheers.

Categories