我的应用使用标准的.NET RESX方法(即String.fr.resx,Strings.de.resx等)进行了本地化,在Windows Phone下可以很好地工作。
我正在使用MonoDroid移植到Android,当我在手机上切换语言环境时,看不到本地化的UI。如果我将APK文件重命名为ZIP并打开它,我会发现它没有打包在生成过程中生成的语言环境DLL(即,中间\ .Resources.dll文件位于bin目录下,但未打包到APK中) 。
我想念什么?我尝试将RESX文件上的生成操作从“嵌入式资源”更改为“ Android资源”,甚至更改为“ Android资产”,但无济于事。
在此先感谢您的帮助!
干杯
沃伦
最佳答案
我在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);
}
}
}
}