2013-06-06 2 views
4

, 그것은 자신의 안드로이드 문서에 도시 된 바와 같이, 다중 언어 응용 프로그램에 대한 지역화 된 문자열을 만들 수 있습니다 :크로스 플랫폼 현지화

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

는 그러나, 나는 다양한 시도/catch 블록을 오류 메시지를 다시 문자열로 보내는 모델. 이상적으로는 모델과 컨트롤러 부분을 플랫폼 전체로 유지하고 싶지만 플랫폼에 특정한 Android 컨텍스트를 모델에 전달하지 않아도 메시지를 효과적으로 현지화 할 수있는 방법은 없습니다.

누구에게 어떻게 달성 할 수있는 아이디어가 있습니까?

답변

7

나는 안드로이드 대신에 .net resource files을 사용하고 있습니다. 어디서나 코드의 문자열에 액세스 할 수 있습니다.

내가 자동으로 수행 할 수없는 유일한 작업은 레이아웃의 해당 문자열을 참조하는 것입니다. 이 문제를 해결하기 위해 resx 파일을 분석하고 동일한 값을 가진 Android 리소스 파일을 만드는 빠른 유틸리티를 작성했습니다. 안드로이드 프로젝트가 빌드되기 전에 실행되어 모든 문자열이 제자리에있을 때 실행됩니다.

면책 조항 : 실제로 여러 언어로 테스트하지는 않았습니다.

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()); 
     } 
    } 
} 
+2

이 답변을 바탕으로 많은 Android 리소스를 생성하고 iOS UIColor 리소스 (더 많은 iOS)를 생성하는 OSS 프로젝트를 만들었습니다. https://github.com/ChaseFlorell/ResourceMigrator –

2

자 마린를 들어, 당신은 또한 번역에 대한 걱정없이 특유의 https://github.com/rdio/vernacular

당신은 최소한의 노력으로 코드를 작성할 수 있습니다 볼 수 있습니다. 생성 된 IL을 Vernacular에 공급하여 iOS, Windows Phone 형식의 Andorid에서 번역 가능한 문자열을 가져옵니다.