Click here to Skip to main content
15,885,048 members
Articles / Programming Languages / C#
Tip/Trick

Writing a C# Multilingual UWP Application in VS2017

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
8 Feb 2018CPOL2 min read 13.9K   156   7   3
This article provides an example of a C# Multilingual UWP Application in Visual Studio 2017. It does NOT use the Multilingual App Toolkit to achieve this.

Download source

 

Introduction

This article provides an example of a C# Multilingual UWP Application in Visual Studio 2017 and Windows 10. It does NOT use the Multilingual App Toolkit to achieve this. The reason for this was to provide a toolkit independent solution. The first thing you need to do is add the language to your System. In the lower right hand corner of your Windows task bar is the language. Click on that and then select Language Preferences.

Image 1

Add the language of choice. Pay attention to the abbreviation of the language. You will need this later in coding.

Image 2

Create a new Universal Windows Application Project. Mine is called MultiLingualTest. Add a new folder to the project called “Strings”. Make sub folders with the folder names for the languages you wish to include. Create a new Resources.resw file in each language folder. The name of the file is important and they should all be the same.

Image 3

Each of these resource files will contain the name value pairs of the strings for the application. For example, this application has English-US and French.

In English:

Image 4

In French:

Image 5

The names will either be a name.Text for textboxes or a name.Content for Buttons and other controls. The Name has to be the same across resource files although order doesn’t have to be.

In your application GUI, these string resources are consumed in the XAML by using the following example:

XML
<TextBlock x:Uid="message" MinWidth="200" Height="40" />
<Button x:Uid="command01" Name="btnDisplay" 
Margin="10" Click="{x:Bind ClickLanguage}"/>

Setting the language can now be a runtime operation. First, we have the startup language which is set by the following code and is called in the constructor BEFORE calling InitializeComponent.

C#
static bool m_blnInitEnglish = true;
public void InitEnglishOnce()
{
   if (m_blnInitEnglish)
   {
       Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en-US";

 Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
 Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
       m_blnInitEnglish = false;
   }
}

public MainPage()
{
    InitEnglishOnce();
    this.InitializeComponent();
}

Changing Language at runtime is done by the following code and should be done in the main application user interface code. Note that in the code, the language abbreviations noted earlier are used in setting the PrimaryLanguageOverride for the application.

C#
public void ClickLanguage()
{
   if(m_blnEnglish)
   {
      Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en-US";
      m_blnEnglish = false;
   }
   else
   {
      Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "fr";
      m_blnEnglish = true;
   }

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

   if (Frame != null)
      Frame.Navigate(typeof(MainPage));
}

Now when you click on the button, the language changes in the Hello World Application at run time.

In English:

Image 6

and in French:

Image 7

That's it. You basically have identical resource descriptions and then reference them in the XAML code. That way, when you change languages in the app at runtime, the GUI switches which resource file it is grabbing strings from.

Good luck!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseVery good, thank you. Pin
Eidorff29-Jan-21 18:25
professionalEidorff29-Jan-21 18:25 
QuestionBAD DONWLOAD LINK Pin
Member 101955058-Feb-18 3:28
professionalMember 101955058-Feb-18 3:28 
Could you check the link to download the source code?
thanks
AnswerRe: BAD DONWLOAD LINK Pin
trident998-Feb-18 5:10
trident998-Feb-18 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.