【问题标题】:Is it possible to use RESX files defined PCL in Xamarin iOS/Android for multi-language support?是否可以在 Xamarin iOS/Android 中使用定义 PCL 的 RESX 文件来支持多语言?
【发布时间】:2016-11-02 00:57:10
【问题描述】:

我正在开发 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 语言。

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

【问题讨论】:

标签: c# xamarin multilingual resx


【解决方案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,
}

【讨论】:

猜你喜欢
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多