Dav*_*ave 6 c# android localization ios xamarin
我正在尝试在Xamarin iOS/Android项目的便携式类库中使用.NET本地化.我按照以下步骤操作:
并且具有如下代码:
string sText = strings.enter_movie_name;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
sText = strings.enter_movie_name;
lblEnterMovieName.Text = sText;
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
ResourceManager resman = new ResourceManager(typeof(MyApplication.strings));
string sText = resman.GetString("enter_movie_name", new CultureInfo("fr"));
Run Code Online (Sandbox Code Playgroud)
我创建了strings.resx,其中enter_movie_name等于"输入电影名称:",strings.fr.resx等于"Entre la movie name:"
但是,sText始终以"输入电影名称:"结尾.我似乎无法获得"Entre la movie name:"版本.
我也看到了跨平台本地化的帖子,但无法解决这个问题.我也不想在Xamarin.iOS上的Localization上使用iOS特定版本,因为我希望能够从独立于平台的库中获取我的字符串.
谁能指出我哪里出错?
我创造了一个丑陋的解决方案,但它的确有效.我所做的是在我的可移植类库(PCL)中创建了一个名为"strings"的文件夹,并在其中创建了名为的文件:
我已将所有这些设置为嵌入式资源,并使用自定义工具作为ResXFileCodeGenerator.这意味着我为每种语言都有一个单独的资源DLL.
在iOS中,我可以通过调用以下方式获取区域设置:
string sLocale = NSLocale.CurrentLocale.LocaleIdentifier;
Run Code Online (Sandbox Code Playgroud)
我猜有一个使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用它(参考上面的代码)的一个例子是:
string sText = Localise.GetString("enter_movie_name", sLocale);
lblEnterMovieName.Text = sText;
Run Code Online (Sandbox Code Playgroud)
这方面的一个重要缺点是,所有视图都需要以编程方式设置文本,但确实具有翻译可以一次完成然后在许多平台上重复使用的好处.它们在自己的DLL中也与主代码分开,因此可以根据需要单独重新编译.
| 归档时间: |
|
| 查看次数: |
5587 次 |
| 最近记录: |