I have written a multi-lingual app.
I have different values
folder for different languages like values-fa
and values-ru
. As some of these languages like Farsi and Arabic are RTL, I used a different layout design by putting some other layout .xml files in a folder layout-ldrtl
beside the main layout
folder.
I also put a file named is_right_to_left.xml
int a folder named values-ldrtl
which defines a variable like this:
<bool name="is_right_to_left">true</bool>
I have the same file in values
folder and set the variable value to false
there.
Whenever in the code I need to know, I use this line to understand if current locale is a RTL one or not:
isRightToLeft = context.getResources().getBoolean(R.bool.is_right_to_left);
I've tested the app with different device configs and everything works like charm. For example setting device language to Farsi causes the app to read the strings from values-fa
and the layouts from layout-ldrtl
, while when Arabic is set for device, values are read from values-ar
and the layouts are also read from layout-ldrtl
because both of them are RTL languages. It of-course works correctly for LTR languages too. That's what I want.
The problem occurs when I set the locale of the app programmatically. This is the code I wrote in onCreate()
method of my MainActivity
:
Locale myLocale = new Locale("fa");
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Now, the values are read correctly according to the language, but layout-ldrtl
and values-ldrtl
are never read. So, the app would load in LTR design because of wrong .xml files and wrong value of isRightToLeft
variable.
One solution I've tested is putting those .xml files in each language layout folder separately, and it works. But I hope to get a better and neater way of solving the problem.
And besides the solution, why does android act in this way?