我无法找到任何有关为 MAUI 应用程序实施本地化的指导。我已经阅读了一些有关本地化 Xamarin 应用程序的信息,但即使经过广泛的网络搜索,也无法将其转发到 MAUI。
谁能给我指出我可能错过的参考资料?
试试这个 - 创建标准资源
- “添加新项目/资源文件”>MauiApp1/资源
- 设置名称“AppRes.resx”
- 创建第二语言“Apps.to.resx”
- 添加字符串
如何在XAML中使用
[...] xmlns:res="clr-namespace:MauiApp1.Resources"
<Button Text="{x:Static res:AppRes.Title}" />
use code
//get lang as "en"
string lang = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
//toggle lang
if(lang == "ru")
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");
}
else
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
}
//get translated title
//using MauiApp1.Resources;
string title = AppRes.Title
要更新只需重置应用程序
(App.Current as App).MainPage = new AppShell();
就这样
UPD1:从任何地方重新启动
void Reset()
{
(App.Current as App).MainPage.Dispatcher.Dispatch(() =>
{
// there some LoadLang method;
(App.Current as App).MainPage = new AppShell();//REQUIRE RUN MAIN THREAD
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)