MultiLanguage - Logic error beginner - c#

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

Related

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.

Why does my localization only work on my Development-Machine?

I've localized my App in two languages (English and German) with the MulitlingualAppToolkit 4.0. English is the base language, while german is a translation based on the english one.
The translations are stored as resw-file inside folder "strings.en" and "strings.de".
In App.xaml.cs App() I set the culture like this:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = GetCurrentCulture();
CultureInfo.CurrentUICulture = GetCurrentCulture();
private CultureInfo GetCurrentCulture()
{
var cultureName = new DateTimeFormatter("longdate", new[] {"US"}).ResolvedLanguage;
return new CultureInfo(cultureName);
}
(I got this quiet weird way to the regional-culture in Windows 10 from this article https://www.pedrolamas.com/2015/11/02/cultureinfo-changes-in-uwp/ since I recognized that CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture are always "en-EN" no matter what i configurate in my machines regional- and language-settings)
To check if PrimaryLanguageOverride works as expected, I added a TextBox by the name of tbTest on my first Page and a button linkt to this event:
private void Button_Click(object sender, RoutedEventArgs e)
{
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = tbTest.Text;
Frame.Navigate(this.GetType(), 0);
System.IO.File.AppendAllText(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "PrimaryLanguageOverride.txt"),
DateTime.Now + " - Actual PrimaryLanguageOverride:\n " + WIndows.Globalization.ApplicationLanguages.PrimaryLanguageOverride);
}
catch(Exception ex) { Helper.Log.LogUnhandledError(ex); }
}
Now comes the weird stuff:
When Debugging, or executing the App from my Development-Machine, everything works as expected, but when I make an appx-bundle and install it on another (Windows10-Desktop) device, the App does not recognize the its Language.
In my situation, the device is set to german, regional as well as its language. Also when using the test-procedure, it defenetively gets the string I set , as long as it's a valid culture-name, e.g.: "en-EN", "en-US", "de-DE", "de", "en" (all of these are working fine on my development machine) if it's an invalid string, I get an exception, with a log-entry in my unhandled-error-log. It refresh the Page, and even writes the new-set language in my PrimaryLanguageOverride-Log, but it doesn't change any text I did translate.
So my question is, is this a common Issue (since I recognized in UWP/Win10 the culture-system is a little messed up) or do I use the wrong procedure to override the App-Culture?
This is an issue with AppXBundling. When Installing bundles, it checks with the current OS for the installed Language packs and copies the relevant language resources from the bundle and omits the other language files. The objective of a single bundle is to copy necessary resources and build the application and therefore the languages which are not in the system are considered irrelevant. As a fix you could stop generating single bundles and create package for each CPU architecture. Check this for more info

Resource.resx load dynamically in wpf

I'm making an app where the beginning , before the login , the user can choose the language .
I made 4 different files ,
- Resource.resx
- Resource.IT-it.resx
- Resource.ES-es.resx
- Resource.DE-de.resx
I would like to click the the language file was loaded clicked .
Currently in xaml call so the file :
xmlns:res="clr-namespace:MyClass.Properties;assembly=MyClass"
Title="{x:Static res:Resources.mynamevariable}"
How can I make to click on the tongue , you can recharge the resource file ?
ps .: 4 file containing the same variables , so as to draw the translation easily .
Thank you
Try this
MyClass.Properties.Resources.Culture = new System.Globalization.CultureInfo("it-IT");
instead of
Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
You need to set the CultureInfo to the culture that the user has selected. For example to change to Italian you need to use:
Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("it-IT");
Your resource files should also be named Resources.resx - Resources.it-IT.resx - Resources.es-ES.resx - Resources.de-DE.resx
i have solved like:
myclass.Properties.Resources.Culture = new System.Globalization.CultureInfo("it-IT");
where it-IT can are de-DE...fr-FR..ecc

Aspx and aspx.cs getting strings from different resx files

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

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

Categories

Resources