c# - 如何在运行时设置 UWP 语言
问题描述
我目前正在使用 Xamarin.Forms 3.4 和 Visual Studio 2017(目前最新版本)开发跨平台应用程序。
由于该应用程序应支持可在运行时更改的多种语言,因此我目前正在寻找完成此任务的方法。我已经添加了几个资源并翻译了所有工作正常的界面元素。我通读这篇文章开始: https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/
目前,使用 UWP 应用程序时,语言会自动设置为我当前的系统设置,即德语,即使默认应用程序语言是英语。没关系。
现在,我得到了一个方法,它通过设置一些信息来配置我当前的语言,比如默认的 CultureInfo 对象。该方法如下所示:
public void UpdateAppLanguage()
{
CultureInfo ci;
Language l;
// storage is a class containing several persistent information, like
// the language selected by the user to be used
// the Local attribute states if the language is actually present on
// the current user's system
if(Storage.GetCurrentLanguage().Local == true)
{
// language is present locally, so the user wants to use that one for
// the interface
ci = new CultureInfo(Storage.GetCurrentLanguage().Code);
AppResources.Culture = ci;
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
DependencyService.Get<ILocalize>().SetLocale(ci); // set the Thread for locale-aware methods
}
else
{
// no preferences available yet, use the system's current ci
if (Device.RuntimePlatform != Device.UWP)
ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
else
ci = CultureInfo.CurrentCulture;
l = new Language{
Name = ci.EnglishName,
Code = ci.TwoLetterISOLanguageName
};
Storage.SetCurrentLanguage(l);
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
AppResources.Culture = ci; // set the RESX for resource localization
DependencyService.Get<ILocalize>().SetLocale(ci); // set the Thread for locale-aware methods
}
}
我目前正在使用一种语言集进行测试,该语言是英语。
这里是 UWP 子项目的依赖服务实现:
public class Localize : stereopoly.ILocalize
{
public void SetLocale (CultureInfo ci)
{
//Windows.Globalization.ApplicationSettings.PrimaryLanguageOverride = ci.Name;
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "EN-US";
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
}
public CultureInfo GetCurrentCultureInfo ()
{
return null;
}
}
如您所见,我还尝试将语言强制设置为 EN-US,这也不起作用。我什至在初始 Application.InitializeComponent() 方法运行之前调用了 UpdateAppLanguage() 方法,我也尝试在 OnStart() 事件方法中调用它,但都不起作用。
我希望语言更改得到应用,因为它甚至在实际应用程序启动之前就已执行,但我可以根据需要随时切换应用程序的几个页面,无论我做什么,语言都将始终是德语。对于普通的 UWP 应用程序,我找到了这个问题的几个答案,但不是针对 UWP 项目的 Xamarin.Forms 实现,这似乎是这里的问题,因为没有其他关于纯 UWP 应用程序的提示似乎有效。
你知道什么可以帮助我吗?
谢谢。
解决方案
确保您的目标至少是 UWP 16299 (FCU),因为在该版本之前,UWP 在调试模式下正确切换语言存在问题。但是,如果您在发布模式下运行,它应该可以正常工作。
此外,UWP 应用需要知道它支持给定的语言,因为这是在编译时生成的(使用<Resource Language="x-generate"/>
in Package.appxmanifest
)
为此,您可以Strings
在 UWP 项目中添加一个文件夹,然后添加多个子文件夹 - 每种支持的语言一个。最后在每个文件中添加一个Resources.resw
文件(右键单击文件夹,添加 > 新项目...,资源文件 (RESW))。结果将如下所示。
然后在每个 RESW 文件中添加一些资源。我通常输入AppName
,因为我也经常将其本地化:
在此之后,应用程序应该了解您支持给定语言并显示本地化的事实。
推荐阅读
- google-apps-script - 试图将现有脚本限制为特定工作表
- r - 通过组 ID 对 data.table 中的行进行简单扩展
- python - 按键分组并创建相应值的列表
- xpath - 从电子商务网站 google sheet importxml 刮取价格
- c++ - 为什么我的线性搜索总是返回 false,即使该项目在数组中?
- matlab - Matlab intlinprog 指标不同,求解器数据相同
- python - 为第二个数据帧中的每个观察结果从一个 pandas 数据帧中查找最近的(纬度/经度)观察值
- javascript - 旋转机械臂
- javascript - 你如何在 JavaScript 中创建一个 12 小时的时间
- r - 使用 facet_wrap() 时在 ggplot 中手动标记轴