我如何强制本地化文化在 Xamarin 中的整个应用程序中使用 en-US

How can I force Localization Culture to en-US for whole applicaiton in Xamarin

如何强制 Xamain - Android / iOS美国英语 文化中工作,无论用户如何环境。 我在我的应用程序中面临的问题是,该应用程序仅支持 US/UK 英语,但是如果用户将语言偏好更改为西班牙语、德语等。数字日期等格式将会改变。

例如, 2.35 将在西班牙语、德语中表示为 2,35

因此,如果用户尝试使用类似语言的应用程序,应用程序将无法运行或崩溃。当我尝试 Convert.ToDouble("2,35"); 或类似的情况时会发生崩溃。

所以我的疑问是,

在 Xamarin 中是否可以强制将区域性设置为 en-US。可能在一个地方,否则我需要在我执行转换的所有地方更改它。

请帮忙。

您可以通过以下方式设置默认文化 属性:

CultureInfo.DefaultThreadCurrentCulture

但这在 Android 中不起作用。因此,对于 Android,您需要在每次恢复 activity 时设置文化。您可以添加基数 activity,例如:

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 Class 中设置文化对我有用。

using System.Globalization;
using System.Threading;


private void SetCultureToUSEnglish()
{
    CultureInfo englishUSCulture = new CultureInfo("en-US");
    CultureInfo.DefaultThreadCurrentCulture = englishUSCulture;
}

我尝试了 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);

还创建了基地activity

在这里找到答案:

changing cultureinfo on android using xamarin and c#

您必须在您的应用中设置当前文化。本地化效果非常好。我是这样做的:

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

如果您使用带有翻译扩展的 Localize class 并且 lang 资源都已设置,您可以在运行时调用每个平台项目中可用的 setLocale 方法来自 PCL 像这样使用界面 ILocalize:

CultureInfo ci=new CultureInfo("en-US");
Xamarin.Forms.DependencyService.Get<ILocalize>().SetLocale(ci);                      
AppResources.Culture = ci;

我的解决方案是结合使用其他答案!

基本上,我没有发现需要创建新的 Activity 并按照建议扩展它。相反,我将以下覆盖添加到 MainActivity.cs:

  protected override void OnResume()
        {
            base.OnResume();

            try
            {
                var userSelectedCulture = new CultureInfo("en-GB");

                Thread.CurrentThread.CurrentCulture = userSelectedCulture;
            }
            catch (Exception exception)
            {

                Console.WriteLine(exception.Message);
            }
        }

然后,在 OnCreate 中,我添加了以下内容:

  try
            {
                string cultureName = "en-GB";
                var locale = new Java.Util.Locale(cultureName);
                Java.Util.Locale.Default = locale;

                var configg = new Android.Content.Res.Configuration { Locale = locale };
                BaseContext.Resources.UpdateConfiguration(configg, BaseContext.Resources.DisplayMetrics);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

            }

我通过将语言更改为法语并显示一个简单的 DateTime 来在模拟器中测试它,以确定今天是显示为 samedi 还是星期六,它显示的是星期六!

我正在开发一个 Xamarin.Forms 应用程序,我有同样的要求,我最终使用 MultilingualPlugin 作为 Xamarin.Forms