Change localization at execution WPF - c#

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.

Related

Windows Forms Localize resx without changing Culture

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;

C# changing application language programmatically (User prefference)

I need a few tips on how to change application launguage. (Windows 8.1)
So here is my code that I put under OnLaunched method, It gets the current preffered launguage. I am using Multilingual app toolkit. I have all the resource files.
But I have no idea what to do next.
var rootFrame1 = new Frame();
rootFrame1.Language = Windows.System.UserProfile.GlobalizationPreferences.Languages[0];
You have to set current CultureInfo properties in this way:
var culture = new CultureInfo("en-US");
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = culture.Name;
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Some resources point to placing this code in Page constructor to react to your default language being changed
ResourceContext.GetForCurrentView().QualifierValues.MapChanged +=
(IObservableMap<string, string> sender, IMapChangedEventArgs<string> e) =>
{
ResourceManager.Current.DefaultContext.Reset();
};
Also, worth mentionig is that preferable method for changing language is setting Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride property.
This works not so good for Windows Phone 8.1, but some claim that this solved their issue on Windows 8.1.

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

Mysterious Resources /CultureInfo behaviour in WPF App

i have two Resources files in the Properties folder of a WPF-project (VS 2008):
Resources.resx
Resources.de-DE.resx
Selecting the culture "de-DE" does not work (no error, but always the strings from "Resources.resx" are used):
public App()
{
UntitledProject2.Properties.Resources.Culture = new CultureInfo("de-DE");
}
BUT: if I rename "Resources.de-DE.resx" to "Resources.fr-CA.resx" or "Resources.en-US.resx"
and then set it via
UntitledProject2.Properties.Resources.Culture = new CultureInfo("fr-CA");
it works!! But why!? Mysterious...
By default, WPF will always use "en-US"; at least it did the last time I checked (which was .net 3.5). If you want WPF to instead use the culture currently set by the system, you would execute this code block:
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(
CultureInfo.CurrentCulture.IetfLanguageTag)));
This will override the default value used for the FrameworkElement's Language dependency property, which, again, is "en-US" by default.
Execute this code once and early on in the lifetime of your application. AFter that, you shouldn't have to worry about it again, unless you expect your user to be switching the culture in the middle of program execution...

Changing the CurrentUICulture at runtime using locbaml method

Using the locbaml method, I've created a new resource dll and am trying to programatically change the CurrentCulture and CurrentUICulture on the fly, but it doesn't seem to be working.
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ar-SA");
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
Am I missing something obvious?
As far as I know, using the 'locbaml' metod you cannot switch languages on the fly, thus you have to setup Current[UI]Culture appropriately if needed before showing any UI.

Categories

Resources