11

TL;DR

I have an app that supports a number of languages. One activity isn't being translated despite using all the usual localization methods. It is the only Activity in the app that fully utilizes DataBinding.

Full explanation

I have an Activity that uses DataBinding. So basically, entire XML layout for that Activity is wrapped inside the <layout> tag. This is how I inflate the view and get the binding:

SomeActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.some_activity);

There is a number of TextViews in the XML have text set in this way:

... android:text="@string/<name of a string>" ...

We provide user with predefined language options and set app locale using

Locale.setDefault(new Locale("<language name>"));

In English locale, everything works perfectly.

When the app is in a different locale thought, strings in the activity in question still show up in English - even though those strings have been translated to the appropriate language, and are located in the same string files where the rest of the app grabs the strings from. Until that activity is started, app is fine - after the activity is started and I back out of it, app reverts to English locale until its killed and restarted. If that Activity is never opened, app behaves as expected.

Does anyone have any idea what might be causing this? Thanks.

C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47
  • 1
    My understanding is that the app will retrieve the Locale from the device, no need for programmatically interfere with it. So my guess would be you're messing this up with Locale.setDefault(...), but I might very well be wrong. – fweigl Apr 21 '17 at 22:16
  • @Ascorbin well regardless of device locale, we'd still like to offer users an option to have the app in whichever language they prefer, as long as we support it. Please note that the rest of the app is working fine. – C0D3LIC1OU5 Apr 21 '17 at 22:18
  • When you are not using data binding syntax, the normal rules for applying resources are used. Are you reusing the SomeActivityBinding or do you reinflate it when you change the locale as you would normally? – George Mount Apr 22 '17 at 02:27
  • @GeorgeMount Good point but I am not re-using that binding - it's re-inflated every time `onCreate` is called. – C0D3LIC1OU5 Apr 24 '17 at 14:44

1 Answers1

-4

Figured out what was happening. It has nothing to do with ViewBinding. That activity starts in the landscape mode - which resets the app locale for some reason. This answer helped me fix it.

Community
  • 1
  • 1
C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47