如何在Xamarin中强制本地化文化到en-US进行整个应用
如何强制Xamain – Android / iOS在美国英语文化中工作,无论用户设置如何。 我在我的应用程序中遇到的问题是,该应用程序仅支持美国/英国英语,但如果用户将语言首选项更改为西class牙语,德语等,则数字日期等格式将更改。
例如, 2.35将代表西class牙语,德语代表2,35 。
因此,如果用户尝试使用具有类似语言的应用程序,则应用程序将错过行为或崩溃。 在我尝试Convert.ToDouble("2,35");
情况下会发生崩溃Convert.ToDouble("2,35");
或类似的。
所以我怀疑是,
在Xamarin中是否有可能将文化强制设置为en-US 。 可能在一个地方,否则我需要更改我执行转换的所有地方。
请帮忙。
您必须在应用中设置当前文化。 本地化工作非常好。 这就是我做的方式:
public void SetLocale(CultureInfo ci) { Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; }
在这里,您可以从设备获得文化:
public CultureInfo GetCurrentCultureInfo() { var netLanguage = "en"; if (NSLocale.PreferredLanguages.Length > 0) { var pref = NSLocale.PreferredLanguages[0]; netLanguage = iOSToDotnetLanguage(pref); } // this gets called a lot - try/catch can be expensive so consider caching or something CultureInfo ci = null; try { ci = new CultureInfo(netLanguage); } catch (CultureNotFoundException e1) { } } private string iOSToDotnetLanguage(string iOSLanguage) { // Testing special cases.. }
如果您使用PCL项目,请使用抽象。 在PCL中使用接口及其在本机项目中的实现。
你可以在这里看到更多: https : //docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/localization
我尝试了Joehl方法,但它对我不起作用。 我用这种方法
string cultureName = "es-US"; var locale = new Java.Util.Locale(cultureName); Java.Util.Locale.Default = locale; var config = new Android.Content.Res.Configuration { Locale = locale }; BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics);
还创建了一个基本活动
在这里找到答案:
使用xamarin和c#在android上更改cultureinfo
您可以使用以下属性设置默认文化:
CultureInfo.DefaultThreadCurrentCulture
但这不适用于Android。 因此,对于Android,您需要在每次活动恢复时设置文化。 您可以添加以下基本活动:
internal class MyBaseActivity : Activity { protected override void OnResume () { base.OnResume (); // Here you would read it from where ever. var userSelectedCulture = new CultureInfo ("fr-FR"); Thread.CurrentThread.CurrentCulture = userSelectedCulture; } }
在xamarin论坛中找到: https ://forums.xamarin.com/discussion/9764/how-to-set-a-global-cultureinfo-for-an-app
我正在使用Xamarin Forms
App。
在App
类中设置文化为我做了诀窍。
using System.Globalization; using System.Threading; private void SetCultureToUSEnglish() { CultureInfo englishUSCulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentCulture = englishUSCulture; }
如果您正在使用带有translate扩展和lang资源的Localize
类,则可以使用ILocalize
接口在PCL的每个平台项目中在运行时调用setLocale
方法,如下所示:
CultureInfo ci=new CultureInfo("en-US"); Xamarin.Forms.DependencyService.Get().SetLocale(ci); AppResources.Culture = ci;
- 具有可触发初始化的C#Singleton模式
- NetworkInterface.GetIPv4Statistics()。BytesReceived – 它返回什么?
- 本地化和DataAnnotations。 GlobalResourceProxyGenerator和PublicResxFileCodeGenerator
- 如何在Datagrid WPF中添加复选框c#
- 没有名为“ASP.NET 4.6.81.0”的EventLog源。 此模块需要.NET Framework 2.0
- 如何在C#中在运行时向类添加属性?
- Assembly.CreateInstance和Activator.CreateInstance之间的区别?
- 如何在ASP.NET标识中创建不同的用户类型?
- C#中的只读列表