在 Xamarin Forms 中更改语言 [英] Changing language in Xamarin Forms

查看:49
本文介绍了在 Xamarin Forms 中更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说:我编写的应用程序使用 Translation 文件夹中的 LangResource.{xx-XX}.resx 文件支持多种语言.

Long story short: The app I wrote supports multiple language using LangResource.{xx-XX}.resx files in the Translation folder.

我使用

`xmlns:resources="clr-namespace:Elettric80.TP.Translations"`

ContentPage 初始声明中,以及

in the ContentPage initial declaration, and

`Text="{x:Static resources:LangResource.{string}}"` 

需要的地方.

在代码方面,我只是使用 LangResource.{string} 分配翻译后的字符串.

On code side i just assign the translated strings using LangResource.{string}.

在 MainPage 的子页面 Option 中,我提供了根据可用的 resx 文件选择语言的可能性.选择后,我将所选的语言保存在设置中并使用

In page Option , which is a child of MainPage, I offer the possibility to select a language based on the available resx files. Once selected I save the selected lang in the settings and reload the MainPage with

Application.Current.MainPage = new MainPage();

当应用程序重新启动时,我读取设置,获取所选语言并执行

When the app restarts, I read the settings, get the selected language and execute

Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);

InitializeComponent();被调用之前.

问题是 MainPage 已翻译,但所有其他页面都保留以前的语言,除非我关闭应用程序并手动重新启动.

The problem is that the MainPage is translated, but all the other pages keeps the previous language, unless I close the app, and restart it manually.

我也试过用DependencyService直接应用Android端调用的语言:

I also tried to use DependencyService to apply directly the language on Android side calling:

DependencyService.Get<ILanguageService>().SetLanguage(lang);

在c#中并将语言传递给以下方法

in c# and passing the language to the following method

public void SetLanguage(string lang)
{
    CultureInfo myCulture = new CultureInfo(lang);
    CultureInfo.DefaultThreadCurrentCulture = myCulture;

    Locale locale = new Locale(lang);
    Locale.Default = locale;
    var config = new global::Android.Content.Res.Configuration();
    config.Locale = locale;
    config.SetLocale(locale);
}

不过,只有当我关闭应用程序并再次打开它时,语言才会改变.有什么建议吗?

Still, the language changes only if I close the app and open it again. Any suggestion?

推荐答案

我用DependencyService解决了这个问题,调用

I solved this problem with DependencyService, calling

DependencyService.Get<ILanguageService>().SetLanguage(lang);

传递已选择的语言(格式为en"/es"/it"/etc...).

passing the language that has been selected (in the format "en"/"es"/"it"/etc...).

接口在 ILanguageService.cs 中声明为:

The interface is declared in ILanguageService.cs as:

namespace {namespace}
{
    public interface ILanguageService
    {
        void SetLanguage(string lang);
    }
}

Android端如下:

The Android side is as follows:

[assembly: Dependency(typeof({namespace}.Droid.LanguageService))]

namespace {namespace}.Droid
{
    public class LanguageService : ILanguageService
    {
        public void SetLanguage(string lang)
        {
            if (!string.IsNullOrEmpty(lang))
            {
                // Get application context
                Context context = Android.App.Application.Context;

                // Set application locale by selected language
                Locale.Default = new Locale(lang);
                context.Resources.Configuration.Locale = Locale.Default;
                context.ApplicationContext.CreateConfigurationContext(context.Resources.Configuration);
                context.Resources.DisplayMetrics.SetTo(context.Resources.DisplayMetrics);

                // Relaunch MainActivity
                Intent intent = new Intent(context, new MainActivity().Class);
                intent.AddFlags(ActivityFlags.NewTask);
                context.StartActivity(intent);
            }
        }
    }
}

和之前一样,我以所选语言设置应用程序,从设置中读取它并使用

As before I set the app in the selected language reading it from the settings and applying it with

Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);

这篇关于在 Xamarin Forms 中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆