Xamarin.Forms:如何使用独立于设备语言的本地化 [英] Xamarin.Forms : How to use localization independent of device language
问题描述
我正在使用Visual Studio 2013 CE开发一个Xamarin.Forms应用程序(可移植类库项目)。首先我关注iOS版本。
I am developing a Xamarin.Forms app (portable class library project) with Visual Studio 2013 CE. First I'm focusing the iOS version.
现在我正在考虑如何使应用程序多语言化。
我刚刚阅读了关于它的官方Xamarin文档,但我意识到这个解决方案只采用目标设备的系统语言。
Now I'm thinking about how to make the app multilingual. I just read the offical Xamarin documentation about it but i realized that this solution only takes the system language of the target device.
在便携式类库中我有一个包含三种语言的Resources文件夹:德语(默认),英语和法语。
In the portable class library I have a Resources folder with three languages: German (default), English and French.
Resource.resx
Resource.en-US.resx
Resource.fr-FR.resx
Resource.Designer.cs
现在我刚刚创建了一个静态设置类,如下所示:
Now i just created a static settings class which looks like this:
public static class Settings
{
public static Dictionary<String, CultureInfo> Languages = new Dictionary<String, CultureInfo> { { "German", new CultureInfo("de-DE") }, { "English", new CultureInfo("en-US") }, { "French", new CultureInfo("fr-FR") } };
public static CultureInfo CurrentCulture = Languages["German"];
public static CultureInfo CurrentUiCulture = Languages["German"];
public static void ChangeCurrentCultureInfo(CultureInfo cultureInfo)
{
Resource.Culture = cultureInfo;
}
}
现在我的问题是,是否有可能改变文化在应用程序中运行时单击按钮。
也许类似
Now my question is if it's possible to change the culture in the application while runtime with a button click. Maybe something like
Settings.CurrentCulture = Settings.Languages["English"];
Settings.ChangeCurrentCultureInfo(Settings.CurrentCulture);
有人能告诉我这是怎么做到的吗?
Does anyone can tell me how this can be done?
问候
推荐答案
最后我得到了Xamarin支持的答案。
我正在思考的解决方案。在PCL中,我只有静态设置类
,它存储不同的 CultureInfo
对象。另外,我定义了一种方法来更改当前文化和资源文件的语言。
Finally i got an answer of the Xamarin support.
The solution i was thinking about works. In the PCL i just have the static Settings class
which stores the different CultureInfo
objects. In addition I defined a method for changing the current culture and the language of the resources file.
public static void ChangeCurrentCultureInfo(CultureInfo cultureInfo)
{
CurrentCulture = cultureInfo;
Resource.Culture = CurrentCulture;
}
在中设置默认语言非常重要初始化主页面之前的App()
构造函数。现在我得到了一个包含所有支持语言的列表视图,其中一个调用事件调用 ChangeCurrentCultureInfo
方法。
It was important to set the default language in the App()
constructor before I initialize the main page. Now i got a listview with all supported languages with one tapped event which calls the ChangeCurrentCultureInfo
method.
尽管如此我想要感谢 Andy Hopper 提供他的解决方案!
Nevertheless i want to thank Andy Hopper for providing his solution!
问候
这篇关于Xamarin.Forms:如何使用独立于设备语言的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!