Created
May 13, 2017 14:48
-
-
Save JakeSteam/4ee40b6df4e30a968231d38faa2f26de to your computer and use it in GitHub Desktop.
"Implementing A Locale / Language Selector" for GameDevAlgorithms.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LanguageHelper { | |
public static void updateLanguage(Context ctx) { | |
String lang = PreferenceManager.getDefaultSharedPreferences(ctx).getString("locale", ""); | |
updateLanguage(ctx, lang); | |
} | |
public static void updateLanguage(Context context, int language) { | |
updateLanguage(context, getLocaleById(language)); | |
} | |
private static void updateLanguage(Context ctx, String lang) { | |
PreferenceManager.getDefaultSharedPreferences(ctx).edit().putString("locale", lang).apply(); | |
Configuration cfg = new Configuration(); | |
if (!TextUtils.isEmpty(lang)) { | |
cfg.locale = new Locale(lang); | |
} else { | |
cfg.locale = Locale.getDefault(); | |
} | |
ctx.getResources().updateConfiguration(cfg, null); | |
} | |
private static String getLocaleById(int id) { | |
switch (id) { | |
case Constants.LANG_ENGLISH: | |
return "en"; | |
case Constants.LANG_FRENCH: | |
return "fr"; | |
case Constants.LANG_RUSSIAN: | |
return "ru"; | |
case Constants.LANG_DUTCH: | |
return "nl"; | |
case Constants.LANG_CHINESE: | |
return "zh"; | |
case Constants.LANG_KOREAN: | |
return "ko"; | |
case Constants.LANG_JAPANESE: | |
return "ja"; | |
case Constants.LANG_SPANISH: | |
return "es"; | |
case Constants.LANG_GERMAN: | |
return "de"; | |
} | |
return ""; | |
} | |
public static int getDefaultLanguage() { | |
switch (Locale.getDefault().getLanguage()) { | |
case "en": | |
return Constants.LANG_ENGLISH; | |
case "fr": | |
return Constants.LANG_FRENCH; | |
case "ru": | |
return Constants.LANG_RUSSIAN; | |
case "nl": | |
return Constants.LANG_DUTCH; | |
case "zh": | |
return Constants.LANG_CHINESE; | |
case "ko": | |
return Constants.LANG_KOREAN; | |
case "ja": | |
return Constants.LANG_JAPANESE; | |
case "es": | |
return Constants.LANG_SPANISH; | |
case "de": | |
return Constants.LANG_GERMAN; | |
} | |
return Constants.LANG_ENGLISH; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
LanguageHelper.updateLanguage(getApplicationContext()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { | |
setting.setIntValue(position + 1); | |
setting.save(); | |
LanguageHelper.updateLanguage(activity, position + 1); | |
ToastHelper.showPositiveToast(parentView, ToastHelper.SHORT, parentView.getSelectedItem().toString(), true); | |
onResume(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parentView) { | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment