Windows Forms Localize resx without changing Culture - c#

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;

Related

Get and Set values in local resource file

I have put 2 strings in the built-in resource file of my main project (not sure how I define this properly). How can I access these values? How would I change them? The resource is public so that the user can change them.
eg:
Project1.Resources.Get("string1").Value();
and
Project1.Resources.Set("string1") = "whatever";
This is pseudo code.
Resources are not intended to be changed at runtime. You should consider using user settings instead. For details, see Using Application Settings and User Settings on MSDN.
This will allow you to use the designer to build the settings, and write:
string string1 = Properties.Settings.Default.String1;
And:
Properties.Settings.Default.String1 = "whatever";
Properties.Settings.Default.Save();
You can get resource by writing the following piece of code
Project1.Resources.String1
You can not change it at runtime.

Internationalize Windows Phone 7 App

I am currently working on a windows phone application that takes some information from the user and returns some other information based on the user input.
The application works great if the specific device has its region settings set to US. If the region settings of the device are set to Greek or German, some problems occur. For example, the US decimal point character "." is considered as "," and vice versa. As a result, all the calculations are false.
What I want to do is internationalize the application so that it works exactly the same no matter what the regional settings are. Is this possible?
If you want your app allways to show number and dates in one specify format you can force the app to allways run in one specify culture like this.
You just have to set the current thread of your app to one specify culture (add to the App.cs file)!
public App()
{
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Set the current thread to US!
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

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.

ResourceLoader and Neutral Language

I want to localize my Windows Store app. I have string\cs-CZ\Resources.resw and I load the strings in C# using ResourceLoader. When I have my system set to cs-CZ locale, the string are returned ok. When I set to to another locale, the GetString method returns an empty string. I have my projects neutral solution set to cs-CZ so what is the problem? How do I make the app always take resources from string\cs-CZ\Resources.resw?
The default locale can be programatically set using
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "cs-CZ";
in OnLaunched

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...

Categories

Resources