use*_*034 5 localization resx xamarin.android
我的应用程序使用标准.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);
}
}
Run Code Online (Sandbox Code Playgroud)
因为我不是 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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
919 次 |
| 最近记录: |