If you think the Android project Munin-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.chteuchteu.munin.hlpr;
//fromwww.java2s.comimport android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import com.chteuchteu.munin.MuninFoo;
import com.chteuchteu.munin.R;
import java.util.Arrays;
import java.util.Locale;
publicfinalclass I18nHelper {
publicenum AppLanguage {
EN("en", R.string.lang_english), FR("fr", R.string.lang_german), DE("de", R.string.lang_german), RU("ru", R.string.lang_russian);
public String langCode;
publicint localeNameRes;
AppLanguage(String langCode, int localeNameRes) {
this.langCode = langCode; this.localeNameRes = localeNameRes;
}
publicstatic AppLanguage defaultLang() { return AppLanguage.EN; }
/**
* get AppLanguage enum value from langCode.
* If not found, returns the default language (EN)
*/publicstatic AppLanguage get(String langCode) {
for (AppLanguage lang : AppLanguage.values()) {
if (lang.langCode.equals(langCode))
return lang;
}
return defaultLang();
}
publicint getIndex() {
return Arrays.asList(AppLanguage.values()).indexOf(this);
}
}
publicstaticboolean isLanguageSupported(String languageCode) {
for (AppLanguage lang : AppLanguage.values()) {
if (lang.langCode.equals(languageCode))
return true;
}
return false;
}
publicstaticvoid loadLanguage(Context context, MuninFoo muninFoo) { loadLanguage(context, muninFoo, false); }
/**
* Load language according to preferences, if set. If not : lang is
* already set according to device locale.
* @param context Activity context
* @param forceLoad Force language load (after language change)
*/publicstaticvoid loadLanguage(Context context, MuninFoo muninFoo, boolean forceLoad) {
String lang = Util.getPref(context, Util.PrefKeys.Lang);
if (!lang.equals("")) {
if (!muninFoo.languageLoaded || forceLoad) {
if (!isLanguageSupported(lang))
lang = "en";
Resources res = context.getApplicationContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = new Locale(lang);
res.updateConfiguration(conf, dm);
muninFoo.languageLoaded = true;
}
}
// else: lang set according to device locale
}
}