I have a website which currently uses EN-US and I also have resource file for french. what code should I need to write to make the application use the french version of the resource file.
The site is a commerce server and share point site that uses .NET
Any code examples would be great.
You have a couple of options. Option one is at the application level. You could do this in the system.web section of the web.config:
<globalization culture="fr-FR" uiCulture="fr-FR" />
Another option is on the page:
<%#Page Culture="fr-FR" Language="C#" %>
or by thread:
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
See http://support.microsoft.com/kb/306162 for more info.
I consider the main resource file's feature as the ability to retrieve localized strings according to the culture specified in the url through code such as:
localresourcestring = (String)GetLocalResourceObject("username")
//will retrieve the localized string for username from your resx (according to the resx's name)!
Here's a great place to start, including a Walkthrough and such..
I would set the language at the Application_BeginRequest() global event in the Global.asax file:
protected void Application_BeginRequest()
{
// Get the CultureInfo object that contains the French language specification
CultureInfo frenchCulture = CultureInfo.CreateSpecificCulture("fr-FR");
// Set the language the current thread uses (per-user)
Thread.CurrentThread.CurrentCulture = frenchCulture;
Thread.CurrentThread.CurrentUICulture = frenchCulture;
}
Hope it helps.
Related
I know it's easy to localize Windows Forms App: set Localizable=True, change Language and set text in Controls for every Language. This information saves in resx-files and application will automatically select the required file. Great!
I know about disadvantages of this solution (you need to rebuild the app if there a typo, it's impossible to change language in runtime, etc), but it's not a problem for me and "resources" is the simpliest, built-in solution.
But this mechanism uses the property Culture of app's thread.
My app is the part ("plugin") of the bigger application and works in the same Thread.
The main application is multilingual too, but it doesn't use Culture to change interface's language. I can change the thread's culture globally, but it crushes the main app's interface.
So my question:
is it possible to manually set the resx-localizable resurce file that will be used? Not based on Culture, but, for example, on some variable in my app:
if (this.Language == "fr")
this.Resources.Add("Form1.fr.resx");
else
this.Resources.Add("Form1.en.resx");
Or something else.
Thank you!
My sandbox:
https://github.com/Tereami/WindowsFormsTestLanguage
The built resources file has a property ResourceManager that's used to return the desired content. This has an overload with a CultureInfo parameter. You can use it to request resources in individual languages:
var desiredCulture = new CultureInfo("en-us");
var text = MyStrings.ResourceManager.GetString(nameof(Resources.ExitMessage), desiredCulture);
If you want to set the culture globally for your resource file, you could also set it through the corresponding property:
var desiredCulture = new CultureInfo("en-us");
MyStrings.Culture = desiredCulture;
var text = MyStrings.ExitMessage;
When opening a (razor) page containing a number with decimal places (set at 4 in this case) the comma separator is interpreted as a thousands separator which expands the number by it's decimal places as seen in this picture:
This is happening in the administration panel of the NopCommerce 3.8 development tree and only on a remote server. I don't see this issue with my debug build locally so this has probably something to do with server settings or even with underlying database (Microsoft SQL-Server). I'm not sure this is a NopCommerce issue though so I'm not limiting it to that tag.
I know that NopCommerce is forcing the en culture in the Administration panel because of how Kendo Grid works. Again, I'm not sure this has anything to do with the actual issue.
This issue also materializes in the fact that the text box doesn't allow the ',' character. i can only use '.'. Trying to save a number like '0.20' is terminated with the message:
The value '0.2000' is not valid for ...
Update 1 (culture set in Global.asax.cs):
if (webHelper.GetThisPageUrl(false).StartsWith(string.Format("{0}admin", webHelper.GetStoreLocation()),
StringComparison.InvariantCultureIgnoreCase))
{
//admin area
//always set culture to 'en-US'
//we set culture of admin area to 'en-US' because current implementation of Telerik grid
//doesn't work well in other cultures
//e.g., editing decimal value in russian culture
CommonHelper.SetTelerikCulture();
}
else
{
//public store
var workContext = EngineContext.Current.Resolve<IWorkContext>();
var culture = new CultureInfo(workContext.WorkingLanguage.LanguageCulture);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
This does seem like an issue with the culture it is using. Try specifying which culture to use in the web.config:
<configuration>
<system.web>
<globalization uiCulture="en" culture="en-US" />
I created two resource files, "SOSResources.en.resx" and "SOSResources.pt.resx".
In my aspx page, when I try to use a string from these files as below, I get it from the standard English language resx file.
asp:Literal ID="btnDelete_Text" Text="<%$Resources:SOSResources, String1%>"
However, if I try to get the same string on code-behind as below, I get it from the Portuguese language file (which is the user language).
btnEdit_Text.Text = Resources.SOSResources.String1
The following code is used to handle this process. It is supposed to select the User's language, or English in case this info is not available. However, only the example from code-behind gets the user language. The example from aspx page always gets the strings from the English resx file.
protected override void InitializeCulture()
{
string lang;
if (ActiveUser != null && ActiveUser.Language != null)
{
lang = ActiveUser.Language;
}
else
{
lang = "en";
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
base.InitializeCulture();
}
Can someone help me? How can I assure that in both cases the page will get the strings from the appropriate language? I'm not sure either if this is the correct way to perform globalization on .NET.
After many hours of research I found this example that worked for me:
https://msdn.microsoft.com/en-us/library/bz9tc508(v=vs.85).aspx
In addition to that, there was another question which also helped me when trying to move the code to the Site:Master or to a PageBase.cs:
ASP.NET Web Page Globalization & Localization in Master Page C# 3.0
I have a localization file that I created that works fine, I called it:
Labels.resx
Now in my global.asax.cs, if a particular querystring value is present, I change the language to french:
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
I created a new resource file:
Labels.fr-CA.resx
I just put a single entry in the new french local file to test it, now when the controller action executes, while debugging I can see in the immediate window that the culture has changed correctly, but my text label is not in french.
What could the issue be?
The properties for my Labels.fr-CA.resx file are exactly like my Labels.resx file:
embedded resource
PublicResXFileCodeGenerator
(same namespace)
Also, if the given key isn't found, does it automatically lookup the value in the Strings.resx key as a fallback or does it cause an exception?
Try add the folowing line to global.ajax.cs:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
I have a webpage that has to be displayed in several different languages based on user selection. For that, I'm using RESX files for each of the asp.net webpages. I don't want to used the automatic detection of the language in the browser, but I want to set the language, again, based in the user selection. In order to accomplish this I'm doing the following:
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-MX", false);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-MX", false);
OR
Page.Culture = "es-MX";
Page.UICulture = "es-MX";
But neither of those are working as expected! I'm initializing the Culture in the Init method of the page but it will always display the default language. I'm inspecting the values of those properties and those have the culture correctly, but still is not being rendered using the RESX file. Any ideas? Suggestions?
Thanks
In case someone runs into this issue when working with Explicit localization, here is what has to be done:
protected override void InitializeCulture()
{
Page.Culture = "en-US";
Page.UICulture = "en-US";
}
From the net-tutorials.com website:
Since the Page directive is just a shortcut to the Page class, this can be done from CodeBehind as well. However, we have to do it at a certain point, before the page is being rendered, to make sure that it has the desired effect. This is where the InitializeCulture() method comes into play, a method that is called by ASP.NET pretty early in the Page life cycle, which you can override.
Try this
System.Resources.ResourceReader resourceReader
= new System.Resources.ResourceReader("RES_PATH");
Now you can use this to load users language like es.resx
System.Resources.ResourceReader resourceReader
= new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0]
+ ".resource");