xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

[Bug] [Xamarin.Forms] Always get default language from AppResources.resx in release config #15747

Closed qq1688 closed 1 year ago

qq1688 commented 1 year ago

Description:

I added multi language support for my APP, when i set the "Active config" value to "Debug|Any CPU" in solution properties, then build the test APP in my device, the APP can change the language successful, but when i change the value to "Release|Any CPU", rebuild and make a new apk, then install the APP to my device, the APP can not change the language(eg. change to TH), it always show english which i defined in AppResources.resx

Steps to Reproduce

  1. Set the "Active config" value to "Debug|Any CPU" in solution properties, use debug mode use a USB line build the APP in my phone, can successful change the APP language to en or th.
  2. Set the "Active config" value to "Release|Any CPU" in solution properties, rebuild, archive all, distribute the APK file, then install the APK to my phone, change the language to TH, but nothing change, still show english.

Try the solution in post: https://github.com/xamarin/xamarin-android/issues/4664,but not working...

The logic to change the language

private async void SelLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedLang = (Language)SelLang.SelectedItem;
            try
            {
                if (selectedLang != null)
                {
                    var culture = new CultureInfo(selectedLang.LangShortName);
                    Thread.CurrentThread.CurrentUICulture = culture;
                    Thread.CurrentThread.CurrentCulture = culture;
                    AppResources.Culture = culture;
                    await DisplayAlert("", AppResources.ChangeSuccess, "OK");
                }
            }
            catch (Exception)
            {
            }
        }

11 22 33

Expected Behavior

Can change language in release mode included the english(AppResources.resx) and TH(AppResources.th.resx)

Actual Behavior

Can only show language from AppResources.resx

Basic Information

Microsoft Visual Studio Professional 2019 Version 16.11.21 VisualStudio.16.Release/16.11.21+33027.164 Microsoft .NET Framework Version 4.8.09032

Xamarin 16.11.000.197 (d16-11@6934992) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.11.0.47 (remotes/origin/d16-11@e0d612363) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 12.0.0.3 (d16-11/f0e3c2d) Xamarin.Android Reference Assemblies and MSBuild support. Mono: c633fe9 Java.Interop: xamarin/java.interop/d16-11@476bb5b ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.35.4@85460d3 Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@87af37b

qq1688 commented 1 year ago

After hundreds of tests, I solved the issue by myself...

Two things i made wrong 1, AppResources.th.resx this file name format should be AppResources.th-TH.resx 2, new CultureInfo(parm) parm value should be the name of culture instead of TwoLetterISOLanguageName

var curDeviceLngName = CultureInfo.CurrentUICulture.Name;
var culture = new CultureInfo(curDeviceLngName);
qq1688 commented 1 year ago

After hundreds of tests, I solved the issue by myself...

Two things i made wrong 1, AppResources.th.resx this file name format should be AppResources.th-TH.resx 2, new CultureInfo(parm) parm value should be the name of culture instead of TwoLetterISOLanguageName

var curDeviceLngName = CultureInfo.CurrentUICulture.Name;
var culture = new CultureInfo(curDeviceLngName);