MonoDroid中的本地化

5
我的应用程序使用标准的.NET RESX方法进行本地化(例如String.fr.resx,Strings.de.resx等),在Windows Phone下运行得很好。
我正在使用MonoDroid将其移植到Android,并且当我在手机上切换语言环境时,我没有看到本地化的用户界面。如果我将APK文件重命名为ZIP并打开它,则会发现在构建期间生成的区域设置DLL没有打包到APK中(即中间的\. Resources.dll文件位于bin目录下,但未打包到APK中)。
我错过了什么?我尝试将RESX文件的构建操作从“嵌入式资源”更改为“Android资源”,甚至是“Android资产”,但无济于事。
提前感谢任何帮助!
敬礼 沃伦

1
Android中的本地化是通过文件夹标题实现的。请阅读本文以了解如何进行此操作:http://developer.android.com/guide/topics/resources/localization.html - mironych
1个回答

2
我在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);
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接