简体   繁体   English

无法在运行时更改应用程序语言 Xamarin Forms -> Android

[英]Unable to change app language at runtime on Xamarin Forms -> Android

I'm developing a crossplatform app using Xamarin Forms, primary target Android.我正在使用 Xamarin Forms 开发跨平台应用程序,主要目标 Android。 The app I'm working on refers to .resx files for languages.我正在开发的应用程序是指语言的.resx文件。 The translations works fine as the phone is set to different langauges.由于手机设置为不同的语言,因此翻译工作正常。

How can I switch languages from the app settings at runtime on the fly?如何在运行时从应用程序设置中即时切换语言?

I tried using DependencyService to call a function on Android side, but, despite returning with no error, it doesn't change the language.我尝试使用DependencyService在 Android 端调用 function ,但是,尽管返回没有错误,但它并没有改变语言。

C# project C#项目

bool test = DependencyService.Get<ILanguageService>().SetLanguage(selectedLang.StringValue);

Android Project Android项目

[assembly: Dependency(typeof(LanguageService))]
namespace MyLangApp.Droid.Service
{
    public class LanguageService:ILanguageService
    {
        public bool SetLanguage(string lang = "")
        {
            try
            {
                Locale locale = string.IsNullOrEmpty(lang) ? new Locale("en-US") : new Locale(lang);
                Locale.Default = locale;
                var config = new Android.Content.Res.Configuration();
                config.Locale = locale;
                var context = Android.App.Application.Context;
                context.Resources.UpdateConfiguration(config, context.Resources.DisplayMetrics);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

Take a look at CurrentCulture and CurrentUiCulture .看看CurrentCultureCurrentUiCulture

As far as I remember, you can set them at runtime.据我记得,您可以在运行时设置它们。

I will guess that you are using i18n with a MarkupExtension .我猜你正在使用带有MarkupExtensioni18n If so, then you can dynamically change the culture inside the Translate extension class' ctor.如果是这样,那么您可以动态更改Translate扩展类的 ctor 中的文化。

namespace ResxLocalize
{
    [ContentProperty("Text")]
    public class TranslateExtension : IMarkupExtension
    {
        private readonly CultureInfo ci;
        private const string ResourceId = "MyApp.Resources.R";

        public TranslateExtension()
        {
            if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
            {
                // SET YOUR CULTURE HERE
                ci = new CultureInfo("DESIRED CULTURE");
            }
        }

        public string Text { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            string translation = string.Empty;

            if (Text != null)
            {
                ResourceManager manager = new ResourceManager(ResourceId, typeof(TranslateExtension).GetTypeInfo().Assembly);

                translation = manager.GetString(Text, ci) ?? Text;
            }

            return translation;
        }
    }
}

Every time a string is being obtained, a new ctor is being fired and the culture is being retrieved dynamically.每次获取字符串时,都会触发一个新的 ctor 并动态检索文化。 You can change the CultureInfo inside the ctor here: ci = new CultureInfo("DESIRED CULTURE");您可以在此处更改 ctor 内的 CultureInfo: ci = new CultureInfo("DESIRED CULTURE");

You can see an example using App properties here您可以在此处查看使用 App 属性的示例

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

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