Я боролся с динамическими c Изменение языка на устройствах Samsung даже до Android 10.
Возможно, сейчас есть лучшее решение,
, но в то же время в дополнение к тому, что вы сделали,
Я закончил тем, что получил все строки по идентификаторам ресурсов следующим образом:
public static String getStringByIdentifier(final Context context, final String stringResIdName, final boolean forceRefresh) {
final Resources res = getResources(context, forceRefresh);
String result;
try {
result = res.getString(res.getIdentifier(stringResIdName, "string",
context.getPackageName()));
} catch (final Resources.NotFoundException e) {
result = stringResIdName; // TODO or here you may throw an Exception and handle it accordingly.
}
return result;
}
public static String getStringByIdentifier(final Context context, final String stringResIdName) {
return getStringByIdentifier(context, stringResIdName, false);
}
private static Resources getResources(final Context context, final boolean refreshLocale) {
if (!refreshLocale) {
return context.getResources();
} else {
final Configuration configuration = new Configuration(context.getResources().getConfiguration());
configuration.setLocale(Locale.getDefault());
return context.createConfigurationContext(configuration).getResources();
}
}
Таким образом, вы должны установить текст следующим образом:
textView.setText(AndroidUtils.getStringByIdentifier(context, "string_res_name"));
Где соответствующая строка Ресурс:
<string name="string_res_name">Some string</string>