xamarin / Xamarin.Forms

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

[Android] Localization does not work properly with "Bundle assemblies into native code" option enabled #1595

Open ninaada opened 6 years ago

ninaada commented 6 years ago

Description

Localization does not work properly with "Bundle assemblies into native code" option enabled in release mode. Once the locale is changed, the app picks the default locale strings(English), although the language/locale was changed to, say, spanish/french.

To fully change the strings, we have to force kill the app (Remove from recent apps section), and then restart the app, only then the strings are picked correctly according to the set locale. The locale changes only once per session of the app. If we change the locale again from spanish to french, it starts picking default strings(English) again and we have to force kill the app and restart again.

However this problem doesn't occur when running in debug mode, or in release mode with "Bundle assemblies into native code" switched off. The resource strings are in Xamarin.Forms project and we are changing the locale as given in this link. Somewhat very similar to this bug . Also, the said workaround in the bug comments don't work either.

Steps to Reproduce

  1. Create Xamarin.Forms app
  2. Add localization support as shown here
  3. Enable "Bundle assemblies into native code" in Android build options
  4. Start the app and change the language

Expected Behavior

App should change the locale and display the strings of the changed locale.

Actual Behavior

App shows the default(English) locale strings, doesn't change the strings until next restart of the app.

Basic Information

PureWeen commented 6 years ago

do you have a sample project with the issue?

I tried the Xamarin localization sample project here https://github.com/xamarin/xamarin-forms-samples/tree/master/TodoLocalized

and in release mode with embedded assemblies the language on everything updated when I changed the locale on the device without having to restart the app.

bulente commented 6 years ago

It is working if you change the device locale from Andoid's settings but if you want to change the locale from the code like below, it doesn't work and fallbacks to English.

var ci = new System.Globalization.CultureInfo("es");
DependencyService.Get<ILocalize>().SetLocale(ci);
AppResources.Culture = ci;
public void SetLocale(CultureInfo ci)
{
    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;
}
ninaada commented 6 years ago

@bulente Exactly! It doesn't change in runtime like that.

MelbourneDeveloper commented 6 years ago

This is probably related to this bug https://github.com/xamarin/Xamarin.Forms/issues/2049

Other platforms like UWP, Silverlight, and WPF have a "Language" property on FrameworkElement. But, Xamarin Forms doesn't yet have such a property and therefore the page doesn't know what language to use. We've had to work around this with converters.

bulente commented 6 years ago

We've had to work around this with converters.

My workaround is: Instead of giving the user a list of languages for selection, I only list 2 selection items: Use phone's language and English. Then the samples work fine.

PureWeen commented 5 years ago

https://github.com/xamarin/xamarin-android/issues/2178

rmarinho commented 5 years ago

from #4479 @raphibolliger reports

Demo project

https://github.com/raphibolliger/XamarinLocalizeTest You can have a look at the view on the running app. Its also a short logging included during app startup which will write a translated (or not) sentence to the console.

Workaround

My workaround for this problem is to manually include builded dll's of resources in the Android project via Assets. And then load it on startup in MainActivity. This is not included in the demo project.

Steps to Reproduce

  1. Build the demo project with "Bundle assemblies into native code" enabled in release mode
  2. Start the app
  3. Verify the localized sentences
raphibolliger commented 5 years ago

@rmarinho Do you have any idea when someone can take care of this bug? Its a blocking issue for your customer project. And this comes along with a enterprise feature which is not cheap.

I have also new information about my described workaround above. After some more tests I figured out that this is also not working. So we have no workaround and still waiting that this would be fixed.

emarchal78 commented 5 years ago

Does anybody solved this issue ? We are still waiting for a new Xamarin version to correct this bug.

bobbydharrell commented 5 years ago

Any updates on this bug?

fatihgoksen commented 5 years ago

Hi Xamarin team. Do you have any update on this serious bug? We cannot implement multi-language features right now because of this bug. Also, We need a proper workaround.

rapcgutz commented 4 years ago

Any updates on this bug? This is a major blocker for the multi-language application and we need some workaround to address this issue.

matteopiccioni commented 4 years ago

Hello @PureWeen , I have the same problem With the app in debug all works in the right way If I publish the app in release, then only english is shown. nb: in android settings I cant see "Bundle assemblies into native code", anyway I publish my app into playstore using aab

My company app should go live in store in a few days...How should I solve this issue? I cant have app only in english

I noticed that if I remove
<AndroidPackageFormat>aab</AndroidPackageFormat> from 'myproject.Android.csproj' then localization works, but I can't create aab anymore

For now I solve removing AndroidPackageFormat and using 'Generate one package per selected ABI'

sasivishnu commented 4 years ago

@PureWeen Hi, any update on this? This is a major blocker for us too. We change language at runtime based on user need.

huybuidac commented 4 years ago

For anyone is facing the issue, you only need to update Xamarin.Android from 10.0 to 10.1 or up.

ref: https://github.com/xamarin/xamarin-android/issues/3596

PureWeen commented 4 years ago

@sasivishnu can you confirm what @huybuidac has indicated?

sasivishnu commented 4 years ago

@PureWeen Sorry, missed this comment altogether due to chaos situation at that time. We released our app without having multi-language support.

Later this weekend or next weekend, I will play around and confirm this. Thanks @huybuidac for information :)