Reputation: 12989
In my Activity I have the following line to find the id of a layout I need to inflate:
int layoutId = res.getIdentifier("name_of_layout_that_does_exist", "layout", context.getPackageName());
This works fine, returning the id of the relevant layout, and everything after works fine... except when the language for the app is set to Turkish! (tr
)
I set the language to be used for the Activity by way of the following override in the Activity definition:
@Override
protected void attachBaseContext(Context baseContext) {
String appLocale = MyFunctions.getAppLocale(baseContext); // this is "tr"
super.attachBaseContext(MyContextWrapper.wrap(baseContext, appLocale));
}
where MyContextWrapper
is:
public class MyContextWrapper extends ContextWrapper {
private static final String TAG = "Meteogram_ContextWrappr";
public MyContextWrapper(Context base) {
super(base);
}
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, String language) {
Configuration config = context.getResources().getConfiguration();
Locale sysLocale = myGetLocale(config);
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setLocale(config, locale);
} else {
setLocaleLegacy(config, locale);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
return new MyContextWrapper(context);
}
@SuppressWarnings("deprecation")
public static Locale getLocaleLegacy(Configuration config){
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static Locale getLocale(Configuration config){
return config.getLocales().get(0);
}
public static Locale myGetLocale(Context context) {
Configuration config = context.getResources().getConfiguration();
return myGetLocale(config);
}
public static Locale myGetLocale(Configuration config) {
Locale sysLocale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = getLocale(config);
} else {
sysLocale = getLocaleLegacy(config);
}
return sysLocale;
}
@SuppressWarnings("deprecation")
public static void setLocaleLegacy(Configuration config, Locale locale){
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public static void setLocale(Configuration config, Locale locale){
config.setLocale(locale);
}
}
This all works fine for any other language (and there are several in my app), and even for Turkish the layout id of other equivalent layouts are returned just fine... it's just one layout, and just for Turkish.
I'm stumped.
Upvotes: 0
Views: 51
Reputation: 54204
In comments you said String layoutName = "layout_" + tag.replaceAll("([A-Z])", "_$1").toLowerCase()
. You should know that toLowerCase()
and toUpperCase()
will do "weird" things with the i
character in Turkish. I am 99% sure this is the root of the problem.
You can solve by passing a Locale
argument to the toLowerCase()
calls... probably want Locale.ROOT
here.
Upvotes: 1