Click here to Skip to main content
13,863,239 members
Click here to Skip to main content
Add your own
alternative version

Stats

6.6K views
98 downloads
7 bookmarked
Posted 7 Feb 2018
Licenced CPOL

Writing a C# Multilingual UWP Application in VS2017

, 8 Feb 2018
Rate this:
Please Sign up or sign in to vote.
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.

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

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.

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:

In French:

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:

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

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.

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:

and in French:

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)

Share

About the Author

trident99
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionBAD DONWLOAD LINK Pin
Member 101955058-Feb-18 4:28
professionalMember 101955058-Feb-18 4:28 
AnswerRe: BAD DONWLOAD LINK Pin
trident998-Feb-18 6:10
membertrident998-Feb-18 6: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 8 Feb 2018
Article Copyright 2018 by trident99
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid