我正在尝试使用.resx文件在xamarin.mac中进行本地化
在AppDelegate里面,我改变了线程的当前文化:
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ru");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo ("ru");
我的应用程序中还有两个资源文件:
但是字符串始终显示在默认资源文件中…任何解决方案?
我也使用本地语言更改,这里是整个AppDelegate构造函数:
public AppDelegate ()
{
string [] lang = { "ru", "en" };
NSUserDefaults.StandardUserDefaults.SetValueForKey (NSArray.FromObjects (lang), (Foundation.NSString)"AppleLanguages");
NSUserDefaults.StandardUserDefaults.Synchronize ();
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ru");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo ("ru");
Resources.Culture = GetCurrentCultureInfo ();
}
顺便说一句,xamarin工作室6.1.1(版本15)不允许我添加resource.ru.resx如果我在我的项目中有resource.ru,有点错误!
我创建了resource-ru.resx文件,然后重命名了它.
最佳答案 你需要在从操作系统中检索它之后设置应用程序文化,类似这样(警告,这是来自Xamarin iOS,但对于文档它的工作方式相同,如果不是建议我,我将删除帖子):
//Add this somewhere
public System.Globalization.CultureInfo GetCurrentCultureInfo ()
{
var netLanguage = "en";
if (NSLocale.PreferredLanguages.Length > 0) {
var pref = NSLocale.PreferredLanguages [0];
netLanguage = pref.Replace ("_", "-"); // turns en_US into en-US
}
try
{
return new System.Globalization.CultureInfo(netLanguage);
}catch{
try{
return new System.Globalization.CultureInfo(netLanguage.Substring(0, netLanguage.IndexOf("-")));
}
catch{
return new System.Globalization.CultureInfo("en");
}
}
}
//And finally add this at the start of the app
AppResources.Culture = GetCurrentCultureInfo();