简体   繁体   English

在 Xamarin Forms 中更改语言

[英]Changing language in Xamarin Forms

Long story short: The app I wrote supports multiple language using LangResource.{xx-XX}.resx files in the Translation folder.长话短说:我编写的应用程序使用Translation文件夹中的LangResource.{xx-XX}.resx文件支持多种语言。

I apply the strings directly in xaml using我使用 xaml 直接应用字符串

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

in the ContentPage initial declaration, andContentPage初始声明中,以及

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

where needed.需要的地方。

On code side i just assign the translated strings using LangResource.{string} .在代码方面,我只是使用LangResource.{string}分配翻译后的字符串。

In page Option, which is a child of MainPage, I offer the possibility to select a language based on the available resx files.在 MainPage 的子页面 Option 中,我提供了 select 一种基于可用resx文件的语言的可能性。 Once selected I save the selected lang in the settings and reload the MainPage with选择后,我将所选语言保存在设置中并重新加载 MainPage

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);

before InitializeComponent();InitializeComponent(); is called.叫做。

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.问题是 MainPage 已翻译,但所有其他页面都保留以前的语言,除非我关闭应用程序并手动重新启动它。

I also tried to use DependencyService to apply directly the language on Android side calling:我还尝试使用DependencyService直接在 Android 侧调用上应用语言:

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

in c# and passing the language to the following method在 c# 中并将语言传递给以下方法

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?有什么建议吗?

I solved this problem with DependencyService , calling我用DependencyService解决了这个问题,调用

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

passing the language that has been selected (in the format "en"/"es"/"it"/etc...).传递已选择的语言(格式为“en”/“es”/“it”/etc...)。

The interface is declared in ILanguageService.cs as:该接口在 ILanguageService.cs 中声明为:

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

The Android side is as follows: Android侧如下:

[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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM