I Integrated Locale (en
and ar
) on my application. Once I change the application to Arabic(ar
) then close and open my application it showing the layout direction is showing as Arabic but the strings are loading English.
Here is my code to change the language while button click,
public static void setLocale(final Context ctx, final String lang) {
Log.d(TAG, "##Changing Language to: " + lang);
AppSettings.getInstance(ctx).save(PrefKeys.language, lang);
final Locale loc = new Locale(lang);
Resources resources = ctx.getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
configuration.setLocale(loc);
else
configuration.locale = loc;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
ctx.createConfigurationContext(configuration);
else
resources.updateConfiguration(configuration, displayMetrics);
}
Also on the Application class I added code to apply the language, Here is the code on Application Class
@Override
public void onCreate() {
super.onCreate();
String lanuage = AppSettings.getInstance(getApplicationContext()).getLanguage();
setLocale(new Locale(lanuage));
}
private void setLocale(Locale locale){
Log.d(TAG, "##Changing Language to: " + locale.getLanguage());
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
configuration.setLocale(locale);
} else{
configuration.locale=locale;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
getApplicationContext().createConfigurationContext(configuration);
} else {
resources.updateConfiguration(configuration,displayMetrics);
}
}
I created new BaseActivity.class
and extended that class to Home Screen,
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}
protected abstract int getLayoutResourceId();
@Override
protected void attachBaseContext(Context newBase) {
String language = AppSettings.getInstance(newBase).getLanguage();
super.attachBaseContext(LocaleHelper.wrap(newBase, language));
}
private static class LocaleHelper extends ContextWrapper {
public LocaleHelper(Context base) {
super(base);
}
public static ContextWrapper wrap(Context context, String language) {
if (TextUtils.isEmpty(language.trim())) {
return new LocaleHelper(context);
}
Configuration config = context.getResources().getConfiguration();
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
//noinspection deprecation
config.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLayoutDirection(locale);
context = context.createConfigurationContext(config);
} else {
//noinspection deprecation
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
return new LocaleHelper(context);
}
} // LocaleHelper
}
Here is the code modification on HomeActivity,
public class HomeActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_home);
ButterKnife.bind(this);
}
@Override
protected int getLayoutResourceId() {
return R.layout.activity_home;
}
}