Posts

Showing posts with the label Locale

Couldn't Translate Date To Spanish With Locale("es_ES")

Answer : "es_ES" is a language + country. You must specify each part separately. The constructors for Locale are: Locale(String language) Construct a locale from a language code. Locale(String language, String country) Construct a locale from language, country. Locale(String language, String country, String variant) Construct a locale from language, country, variant. You want new Locale("es", "ES"); to get the Locale that goes with es_ES. However, it would be better to use Locale.forLanguageTag("es-ES") , using the well-formed IETF BCP 47 language tag es-ES (with - instead of _ ), since that method can return a cached Locale , instead of always creating a new one. tl;dr String output = ZonedDateTime.now ( ZoneId.of ( "Europe/Madrid" ) ) .format ( DateTimeFormatter.ofLocalizedDate ( FormatStyle.FULL ) .withLocale ( new Locale ( "es" , "ES" ) ) ) ; martes 12 de jul...

Android Get Current Locale, Not Default

Answer : The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched . Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated. If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e. Locale current = getResources().getConfiguration().locale; You may find that this value is updated more quickly after a settings change if that is necessary for your application. Android N (Api level 24) update (no warnings): Locale getCurrentLocale(Context context){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ return context.getResources().ge...

Android Context.getResources.updateConfiguration() Deprecated

Answer : Inspired by Calligraphy, I ended up creating a context wrapper. In my case, I need to overwrite system language to provide my app users with the option of changing app language but this can be customized with any logic that you need to implement. import android.annotation.TargetApi; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Configuration; import android.os.Build; import java.util.Locale; public class MyContextWrapper extends ContextWrapper { public MyContextWrapper(Context base) { super(base); } @SuppressWarnings("deprecation") public static ContextWrapper wrap(Context context, String language) { Configuration config = context.getResources().getConfiguration(); Locale sysLocale = null; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { sysLocale = getSystemLocale(config); } else { sysL...