public class Localer extends Object
Locale for an Android application.
Which locale should be used can be specified via setLocale(Locale, Resources).
The specified locale will be set up as default locale via Locale.setDefault(Locale) and
also will be used to update the configuration of the Android application resources so also strings
from resources will be properly obtained based on the specified locale.
The best place where to place Localer is within custom Application implementation
as shown in the following sample:
public final class LocalizedApplication extends Application {
// Localer instance used to update locale of this Android application.
private final Localer localer = new Localer(Locale.FRENCH);
@Override
public void onCreate() {
super.onCreate();
this.localer.dispatchApplicationCreated(getResources());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
this.localer.dispatchConfigurationChanged(newConfig, getResources());
}
// Changes the current application locale to the specified one.
public void changeLocale(@NonNull Locale locale) {
this.localer.setLocale(locale, getResources());
// Note, that the call above will change default locale and also locale of this Android
// application resources, unfortunately, if you want to this change be presented also in
// the currently running UI (activity), such an UI needs to be recreated so a new values
// from resources based on the new locale can be loaded properly.
}
}
| Constructor and Description |
|---|
Localer()
Creates a new instance of Localer to manage custom locale within this Android application with
default Locale set to
Locale.ENGLISH. |
Localer(Locale locale)
Creates a new instance of Localer to manage custom locale within this Android application with
the given default Locale.
|
| Modifier and Type | Method and Description |
|---|---|
void |
dispatchApplicationCreated(Resources resources)
Called from application's
Application to dispatch, that application
was just created. |
void |
dispatchConfigurationChanged(Configuration newConfig,
Resources resources)
Called from application's
Application
to dispatch changed configuration. |
Locale |
getLocale()
Returns the current locale assigned to this Localer.
|
void |
setLocale(Locale locale,
Resources resources)
Sets the given locale to be used as locale for this Android application, so all stuffs related
to locale, like resources will depends on the passed Locale.
|
public Localer()
Locale.ENGLISH.public Localer(@NonNull Locale locale)
locale - The desired locale.setLocale(Locale, Resources)public void dispatchApplicationCreated(@NonNull Resources resources)
Application to dispatch, that application
was just created.resources - An application resources.dispatchConfigurationChanged(Configuration, Resources)public void dispatchConfigurationChanged(@NonNull Configuration newConfig, @NonNull Resources resources)
Application
to dispatch changed configuration.newConfig - An instance of changed configuration.resources - An application resources.dispatchApplicationCreated(Resources)public void setLocale(@NonNull Locale locale, @NonNull Resources resources)
Note, that this does not reload the currently running UI (activity/-ies) so the locale change will be not visible in the UI until it is re-created so a new values from resources based on the changed locale can be properly loaded again.
locale - An instance of locale to be used as locale for this Android application.resources - An application resources.