【问题标题】:Localization in MonoDroidMonoDroid 中的本地化
【发布时间】:2011-12-01 09:01:41
【问题描述】:

我的应用程序使用标准 .NET RESX 方法(即 String.fr.resx、Strings.de.resx 等)在 Windows Phone 下运行良好。

我正在使用 MonoDroid 移植到 Android,当我在手机上切换语言环境时,我没有看到本地化的 UI。如果我将 APK 文件重命名为 ZIP 并打开它,我发现它没有打包构建期间生成的语言环境 DLL(即中间 \.Resources.dll 文件位于 bin 目录下,但未打包到 APK 中) .

我错过了什么?我尝试将 RESX 文件的构建操作从“嵌入式资源”更改为“Android 资源”甚至“Android 资产”,但无济于事。

提前感谢您的帮助!

干杯 沃伦

【问题讨论】:

标签: localization xamarin.android resx


【解决方案1】:

我在 monodroid irc 频道上询问了这个问题,官方回答是“尚不支持,但我们确实有计划这样做”。

您需要将 resx 文件转换为 android xml 格式(见下文)并将它们添加到您的项目中,如下所示:http://docs.xamarin.com/android/tutorials/Android_Resources/Part_5_-_Application_Localization_and_String_Resources

在我的应用程序(游戏)中,我需要按名称查找本地化字符串。执行此操作的代码很简单,但不是很明显。我没有使用 ResourceManager,而是将其换成了 android:

class AndroidResourcesProxy : Arands.Core.IResourcesProxy
{
    Context _context;

    public AndroidResourcesProxy(Context context)
    {
        _context = context;
    }

    public string GetString(string key)
    {
        int resId = _context.Resources.GetIdentifier(key, "string", _context.PackageName);
        return _context.Resources.GetString(resId);            
    }
}

由于我不是 XSLT 专家,我制作了一个命令行程序,用于将 resx 转换为 Android 字符串 XML 文件:

/// <summary>
/// Conerts localisation resx string files into the android xml format
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        string inFile = args[0];
        XmlDocument inDoc = new XmlDocument();
        using (XmlTextReader reader = new XmlTextReader(inFile))
        {
            inDoc.Load(reader);
        }

        string outFile = Path.Combine(Path.GetDirectoryName(inFile), Path.GetFileNameWithoutExtension(inFile)) + ".xml";
        XmlDocument outDoc = new XmlDocument();
        outDoc.AppendChild(outDoc.CreateXmlDeclaration("1.0", "utf-8", null));

        XmlElement resElem = outDoc.CreateElement("resources");
        outDoc.AppendChild(resElem);

        XmlNodeList stringNodes = inDoc.SelectNodes("root/data");
        foreach (XmlNode n in stringNodes)
        {
            string key = n.Attributes["name"].Value;
            string val = n.SelectSingleNode("value").InnerText;

            XmlElement stringElem = outDoc.CreateElement("string");
            XmlAttribute nameAttrib = outDoc.CreateAttribute("name");
            nameAttrib.Value = key;
            stringElem.Attributes.Append(nameAttrib);
            stringElem.InnerText = val;

            resElem.AppendChild(stringElem);
        }

        XmlWriterSettings xws = new XmlWriterSettings();
        xws.Encoding = Encoding.UTF8;
        xws.Indent = true;
        xws.NewLineChars = "\n";

        using (StreamWriter sr = new StreamWriter(outFile))
        {
            using (XmlWriter writer = XmlWriter.Create(sr, xws))
            {
                outDoc.Save(writer);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多