Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# change app language programmatically UWP realtime

Tags:

c#

windows-10

uwp

In my application for each language string resources are stored separately and are displayed depending of type of language environment. I want to change the language in the application settings. How do I realize that after the language selection instantly apply it in the user interface?

like image 273
AlexeySRG Avatar asked Sep 22 '15 11:09

AlexeySRG


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

We can use ApplicationLanguages.PrimaryLanguageOverride to change the language during runtime without restart the app.

For example: I have two languages supported "en" and "fr", localized message will show up in textblock.

  1. Add using Windows.Globalization;

  2. Change the default language from "en" to "fr" by

    ApplicationLanguages.PrimaryLanguageOverride = "fr";
    
  3. Re-navigate to the current page to refresh the UI.

    Frame.Navigate(this.GetType());
    

Note that, you need to compare the PrimaryLanguageOverride with the system culture to set the language for next app launch, because the PrimaryLanguageOverride setting is persisted. And if you have page cache enabled, when you apply a different language on the fly, you need to clear the cache by setting Frame.CacheSize = 0; first then set it back.

like image 75
Alan Yao - MSFT Avatar answered Sep 29 '22 22:09

Alan Yao - MSFT


Some addition to Alan Yao's answer. There's one missing piece: After you set the Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride and before re-navigating to the current page, you must call these two functions:

Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();

This way you won't need the Task.Delay() workaround mentioned by Michael Woolsey.

One more important last step: when creating a Store package, you should make sure to set the "Generate app bundle" setting value to "Never". Reason from this article:

Because otherwise, it will create a bundle. It means that he will cut your application into different parts to optimize the download. Only the parts that are relevant for the devices will be downloaded. For example, if there are the assets in different resolution, it will only download the ones that are suitable for the device. Same thing for languages, it will only download the resources file relevant to the language of the device. So if you try to change language, it will fall still fall back on the same base language, because others are not installed.

like image 25
Péter Bozsó Avatar answered Sep 29 '22 22:09

Péter Bozsó


@ThisWillDoIt and @Herdo

I added a delay so that the "First" time it would work in my situation:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = languageCode;

await Task.Delay(100);

Frame.Navigate(this.GetType());

Hope it helps work for you.

like image 24
Michael Woolsey Avatar answered Sep 29 '22 21:09

Michael Woolsey