Cross-platform localization

With Xamarin Android, you can create localized strings for multilingual applications, as shown in their Android documentation:

http://docs.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_5_-_application_localization_and_string_resources

However, I have different try / catch blocks in my model that send error messages as strings. Ideally, I would like part of my solution to be completely cross-platform, and I see no way to efficiently localize messages without passing a specific Android Context platform to the model.

Does anyone have any ideas on how this can be achieved?

+4
source share
3 answers

I use .net resource files instead of Android. They give me access to lines of code wherever it is.

The only thing I can not do automatically is the links to these lines from the layouts. To deal with this, I wrote a brief utility that parses the resx file and creates an Android resource file with the same values. It starts before the creation of the Android project, so all the lines are in place when it does.

Disclaimer: I have not tested this in multiple languages ​​yet.

This is the utility code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace StringThing { class Program { static void Main(string[] args) { string sourceFile = args[0]; string targetFile = args[1]; Dictionary<string, string> strings = LoadDotNetStrings(sourceFile); WriteToTarget(targetFile, strings); } static Dictionary<string, string> LoadDotNetStrings(string file) { var result = new Dictionary<string, string>(); XmlDocument doc = new XmlDocument(); doc.Load(file); XmlNodeList nodes = doc.SelectNodes("//data"); foreach (XmlNode node in nodes) { string name = node.Attributes["name"].Value; string value = node.ChildNodes[1].InnerText; result.Add(name, value); } return result; } static void WriteToTarget(string targetFile, Dictionary<string, string> strings) { StringBuilder bob = new StringBuilder(); bob.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); bob.AppendLine("<resources>"); foreach (string key in strings.Keys) { bob.Append(" "); bob.AppendLine(string.Format("<string name=\"{0}\">{1}</string>", key, strings[key])); } bob.AppendLine("</resources>"); System.IO.File.WriteAllText(targetFile, bob.ToString()); } } } 
+7
source

For Xamarin, you can also look at people's https://github.com/rdio/vernacular

You can write code with minimal effort without worrying about translation. Download the generated IL in Vernacular to get translatable strings in iOS, Andorid, Windows Phone formats.

+2
source

I created a small ugly solution in localizing Xamarin iOS using .NET , which you might find useful.

+1
source

All Articles