Created
February 15, 2015 12:07
-
-
Save czyrux/0edee174d72eb5c12108 to your computer and use it in GitHub Desktop.
Utility class to change update locale settings within an Android Application. Changes will be reflected only within the app
This file contains 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
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.content.res.Configuration; | |
import android.content.res.Resources; | |
import android.os.Build; | |
import java.util.Locale; | |
/** | |
* Utility class to change app locale settings. | |
*/ | |
public class LocaleUtils { | |
private LocaleUtils() { | |
} | |
/** | |
* Transform a language code in its corresponding locale. If the country code includes country settings, | |
* it would be used, otherwise just the language is used. | |
* | |
* @param langCode Language code. Its follows the format de_DE, tr_TR, etc | |
* @return Locale instance | |
*/ | |
public static Locale transform(String langCode) { | |
if (langCode.length() > 2) { | |
return new Locale(extractLocaleLanguage(langCode), extractLocaleCountry(langCode)); | |
} else { | |
return new Locale(extractLocaleLanguage(langCode)); | |
} | |
} | |
/** | |
* Extract country code from the langCode. | |
* | |
* @param langCode Language code. Its follows the format de_DE, tr_TR, etc | |
* @return Locale instance | |
*/ | |
public static String extractLocaleCountry(String langCode) { | |
return langCode.substring(3, 5); | |
} | |
/** | |
* Extract language code from the langCode. | |
* | |
* @param langCode Language code. Its follows the format de, tr, de_DE, tr_TR, etc | |
* @return extracted code | |
*/ | |
public static String extractLocaleLanguage(String langCode) { | |
return langCode.substring(0, 2); | |
} | |
/** | |
* Create a new Android configuration base on the Locale. | |
*/ | |
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | |
public static Configuration updateLocaleConfiguration(Activity activity, Locale newLocale) { | |
final Resources resources = activity.getResources(); | |
// Set default locale | |
Locale.setDefault(newLocale); | |
// Clone current settings and update locale configuration | |
Configuration config = new Configuration(resources.getConfiguration()); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
config.setLocale(newLocale); | |
} else { | |
config.locale = newLocale; | |
} | |
resources.updateConfiguration(config, resources.getDisplayMetrics()); | |
return config; | |
} | |
} |
This file contains 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 LocalizedActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
LocaleUtils.updateLocaleConfiguration(this, LocaleUtils.transform("de_DE")); | |
// Remaining activity code... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment