如何在运行时更改应用程序语言?

问题描述 投票:0回答:1

WinUI3的文档出奇的少,我什至找不到如何在应用程序运行时切换显示语言

回到问题,我创建了多种语言的资源文件,但我不知道如何让用户选择他们想要显示的语言。

MainWindow.xaml

<ComboBox x:Name="LanguangeMode" SelectionChanged="Language_SelectionChanged">
    <ComboBoxItem Content="English"/>
    <ComboBoxItem Content="Simplified Chinese"/>
</ComboBox>

并且在 MainWindow.xaml.cs

private void Language_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = sender as ComboBox;
    var selectedIndex = comboBox.SelectedIndex;

    switch (selectedIndex)
    {
        case 0:
            // Change language to English
            break;
        case 1:
            // Change language to Simplified Chinese
            break;
    }
}
c# uwp uwp-xaml winui-3
1个回答
1
投票

这是演示本地化的最小示例。 (我还有一个示例应用程序 repo 及其 video。)

假设您的项目中有 Strings 文件夹和 resources.resw 文件。

  • 弦乐
    • en-我们
      • 资源.resw
        • 名称:LocalizedButton.内容/值:英文
        • 名称:en-US /值:英语(美国)
        • 名称:es-ES / 值:西班牙语(西班牙)
    • es-ES
      • 资源.resw
        • 名称:LocalizedButton.内容/值:Español
        • 名称:en-US / 值:Inglés (Estados Unidos)
        • 名称:es-ES / 值:Español(西班牙)

MainWindow.xaml

<Window
    x:Class="SwitchingLanguageExample.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:local="using:SwitchingLanguageExample"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid RowDefinitions="Auto,Auto">
        <ComboBox
            Grid.Row="0"
            ItemsSource="{x:Bind LanguageItems, Mode=OneWay}"
            Loaded="ComboBox_Loaded"
            SelectionChanged="ComboBox_SelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate x:DataType="local:LanguageItem">
                    <TextBlock Text="{x:Bind DisplayName}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <!--  You need to set the x:Uid to target a control.  -->
        <Button
            x:Uid="LocalizedButton"
            Grid.Row="1" />
    </Grid>

</Window>

MainWindow.cs.xaml

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.ApplicationModel.Resources;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.Globalization;

namespace SwitchingLanguageExample;

public class LanguageItem
{
    public LanguageItem(string languageTag, string displayName)
    {
        LanguageTag = languageTag;
        DisplayName = displayName;
    }

    public string LanguageTag { get; }

    public string DisplayName { get; }
}

public sealed partial class MainWindow : Window
{
    private ResourceManager resourceManager = new();

    private ResourceLoader resourceLoader = new();

    public MainWindow()
    {
        this.InitializeComponent();

        LanguageItems.Add(new LanguageItem("en-US", this.resourceLoader.GetString("en-US")));
        LanguageItems.Add(new LanguageItem("es-ES", this.resourceLoader.GetString("es-ES")));
    }

    public ObservableCollection<LanguageItem> LanguageItems { get; } = new();

    private void ComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        if (sender is not ComboBox languageTagComboBox)
        {
            return;
        }

        if (LanguageItems
            .Where(x => x.LanguageTag == ApplicationLanguages.PrimaryLanguageOverride)
            .FirstOrDefault() is LanguageItem currentLanguageItem)
        {

            languageTagComboBox.SelectedValue = currentLanguageItem;
        }
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (sender is not ComboBox languageTagComboBox ||
            languageTagComboBox.SelectedValue is not LanguageItem selectedLanguageItem)
        {
            return;
        }

        ApplicationLanguages.PrimaryLanguageOverride = selectedLanguageItem.LanguageTag;
        
        ResourceContext resourceContext = this.resourceManager.CreateResourceContext();
        resourceContext.QualifierValues["Language"] = selectedLanguageItem.LanguageTag;
    }
}

不幸的是,您需要重新启动应用程序才能看到更改。此外,这不适用于未打包(未打包)的应用程序。

这就是我创建 WinUI3Localizer 的原因。试一试。希望有帮助!

  • 无需重新启动应用程序即可切换语言
  • 即使在部署后,您/用户也可以编辑本地化字符串
  • 即使在部署后,您/用户也可以添加新语言
  • 使用标准Resources.resw
© www.soinside.com 2019 - 2024. All rights reserved.