使用xamarin进行移动跨平台本地化,仅限iOS使用

我在Xamarin有一个针对Android,iOS和Windows手机的项目。 我用核心(PCL库)在不同平台之间共享公共代码。 我在我的核心库中添加了资源文件(.net资源).Resx,并在我的一个ViewModel中读取了我在代码片段后面使用的特定于文化的字符串:

public string GetString() { // CommonResources is the name of my resource file ResourceManager resManager = new ResourceManager(typeof(CommonResources)); return resManager.GetString("LoginLabel",CultureInfo.CurrentCulture); } 

“LoginLabel”是我的资源键,其值为“登录”(英文)和荷兰语中的“inloggen”。

我在我的PCL项目中为英语和荷兰语创建了两个资源文件CommonResources。 CommonResources.resx
CommonResources.nl-NL.resx

在android,iOS和windows手机中,我将文化设置如下:

 Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL"); Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL"); 

这适用于Android和Windows手机。

但对iOS来说它不起作用。 它总是从英文文件返回资源字符串。 文化已正确设置,并以调试模式显示。 但不知何故,它无法从荷兰资源加载资源字符串。

所以问题是,可以使用PCL为所有平台本地化字符串(.Net方式)吗? 有谁有任何想法? 提前致谢。

为了在我们的Xamarin项目上进行本地化,我使用了Microsoft和T4模板中的Multilingual App Toolkit( 详细说明 )将工具包的输出转换为Android和iOS的可用格式。

本教程概述了该过程,它基于此代码项目。

或者:

  • 你实际上并没有在iOS上设置CurrentCulture。
  • 您在线程A上设置CurrentCulture,并在线程B上检索它。

试试这个:

 public string GetString() { Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL"); Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL"); // CommonResources is the name of my resource file ResourceManager resManager = new ResourceManager(typeof(CommonResources)); return resManager.GetString("LoginLabel",CultureInfo.CurrentCulture); } 

你在真实的设备上测试吗? 在模拟器中,不可能改变文化。 我的语言文件也有类似的问题。