[Maui] 造轮子——前言、本地化

前言

新的一年开始了,公司提了新的要求:

  1. 国产化
  2. 国际化

开发部的应对是:从wpf转换Maui:

  1. 安卓版也是国产化
  2. 国际化就是本地化,弄个多国语言的事情

话说华为的鸿蒙,为了国产化,在华为P40手机上运行了Maui的缺省项目,天真的以为鸿蒙支持Maui Android。于是去买了个华为的平板,然后悲剧了。亲测:P40手机的鸿蒙4.2支持,新平板的5.0、6.0不支持。要不要为了这个牛绳去买头牛,学一般华为的那什么javascript的超集typescript的超集,叫什么来着?

谁能推荐个安卓平板?

华为的事情是个庭外话,题内化就是开始新一轮的造轮子。

你说,轮子到处都是,非得自己造?

是的,有毛病: 不相信别人,别人走过的路,自己也要走,除非自己不会走。

废话到此为止。

本地化

微软本地化文档,报了个到。

一、准备工作

  1. 新建项目两个,分别为LunZi和Demo;删除iOS、MacCatalyst相关的东西。(话说微软就不能让我选择一下要支持的操作系统吗?非得让我手动删除?)
  2. LunZi项目新建文件夹Resources,并添加三个资源文件(Resource.resx,Resource.en.resx,Resource.zh-Hans.resx)
  3. 修改 Demo 项目的 Windows 的 Package.appxmanifest文件,将
<Resources>
    <Resource Language="x-generate"/>
</Resources>

替换为

	<Resources>
		<Resource Language="en-US"/>
		<Resource Language="zh-CN"/>
	</Resources>

表示本Demo支持中英文。

二、LunZi添加资源

  1. 双击 Resource.resx文件
  2. 单击 +
  3. 输入
  4. 添加后编辑
  5. 将资源的属性从internal改为public
    如此循环往复,便是添加资源。

备注一下:回头弄个程序来添加资源。

三、Demo使用资源

  1. 添加项目引用,引用LunZi
  2. 修改MainPage.xaml
    添加命名空间
    xmlns:lunzi="clr-namespace:LunZi.Resouces;assembly=LunZi"

使用资源

        <Label Text="{x:Static lunzi:Resource.Test}"/>

整个文件变成

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:lunzi="clr-namespace:LunZi.Resouces;assembly=LunZi"
             x:Class="Demo.MainPage">
    <VerticalStackLayout>
        <Label Text="{x:Static lunzi:Resource.Test}"/>
    </VerticalStackLayout>
</ContentPage>

  1. 运行
    图片[1]-[Maui] 造轮子——前言、本地化 - 拾光赋-拾光赋
    看样子,使用了本地是中国

四、中英文动态切换

  1. 修改页面
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:lunzi="clr-namespace:LunZi.Resouces;assembly=LunZi"
             x:Class="Demo.MainPage">
    <VerticalStackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="10">
        <Label x:Name="LabelTest"/>
        <HorizontalStackLayout Spacing="10">
            <Button Text="中文" Clicked="BtnCn_Clicked"/>
            <Button Text="English" Clicked="BtnEn_Clicked"/>
        </HorizontalStackLayout>
    </VerticalStackLayout>
</ContentPage>
  1. 修改代码
using System.Globalization;

namespace Demo;

public partial class MainPage : ContentPage
{

    public MainPage()
    {
        InitializeComponent();
        this.LabelTest.Text = LunZi.Resouces.Resource.Test;
    }

    private void BtnEn_Clicked(object sender, EventArgs e)
    {
        SetCulture("en-US");
        this.LabelTest.Text = LunZi.Resouces.Resource.Test;
    }
    private void BtnCn_Clicked(object sender, EventArgs e)
    {
        SetCulture("zh-Hans");
        this.LabelTest.Text = LunZi.Resouces.Resource.Test;
    }

    private void SetCulture(string cultureCode)
    {
        var culture = new CultureInfo(cultureCode);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
    }
}

  1. 运行
    图片[2]-[Maui] 造轮子——前言、本地化 - 拾光赋-拾光赋
    单击 English

好了,就这样了。

原文链接:[Maui] 造轮子——前言、本地化

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容