如何在Xamarin.Forms中更改日期选择器的语言环境?

时间:2019-04-24 16:50:50

标签: android xamarin.forms locale

我有一个同时支持英语和法语的Xamarin.Forms应用程序。如何在法语语言环境中显示DatePicker的日历(月,周几文本)? Imgur

我已经尝试过Xamarin.Forms

    public void SetLocale(CultureInfo ci)
    {
        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
        Console.WriteLine("CurrentCulture set: " + ci.Name);
    }

在我的DatePicker自定义渲染器中创建日期选择器对话框之前,我尝试过更改上下文

        var config = new Android.Content.Res.Configuration { Locale = Locale.CanadaFrench };
        Context.Resources.UpdateConfiguration(config, Context.Resources.DisplayMetrics);

        _dialog = new DatePickerDialog(Context, (o, e) =>
        {
            view.Date = e.Date;
            ((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
        }, year, month, day);

我也在DatePicker自定义渲染器中尝试了此操作 受保护的覆盖无效

OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
        {
            base.OnElementChanged(e);
            this.Control.TextLocale = Locale.CanadaFrench;
        }

这些都没有做任何更改区域设置的操作。

我还发现这篇文章似乎已经完成了将日历视图更改为其他区域设置的工作 Set Android DatePicker title language 但是尚不清楚它是如何实现的。

2 个答案:

答案 0 :(得分:0)

事实证明,您必须在设置中添加法语作为语言,才能以法语显示内容。

设置->一般管理->语言和输入->语言Language Settings

您只需要在DatePicker android自定义渲染器中使用这两行将日历设置为法语

this.Control.TextLocale = locale;
Resources.Configuration.SetLocale(locale);

如果标题仍为英文,请参考此帖子 Set Android DatePicker title language

答案 1 :(得分:0)

Locale locale = new Locale("fr"); 
Control.TextLocale = locale; 
Android.Content.Res.Configuration config = new Android.Content.Res.Configuration(); 
config.Locale = locale;
Locale.SetDefault(Locale.Category.Format, locale);
Resources.Configuration.SetLocale(locale);
Resources.Configuration.Locale = locale;