• 背水一战 Windows 10 (80)


    [源码下载]


    背水一战 Windows 10 (80) - 本地化



    作者:webabcd


    介绍
    背水一战 Windows 10 之 本地化

    • Demo
    • 改变语言



    示例
    1、演示本地化的基本应用
    Localization/LocalizationDemo.xaml

    <Page
        x:Class="Windows10.Localization.LocalizationDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Windows10.Localization"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="Transparent">
            <Grid.Resources>
                <ResourceDictionary>
                    <local:LocalizedStrings x:Key="Localized"/>
                </ResourceDictionary>
            </Grid.Resources>
    
            <StackPanel Margin="10 0 10 10">
    
                <TextBlock>
                    <Run>本地化资源文件,以下举例说明:</Run>
                    <LineBreak />
                    <Run>1、在 en 目录下的是英文资源文件,在 zh-hans 目录下的是简体中文(zh 代表中文,hans 代表简体中文)资源文件(关于限定符的详细说明请参见 /Resource/Qualifiers/)</Run>
                    <LineBreak />
                    <Run>2、Resources.lang-en.resw 代表英文资源文件,Resources.lang-zh-hans.resw 代表简体中文资源文件(关于限定符的详细说明请参见 /Resource/Qualifiers/)</Run>
                    <LineBreak />
                    <Run>3、Package.appxmanifest 中引用的字符串也支持本地化,引用方式:ms-resource:Hello 或 ms-resource:///Resources/Hello</Run>
                    <LineBreak />
                    <Run>4、Tile 和 Toast 中引用的字符串也支持本地化,引用方式:ms-resource:Hello 或 ms-resource:///Resources/Hello</Run>
                    <LineBreak />
                    <Run>5、当无法找到某语言对应的资源时,系统会自动使用 Package.appxmanifest 中设置的默认语言所对应的资源</Run>
                </TextBlock>
    
                <!--
                    通过 x:Uid 本地化控件的各个属性,请参看资源文件中的 HelloTextBlock.FontSize 和 HelloTextBlock.Text
                -->
                <TextBlock x:Uid="HelloTextBlock" Margin="5" />
    
                <!--
                    图片的本地化
                -->
                <Image Source="/Localization/Logo.png" Width="200" Height="100" Margin="5" HorizontalAlignment="Left" />
    
                <!--
                    code - behind 方式获取本地化资源
                -->
                <TextBlock x:Name="lblMsg1" Margin="5" />
    
                <!--
                    code - behind 方式获取本地化资源
                -->
                <TextBlock x:Name="lblMsg2" Margin="5" />
    
                <!--
                    code - behind 方式获取本地化资源
                -->
                <TextBlock x:Name="lblMsg3" Margin="5" />
    
                <!--
                    code - behind 方式获取本地化资源
                -->
                <TextBlock x:Name="lblMsg4" Margin="5" />
    
                <!--
                    code - behind 方式获取本地化资源
                -->
                <TextBlock x:Name="lblMsg5" Margin="5" />
    
                <!--
                    绑定本地化资源
                -->
                <TextBlock x:Name="lblMsg6" Margin="5" Text="{Binding [Hello], Source={StaticResource Localized}}" />
    
            </StackPanel>
        </Grid>
    </Page>

    Localization/LocalizationDemo.xaml.cs

    /*
     * 演示本地化的基本应用
     * 
     * 
     * 注:建议使用多语言应用工具包 https://developer.microsoft.com/zh-cn/windows/develop/multilingual-app-toolkit
     */
    
    using System;
    using System.Resources;
    using Windows.ApplicationModel.Resources;
    using Windows.ApplicationModel.Resources.Core;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
    namespace Windows10.Localization
    {
        public sealed partial class LocalizationDemo : Page
        {
            public LocalizationDemo()
            {
                this.InitializeComponent(); 
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                /*
                 * ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse(); - 获取默认的 ResourceLoader(Resources.resw 中的资源)
                 * ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse("MyResources"); - 获取指定的 ResourceLoader(MyResources.resw 中的资源)
                 * ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse("ClassLibrary/MyResources"); - 获取指定类库的指定的 ResourceLoader(ClassLibrary 类库中的 MyResources.resw 中的资源)
                 * resourceLoader.GetString(), resourceLoader.GetStringForUri() - 通过资源标识,获取当前语言环境的指定的资源
                 * 
                 * GetForCurrentView() 和 GetForViewIndependentUse() 的区别如下:
                 * 1、GetForCurrentView() - 在 UI 线程上执行
                 * 2、GetForViewIndependentUse() - 在非 UI 线程上执行(注:Independent 这个词在 uwp 中就时非 UI 线程的意思,比如 Independent Animation 就是不依赖 UI 线程的)
                 */
    
                // 获取默认的 ResourceLoader(即 Resources.resw 中的资源)
                ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse();
    
                // 通过资源标识,获取当前语言环境的指定的资源(资源名:Hello)
                lblMsg1.Text = resourceLoader.GetString("Hello");
    
                // 通过资源标识,获取当前语言环境的指定的资源(资源名:HelloTextBlock.Text)
                lblMsg2.Text = resourceLoader.GetString("HelloTextBlock/Text");
    
                // 通过资源标识,获取当前语言环境的指定的资源(资源名:Hello)
                lblMsg3.Text = resourceLoader.GetStringForUri(new Uri("ms-resource:///Resources/Hello"));
    
                // 通过资源标识,获取当前语言环境的指定的资源(资源名:HelloTextBlock.Text)
                lblMsg4.Text = resourceLoader.GetStringForUri(new Uri("ms-resource:///Resources/HelloTextBlock/Text"));
    
                // 获取当前语言环境的指定的资源的另一种方式
                lblMsg5.Text = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap.GetValue("Resources/Hello", ResourceContext.GetForCurrentView()).ValueAsString;
            }
        }
    
        // 用于演示如何绑定本地化资源
        public class LocalizedStrings
        {
            public string this[string key]
            {
                get
                {
                    return ResourceLoader.GetForCurrentView().GetString(key);
                }
            }
        }
    }


    2、演示与“改变语言”相关的一些应用
    Localization/Language.xaml

    <Page
        x:Class="Windows10.Localization.Language"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Windows10.Localization"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="Transparent">
            <StackPanel Name="root" Margin="10 0 10 10">
    
                <!--
                    通过此种方式获取本地化资源时,如果在页面加载后修改了语言首选项的话是不会立即有效果的,需要重新加载页面才行(懒的写了,要看效果的话就先返回,然后再进来就好)
                -->
                <TextBlock x:Uid="HelloTextBlock" Margin="5" />
    
                <ComboBox Name="cmbLanguage" Width="800" HorizontalAlignment="Left" Margin="5" />
    
                <Button Name="btnGetEnglish" Content="获取英文资源" Margin="5" Click="btnGetEnglish_Click" />
                <Button Name="btnGetChinese" Content="获取简体中文资源" Margin="5" Click="btnGetChinese_Click" />
    
                <TextBlock Name="lblMsg" Margin="5" />
    
            </StackPanel>
        </Grid>
    </Page>

    Localization/Language.xaml.cs

    /*
     * 演示与“改变语言”相关的一些应用
     * 
     * 1、演示如何改变当前的语言环境
     * 2、演示如何监测当前语言环境发生的变化
     * 3、演示如何获取指定语言环境下的资源
     */
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Windows.ApplicationModel.Resources;
    using Windows.ApplicationModel.Resources.Core;
    using Windows.Globalization;
    using Windows.UI.Core;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    namespace Windows10.Localization
    {
        public sealed partial class Language : Page
        {
            public Language()
            {
                this.InitializeComponent();
    
                this.Loaded += Language_Loaded;
            }
    
            void Language_Loaded(object sender, RoutedEventArgs e)
            {
                // 获取当前的语言
                string currentLanguage;
                ResourceContext.GetForCurrentView().QualifierValues.TryGetValue("Language", out currentLanguage);
                lblMsg.Text = "current language: " + currentLanguage;
                lblMsg.Text += Environment.NewLine;
    
    
                // ApplicationLanguages.ManifestLanguages - 遍历 Package.appxmanifest 中的语言列表
                foreach (string strLang in ApplicationLanguages.ManifestLanguages)
                {
                    // 关于 Language 的说明详见 GlobalizationDemo.xaml
                    var lang = new Windows.Globalization.Language(strLang);
                    cmbLanguage.Items.Add(string.Format("DisplayName:{0}, NativeName:{1}, LanguageTag:{2}, Script:{3}",
                        lang.DisplayName, lang.NativeName, lang.LanguageTag, lang.Script));
                }
                cmbLanguage.SelectionChanged += cmbLanguage_SelectionChanged;
    
    
                // 获取当前语言环境的指定资源(更多用法请参见 LocalizationDemo.xaml)
                lblMsg.Text += ResourceLoader.GetForViewIndependentUse().GetString("Hello");
    
    
                // 当前语言环境发生改变时所触发的事件(通过 API 更改或者通过“电脑设置 -> 常规 -> 语言首选项”更改都会触发此事件)
                // 注:当通过 API(ApplicationLanguages.PrimaryLanguageOverride)修改语言环境时,如果监听了 MapChanged 事件的话,则有很大的几率会导致崩溃,本例就是这样,原因未知
                ResourceContext.GetForCurrentView().QualifierValues.MapChanged += async (s, m) =>
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        lblMsg.Text += Environment.NewLine;
                        lblMsg.Text += ResourceLoader.GetForViewIndependentUse().GetString("Hello");
                    });
                };
            }
    
            private void cmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                // ApplicationLanguages.PrimaryLanguageOverride - 设置或获取首选语言(BCP-47 语言标记)
                if (cmbLanguage.SelectedValue.ToString().ToLower().Contains("en-us"))
                    ApplicationLanguages.PrimaryLanguageOverride = "en-US";
                else if (cmbLanguage.SelectedValue.ToString().ToLower().Contains("zh-hans"))
                    ApplicationLanguages.PrimaryLanguageOverride = "zh-Hans-CN";
    
                StringBuilder sb = new StringBuilder();
                // ApplicationLanguages.Languages - 按语言级别排序,获取语言列表
                foreach (string item in ApplicationLanguages.Languages)
                {
                    sb.Append(item);
                    sb.Append(",");
                }
    
                lblMsg.Text += Environment.NewLine;
                lblMsg.Text += "ApplicationLanguages.Languages: " + sb.ToString();
            }
    
            private void btnGetEnglish_Click(object sender, RoutedEventArgs e)
            {
                // 指定 ResourceContext 为 en-US 语言环境
                ResourceContext resourceContext = new ResourceContext();
                resourceContext.Languages = new List<string>() { "en-US" };
    
                // 获取 en-US 语言环境下的 Resources 映射
                ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
                lblMsg.Text += Environment.NewLine;
                // 获取指定的语言环境下的指定标识的资源
                lblMsg.Text += "英语的 Hello: " + resourceMap.GetValue("Hello", resourceContext).ValueAsString;
            }
    
            private void btnGetChinese_Click(object sender, RoutedEventArgs e)
            {
                // 指定 ResourceContext 为 zh-Hans-CN 语言环境
                ResourceContext resourceContext = new ResourceContext();
                resourceContext.Languages = new List<string>() { "zh-Hans-CN" };
    
                // 获取 zh-Hans 语言环境下的 Resources 映射
                ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
                lblMsg.Text += Environment.NewLine;
                // 获取指定的语言环境下的指定标识的资源
                lblMsg.Text += "简体中文的 Hello: " + resourceMap.GetValue("Hello", resourceContext).ValueAsString;
            }
        }
    }



    OK
    [源码下载]

  • 相关阅读:
    MyBatis 3源码分析
    Java动态代理
    HashMap原理分析(JDK1.7.x之前)
    JQuery datepicker 用法
    百度一键分享功能
    JQuery BlockUI插件
    AJAX参数详解
    ECharts图标库
    KeCode对照表(键盘按键的获取)
    常见浏览器兼容问题
  • 原文地址:https://www.cnblogs.com/webabcd/p/8078431.html
Copyright © 2020-2023  润新知