Skip to main content

WPF C#/VB
Make Your App Multilingual



C#

// AZUL CODING ---------------------------------------
// WPF C#/VB - Make Your App Multilingual
// https://youtu.be/FJSJLf76mBM


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Threading;
using System.Globalization;

namespace Multilingual_App
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Watch the video to see how to add the following resource string
            OtherTxt.SetResourceReference(ContentProperty, "OtherStr");
            SetLang(Properties.Settings.Default.lang);
        }

        private void LangBtns_Click(object sender, RoutedEventArgs e)
        {
            SetLang(((Button)sender).Tag.ToString());
        }

        private void SetLang(string lang)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);

            Application.Current.Resources.MergedDictionaries.Clear();
            ResourceDictionary resdict = new ResourceDictionary()
            {
                Source = new Uri($"/Dictionary-{lang}.xaml", UriKind.Relative)
            };
            Application.Current.Resources.MergedDictionaries.Add(resdict);

            EnglishBtn.IsEnabled = true;
            FrenchBtn.IsEnabled = true;
            SpanishBtn.IsEnabled = true;

            switch (lang)
            {
                case "en-US":
                    EnglishBtn.IsEnabled = false;
                    break;
                case "fr-FR":
                    FrenchBtn.IsEnabled = false;
                    break;
                case "es-ES":
                    SpanishBtn.IsEnabled = false;
                    break;
                default:
                    break;
            }

            Properties.Settings.Default.lang = lang;
            Properties.Settings.Default.Save();
        }
    }
}

Enjoying this tutorial?


VB.NET

' AZUL CODING ---------------------------------------
' WPF C#/VB - Make Your App Multilingual
' https://youtu.be/FJSJLf76mBM


Imports System.Threading
Imports System.Globalization

Class MainWindow
    Public Sub New()
        InitializeComponent()

        OtherTxt.SetResourceReference(ContentProperty, "OtherStr")
        SetLang(My.Settings.lang)

    End Sub

    Private Sub LangBtns_Click(sender As Button, e As RoutedEventArgs)
        SetLang(sender.Tag.ToString())

    End Sub

    Private Sub SetLang(lang As String)
        Thread.CurrentThread.CurrentCulture = New CultureInfo(lang)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(lang)

        Application.Current.Resources.MergedDictionaries.Clear()
        Dim resdict As ResourceDictionary = New ResourceDictionary() With {
            .Source = New Uri($"/Dictionary-{lang}.xaml", UriKind.Relative)
        }
        Application.Current.Resources.MergedDictionaries.Add(resdict)

        EnglishBtn.IsEnabled = True
        FrenchBtn.IsEnabled = True
        SpanishBtn.IsEnabled = True

        Select Case lang
            Case "en-US"
                EnglishBtn.IsEnabled = False
            Case "fr-FR"
                FrenchBtn.IsEnabled = False
            Case "es-ES"
                SpanishBtn.IsEnabled = False
            Case Else
        End Select

        My.Settings.lang = lang
        My.Settings.Save()

    End Sub
End Class

MainWindow.xaml

<!-- AZUL CODING --------------------------------------- -->
<!-- WPF C#/VB - Make Your App Multilingual -->
<!-- https://youtu.be/FJSJLf76mBM -->


<Window x:Class="Multilingual_App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Multilingual_App"
        mc:Ignorable="d"
        Title="Azul Coding" Height="296" Width="566" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
    <Grid>
        <StackPanel VerticalAlignment="Center">
            <!-- Watch the video to see how to add the resource dictionaries that contain the following resources -->

            <!-- "Welcome" -->
            <Label Content="{DynamicResource TitleStr}" FontSize="24" FontWeight="SemiBold" HorizontalAlignment="Center"/>

            <!-- "This is an example multilingual app" -->
            <Label Content="{DynamicResource SubtitleStr}" FontSize="16" HorizontalAlignment="Center"/>

            <!-- Text added in code-behind: "This is some other text" -->
            <Label x:Name="OtherTxt" Content="" FontSize="14" HorizontalAlignment="Center"/>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,25,0,0">
                <Button x:Name="EnglishBtn" Content="English" Tag="en-US" Padding="10,5" FontSize="16" FontWeight="SemiBold" IsEnabled="False" Click="LangBtns_Click"/>
                <Button x:Name="FrenchBtn" Content="Français" Tag="fr-FR" Padding="10,5" FontSize="16" FontWeight="SemiBold" Margin="10,0,0,0" Click="LangBtns_Click"/>
                <Button x:Name="SpanishBtn" Content="Español" Tag="es-ES" Padding="10,5" FontSize="16" FontWeight="SemiBold" Margin="10,0,0,0" Click="LangBtns_Click"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

App.xaml

<!-- AZUL CODING --------------------------------------- -->
<!-- WPF C#/VB - Make Your App Multilingual -->
<!-- https://youtu.be/FJSJLf76mBM -->


<Application x:Class="Multilingual_App.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Multilingual_App"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary-en-US.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>