Change locale of android application programatically
suggest changeThe examples above show how to localize an application's resources. The following example changes the locale from inside the app, rather than from the device settings. To change only the application locale, use the utility below.
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import android.view.ContextThemeWrapper;
import java.util.Locale;
/**
* Created by Umesh on 10/10/16.
*/
public class LocaleUtils {
private static Locale mLocale;
public static void setLocale(Locale locale){
mLocale = locale;
if(mLocale != null){
Locale.setDefault(mLocale);
}
}
public static void updateConfiguration(ContextThemeWrapper wrapper){
if(mLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
Configuration configuration = new Configuration();
configuration.setLocale(mLocale);
wrapper.applyOverrideConfiguration(configuration);
}
}
public static void updateConfiguration(Application application, Configuration configuration){
if(mLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){
Configuration config = new Configuration(configuration);
config.locale = mLocale;
Resources res = application.getBaseContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
public static void updateConfiguration(Context context, String language, String country){
Locale locale = new Locale(language,country);
setLocale(locale);
if(mLocale != null){
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
configuration.locale = mLocale;
res.updateConfiguration(configuration,res.getDisplayMetrics());
}
}
public static String getPrefLangCode(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString("lang_code","en");
}
public static void setPrefLangCode(Context context, String mPrefLangCode) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putString("lang_code",mPrefLangCode);
editor.commit();
}
public static String getPrefCountryCode(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString("country_code","US");
}
public static void setPrefCountryCode(Context context,String mPrefCountryCode) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putString("country_code",mPrefCountryCode);
editor.commit();
}
}
Initialize locale that user preferred, from Application class.
public class LocaleApp extends Application{
@Override
public void onCreate() {
super.onCreate();
LocaleUtils.setLocale(new Locale(LocaleUtils.getPrefLangCode(this), LocaleUtils.getPrefCountryCode(this)));
LocaleUtils.updateConfiguration(this, getResources().getConfiguration());
}
}
Create a base activity and extend it from every other activity so the locale is applied in one place:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public abstract class LocalizationActivity extends AppCompatActivity {
public LocalizationActivity() {
LocaleUtils.updateConfiguration(this);
}
// We only override onCreate
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Call
applyOverrideConfigurationbeforeonCreate. This sample does that from the activity constructor.attachBaseContextis the more common place, and some support-library versions expect the override there.
On API 17 and later, LocaleApp.onCreate does not update resources: updateConfiguration(Application, Configuration) runs only below API 17. The activity path above is what applies the locale on newer APIs. The updateConfiguration(Context, String, String) overload always writes configuration.locale and calls Resources.updateConfiguration, including on API 17 and later.
Use LocalizationActivity as follows.
public class MainActivity extends LocalizationActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
After a programmatic locale change, restart the activity so the new resources load. Set
android:name=".LocaleApp"on the<application>element inAndroidManifest.xml, and read the saved locale when the application starts.
Lint can block a release build when translations are missing. Use one of the options below.
First:
To keep one string from being translated, add this attribute in the default strings.xml:
<string name="developer" translatable="false">Developer Name</string>
Second:
To ignore every missing translation in one resource file, set the tools-namespace ignore attribute:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" >
<!-- your strings here; no need now for the translatable attribute -->
</resources>
Third:
Non-translatable Strings describes another way to mark strings that should not be translated.
If many resources should not be translated, place them in a file named donottranslate.xml. Lint treats every string in that file as non-translatable.
Fourth:
You can also declare the default locale on the resource file:
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:locale="en"
tools:ignore="MissingTranslation">
</resources>
You can also disable the missing-translation check in app/build.gradle:
lintOptions {
disable 'MissingTranslation'
}