Skip to content

Instantly share code, notes, and snippets.

@zeero0
Last active November 4, 2020 05:41
Show Gist options
  • Save zeero0/9f5a92b6309c4bad977e65f920af689f to your computer and use it in GitHub Desktop.
Save zeero0/9f5a92b6309c4bad977e65f920af689f to your computer and use it in GitHub Desktop.
Multilingual support for android application
import android.annotation.SuppressLint;
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 java.util.Locale;
public class LocaleManager {
public static final String LANGUAGE_ENGLISH = "";
private static final String LANGUAGE_KEY = "selectedLanguage";
public static Context setLocale(Context c) {
return updateResources(c, getLanguage(c));
}
public static Context setNewLocale(Context c, String language) {
persistLanguage(c, language);
return updateResources(c, language);
}
public static String getLanguage(Context c) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
String lang = prefs.getString(LANGUAGE_KEY, LANGUAGE_ENGLISH);
// if (lang.equals("ar")) {
// Constants.CULTURE = 1;
// }
return lang;
}
@SuppressLint("ApplySharedPref")
private static void persistLanguage(Context c, String language) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
// use commit() instead of apply(), because sometimes we kill the application process immediately
// which will prevent apply() to finish
prefs.edit().putString(LANGUAGE_KEY, language).commit();
// if (language.equals("ar")) {
// Constants.CULTURE = 1;
// } else {
// Constants.CULTURE = 0;
// }
// UserSharedPreference.saveLanguage(language);
}
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Build.VERSION.SDK_INT >= 17) {
config.setLocale(locale);
context = context.createConfigurationContext(config);
} else {
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
public static Locale getLocale(Resources res) {
Configuration config = res.getConfiguration();
return Build.VERSION.SDK_INT >= 24 ? config.getLocales().get(0) : config.locale;
}
}
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.m3logi.R;
import com.m3logi.utils.LocaleManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.setLocale(base));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
}
}
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import com.m3logi.utils.LocaleManager;
public class App extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.setLocale(base));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleManager.setLocale(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment