c# – 使用.NET的Xamarin iOS本地化
我正在尝试在Xamarin iOS /
Android项目的便携式类库中使用.NET本地化.我按照以下步骤操作:
How to use localization in C# 并且具有如下代码: string sText = strings.enter_movie_name; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr"); sText = strings.enter_movie_name; lblEnterMovieName.Text = sText; 我也尝试过: ResourceManager resman = new ResourceManager(typeof(MyApplication.strings)); string sText = resman.GetString("enter_movie_name",new CultureInfo("fr")); 我创建了strings.resx,其中enter_movie_name等于“输入电影名称:”,strings.fr.resx等于“Entre la movie name:” 但是,sText始终以“输入电影名称:”结尾.我似乎无法获得“Entre la movie name:”版本. 我也在Cross-platform Localization看过这个帖子但是无法解决这个问题.我也不想在Localization on Xamarin.iOS使用iOS特定版本,因为我希望能够从独立于平台的库中获取我的字符串. 谁能指出我哪里出错? 解决方法
我创造了一个丑陋的解决方案,但它的确有效.我所做的是在我的可移植类库(PCL)中创建了一个名为“strings”的文件夹,并在其中创建了名为的文件:
> strings.resx 我已将所有这些设置为嵌入式资源,并使用自定义工具作为ResXFileCodeGenerator.这意味着我为每种语言都有一个单独的资源DLL. 在iOS中,我可以通过调用以下方式获取区域设置: string sLocale = NSLocale.CurrentLocale.LocaleIdentifier; 我猜有一个使用Locale的Android等价物,但我不知道它是什么. 这给了我一个像“ja_JP”或“fr_FR”或“en_GB”的字符串(注意它们是下划线,而不是破折号).然后我使用我创建的以下静态类来检索适当的资源管理器并从中获取字符串. 如果给定一个区域设置aa_BB,它首先查找strings_aa_BB,然后查找strings_aa,然后查找字符串. public static class Localise { private const string STRINGS_ROOT = "MyPCL.strings.strings"; public static string GetString(string sID,string sLocale) { string sResource = STRINGS_ROOT + "_" + sLocale; Type type = Type.GetType(sResource); if (type == null) { if (sLocale.Length > 2) { sResource = STRINGS_ROOT + "_" + sLocale.Substring(0,2); // Use first two letters of region code type = Type.GetType(sResource); } } if (type == null) { sResource = STRINGS_ROOT; type = Type.GetType(sResource); if (type == null) { System.Diagnostics.Debug.WriteLine("No strings resource file when looking for " + sID + " in " + sLocale); return null; // This shouldn't ever happen in theory } } ResourceManager resman = new ResourceManager(type); return resman.GetString(sID); } } 如何使用它(参考上面的代码)的一个例子是: string sText = Localise.GetString("enter_movie_name",sLocale); lblEnterMovieName.Text = sText; 这方面的一个重要缺点是,所有视图都需要以编程方式设置文本,但确实具有翻译可以一次完成然后在许多平台上重复使用的好处.它们在自己的DLL中也与主代码分开,因此可以根据需要单独重新编译. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |