我正在关注这个
guide来本地化我的应用程序.我不想获得系统语言,因此我不使用ILocalize接口和依赖服务.我有这3个用于英语,西班牙语和法语的resx文件:
> AppResources.es.resx
> AppResources.fr.resx
> AppResources.resx(英文)
这是我使用指南中的TranslateExtension类的TestPage.xaml代码:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:t="clr-namespace:LanguageTest.Resources;assembly=LanguageTest.Resources" x:Class="LanguageTest.TestPage"> <Label Text="{t:Translate TestText}" VerticalOptions="Center" HorizontalOptions="Center" /> </ContentPage>
使用此代码,我启动了语言设置:
public App () { // I can change among "en","es" and "fr" AppResources.Culture = new CultureInfo("es"); this.MainPage = new NavigationPage(new TestPage()); }
在这种情况下,文本以西班牙语显示.但是我希望在页面显示后更改语言(例如,使用LanguageSettingsPage,用户可以在应用程序运行时更改语言).
public App () { AppResources.Culture = new CultureInfo("es"); this.MainPage = new NavigationPage(new TestPage()); AppResources.Culture = new CultureInfo("fr"); // At this point the texts are diplayed in Spanish,not in french }
如何刷新de page以显示法语文本而无需再次执行新的TestPage()?
解决方法
这不是Resx的特定解决方案,但它可以让您根据用户需求加载任何语言.无需手动设置字符串,因为绑定将完成工作.看看
I18NPortable和样品.它很容易启动和运行.
结合样本:
<Button Text="{Binding Strings[key]}" />
要动态更改语言(将更新所有绑定):
I18N.Current.Locale = "fr";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。