Aspx and aspx.cs getting strings from different resx files - c#

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

Related

How to fix Asp.net-mvc website (using Culture and .resx files) changing language out of nowhere?

We support a few languages, for which we have different traductions stored in the Properties of our website. Here are some of them:
Resources.es.resx, Resources.fr.resx, Resources.resx (which contains english traductions).
Sometimes (like 3 times in the last year), our website becomes in french for ALL our english users.
We implemented some logging to try and understand what is happening. The last time the bug occured, what we observed is that when trying to get a sample string from our Resources.resx file, the result was from the Resources.fr.resx file. This was the case for all our users.
This is how we obtained that string:
string englishResource = Properties.Resources.ResourceManager.GetString("Add", new CultureInfo("en-ca", useUserOverride: false));
It's as if the Resources.resx was not found and the language defaulted to Resources.fr.resx.
When the website works properly, the returned string is always from Resources.resx, as expected.
Does anyone know why this is happening and how to fix it ?
EDIT:
here is what we get for an english user when the website is displayed in french:
{
SessionCulture: “en”,
UserLanguages: [
“en-US”,
“en;q=0.9”,
“fr;q=0.8",
“es;q=0.7”,
“ca;q=0.6",
“it;q=0.5”
],
LocalizationSystemLanguage: “en”,
CurrentThreadCulture: “en-CA”,
CurrentThreadUICulture: “en-CA”,
EnglishResource: “Ajouter”,
FrenchResource: “Ajouter”,
SpanishResource: “Agregar”
}
the "EnglishResource" string should read "Add" instead of "Ajouter"

Localization in MVC 5 - How can I render multi-lingual text in my view using a resource file?

I am trying to create a multi-lingual navigation in an MVC 5 application.
What I've done:
Set System.Threading.Thread.CurrentThread.CurrentUICulture to either "en-US" or "es-ES" based on cookie value (defaults to English
unless user selects Spanish)
Created three resource files (this is my first time using them, so I'm not certain I fully understand the concept...) Index.resx, Resouce.en-US.resx, Resouce.es-ES.resx. Each resource file is in a folder called App_GlobalResources folder
Added a name/value combination to each .resx file, Home/Home for Index.resx and en-US.resx, and Home/Casa for es-ES.resx
Tried using #Resources.Index.Home in my layout file, thinking that when the value of CurrentUICulture changed from en-US to es-ES and visa-versa, the language would change based on the values in my resource files.
Could someone please let me know how I can get the Spanish text when the value CurrentUICultureis "es-ES", and the English text when it is "en-US"?
_Layout.cshtml
Resource.resx
EDIT
I should have stated - #Resources.Index.Home does render the text "Home" in the navigation. However, when I switch CurrentUICulture to "es-ES", it still renders "Home", not "Casa"
EDIT 2
Here is how I set CurrentUICulture is global.asax
public void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (Request.Cookies["lang"] == null)
{
HttpCookie lang = new HttpCookie("lang");
lang.Value = "english";
lang.Expires = DateTime.Now.AddDays(30d);
Response.Cookies.Add(lang);
}
else if (Request.Cookies["lang"] != null)
{
if (Request.Cookies["lang"].Value != null && Request.Cookies["lang"].Value == "english")
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
}
else if (Request.Cookies["lang"].Value != null && Request.Cookies["lang"].Value == "spanish")
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-es");
}
}
}
Just try to replace #Resources.Index.Home with #Resources.Home.
This is ur understanding about localization.
Every app with localization (say N cultures) must have N .resx-files with the same name but different suffixes. + Default culture can be used without suffixes.
So u have 2 cultures - u must use 2 .resx files, not more. So Index.resx is not needed at all.
This should work. If not, more fixes:
- Don't use App_GlobalResources folder. Just create .resx in common project-folders or in project root folder, just as in desktop .NET-apps.
This helped me, hope this will help u.

MultiLanguage - Logic error beginner

I am using WIndows 8, and Visual Studio 2012 Metro. I need to add a DropDownList and make the user select languages. When the user selects a particular language i need all the Text values in the label to change to that particular language.
I followed this tutorial
I R-CLick project added `APPGlobal_Resource` folder
I R-Clicked and added `APPGlobal_Resource` and created a Resource file called rss.resx
Then i duplicated this file and named it rss.fr.resx
I gave 2 values `String1` and `Hello` and `Bonjour` as values (in both files)
R-Click `dropdownlist` and `edit item`, and `ADD` and then gave `en-US` as `Name` and `value`as `en-US` (same way i gave `en-fr`)
Then i set the Label property, `Data (Expression)` `Bindable property` to `Text` and `Expression Type` to `Resources`.
I gave the `Class Key` to `Res` and `Resource Key` to `String1`.
and i wrote the following code in Default.aspx.cs
protected override void InitializeCulture()
{
base.InitializeCulture();
String cult = Request["DropDownList1"];
if (cult != null)
{
Culture = cult;
UICulture = cult;
}
}
The drop down displays with en-US and en-fr but when i select it nothing changes. How can i resolve this ?
Not sure, but it can help:
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo( cult );

Loading dynamically RESX file not working

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

How to use various culture specific resource files on ASP.NET

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.

Categories

Resources