Resource.resx load dynamically in wpf - c#

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

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.

Localization using satellite assembly returns default language

I'm trying to localize my application with satellite assemblies.
I tried to follow this blog: http://msdn.microsoft.com/en-us/library/21a15yht(v=vs.110).aspx
I've created two files, one file has the name "Global.resx" with an english string, and the second file has the name "Global.nl-NL.resx" with a dutch string.
After this i created a .resources file from the dutch file with resgen. With al.exe I made a .dll called LocalizationLab.resources.dll and i stored it in the folder: /bin/debug/nl-NL.
in my application i set the CurrentUICulture to nl-NL. and i call the string with Global.test. See underneath
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
Console.WriteLine(Global.Test);
This returns the english string instead of the dutch one. When i debug and take a look into Global and watch the resourcesets the resourcemanager is using, I see three resourcesets: "nl", "nl-NL" and "" all three of them have english string values in them.
Can anyone tell me what i'm doing wrong?
Thanks in advance.
I eventually found out what the problem was,
The problem was that the generated code for initializing the resourcemanager was looking like:
ResourceManager rm = new ResourceManager("LocalizationLab.Global", typeof(Global).Assembly);
After some puzzling i found out that it should be this:
ResourceManager rm = new ResourceManager("Global", typeof(Global).Assembly);
which worked!

French localization file doesn't seem to be working

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

Change localization at execution WPF

I have a WPF application that I want it to be two languages. I duplicated my Resources.resx and built my two languages like this:
So when I first load my MainApplication I do this:
Properties.Resources.Culture = new CultureInfo("es-ES");
before the
InitializeComponent();
So everything is loaded in the desired language. Now I want to go the obvious step further, and I designed a Select language on my application:
Any idea on how to reload the interface for the different languages at execution time?
EDIT:
I found this link, and seems to work. But I have a problem. When I try to find the Resources x:key it launches an error... It says ResourceReferenceKeyNotFoundException. Go here to check my mistake.
You want to change the culture for the UI thread, this should work:
var culture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
I followed this interesting link.

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

Categories

Resources