androiddatetimelocalerelative-date

default locale for getRelativeTimeSpanString


I override the locale of my app using:

 Locale locale = new Locale("ca");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

But it seems that getRelativeTimeSpanString doesn't work with application context, and instead use the system locale.


Solution

  • Your solution was almost correct.

    However, since the strings are loaded from the system resources, you must update the configuration of the system resources. Below is what I did:

        Configuration configuration = new Configuration(Resources.getSystem().getConfiguration());
            configuration.locale = Locale.GERMANY; // or whichever locale you desire
            Resources.getSystem().updateConfiguration(configuration, null);
    

    (NB: From Jelly Bean MR1 onwards, it is recommended to use

    configuration.setLocale(Locale.GERMANY);
    

    to change the locale rather then setting the member directly.)