本文介绍了使用 xamarin 的移动跨平台本地化和仅适用于 iOS 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project in Xamarin which targets Android, iOS and windows phone. I used core (PCL library) to share common code between different platform. I added Resource files (.net resource) .Resx in my core library and to read the culture specific string i used following code snippet in one of my 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"(荷兰语).

"LoginLabel" is my resource key and its value is "Sign in" (in english) and "inloggen" in dutch.

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

I created two resource files CommonResources for English and dutch in my PCL project.CommonResources.resx
CommonResources.nl-NL.resx

在 android、iOS 和 windows phone 中,我设置的文化如下:

in android, iOS and windows phone, I set culture as follow:

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

这适用于 Android 和 Windows 手机.

This works fine for Android and windows phone.

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

But for iOS it does not work. It always return resource string from English file. The culture is properly set and it display in debug mode. but somehow it is not able to load the resource string from the dutch resource.

那么问题是,是否可以在所有平台上使用 PCL 本地化字符串(.Net 方式)?任何人有任何想法?提前致谢.

So the question is, it is possible to localize string(.Net way) using PCL for all platform?anyone has any idea?Thanks in advance.

推荐答案

对于我们 Xamarin 项目的本地化,我使用了多语言应用工具包 (此处详述)来自 Microsoft 和 T4 模板,用于将工具包的输出转换为适用于 Android 和 iOS 的格式.

For localization on our Xamarin projects I've used the Multilingual App Toolkit (detailed here) from Microsoft and T4 templates to transform the output from the toolkit to useable formats for Android and iOS.

教程对流程进行了精彩的概述,它基于 这个 代码项目.

This tutorial has a fantastic overview of the process and it's based on this code project.

这篇关于使用 xamarin 的移动跨平台本地化和仅适用于 iOS 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 12:22