是否可以在Xamarin iOS / Android中使用RESX文件定义的PCL来支持多语言?

时间:2016-06-30 05:16:20

标签: c# xamarin multilingual resx

我正在开发Xamarin iOS / Android,并且还拥有便携式类库的PCL项目。 我需要的是在Xamarin iOS / Android中使用PCL项目的RESX文件。 我的项目不是Xamarin.Forms,所以这个链接对我不起作用。 [使用RESX资源文件本地化Xamarin.Forms应用程序] [1]

我不想在iOS和Android上使用分隔文件来支持多语言。

我的想法如下:

1)在PCL项目中为英文和中文创建两个RESX文件。

AppResources.resx

AppResources.zh-CN.resx

2)从iOS和Android项目加载RESX文件。

3)在我的应用程序中切换语言时,请更改UI语言。

但我找不到确切的方法。 如果你能帮助我,我会很高兴。

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。 也许这篇文章对我有帮助。 ios localizing .net

我在PCL项目中创建了2个resx文件。 并将其命名如下。

LackPack.resx

LangPack_zh_CN.resx

我在PCL中定义了一个静态函数

    public static string GetString (I18N sID)
    {
        string sResource = STRINGS_ROOT + "_" + currLocale;
        Type type = Type.GetType (sResource);
        if (type == null) {
            if (currLocale.Length > 2) {
                sResource = STRINGS_ROOT + "_" + currLocale.Substring (0, 2); // Use first two letters of region code
                type = Type.GetType (sResource);
            }
        }
        if (type == null) {
            sResource = STRINGS_ROOT;
            type = Type.GetType (sResource);
            if (type == null) {
                System.Diagnostics.Debug.WriteLine ("No strings resource file when looking for " + sID + " in " + currLocale);
                return null; // This shouldn't ever happen in theory
            }
        }
        ResourceManager resman = new ResourceManager (type);
        return resman.GetString (sID.ToString());
    }

我们可以在iOS / Android上使用它。

Localise.GetString (I18N.TitleHistory);
public enum I18N
{
    BtnOk,
    BtnContinue,
    BtnLogin,
}