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 string
s 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 string
s from.
Good luck!