C# changing application language programmatically (User prefference) - c#

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.

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;

CultureInfo not changing at design time

I have used resource files for localization in design time support. The localized string will be retrieved for designer action items to be added in designer of a control. Here is the code which i used to get the current culture from the resource manager in design mode.
if (resourceManager != null)
{
CultureInfo currentUICulture = CultureInfo.CurrentUICulture;
if (resourceManager.GetResourceSet(currentUICulture, true, true) != null)
{
ResourceManager result = resourceManager;
return result;
}
}
and i have changed the current culture using below code in form level.
public Form1()
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
CultureInfo.CurrentUICulture = new CultureInfo("fr-FR");
InitializeComponent();
}
My Issue - Changed culture for French is not retrieved at design time. Culture got is en-US all the time.
Does anyone know, how to change the culture to reflect at runtime in order to solve the above mentioned issue?
Regards,
Amal Raj
Consider these facts:
CultureInfo.CurrentUICulture returns current thread UI culture.
Your design-time in visual studio, is visual studio's run-time.
Constructor of your Form1 will not execute at design time. Its InitializeComponent methis will be just deserialized to initialize a an instance of its base class Form which is shown in VS designer.
Since your current thread is Visual Studio UI thread and it uses en-US in your system, so you will receive en-US in design time as current UI thread.

Change language during runtime using values from app settings in a Windows 8.1 store app

The default mechanism in Store app for language localization is by reading the correspondning resources.resw file based on the machines regional/language settings. How can we override this behaviour to make it read from apps settings(from settingsflyout) during runtime.
Is it possible? or any other mechanism is available to achieve this?
I tried setting languages property .
Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
rootFrame.Language = Windows.Globalization.ApplicationLanguages.ManifestLanguages[1];
Windows.UI.Xaml.Window.Current.Content = rootFrame;
but this not working.
In the App.xaml.cs file add this line to override primary language:
public App()
{
// Override to english
ApplicationLanguages.PrimaryLanguageOverride = "en";
this.InitializeComponent();
this.Suspending += OnSuspending;
}

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.

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