Search code examples
javainternationalizationglobalization

Java default Locale fallback


In Java I create Locale with Language and Country parameters and then use appropriate date formats.

However Java does not support all Language_Country combinations and fallbacks to default locale formats (in my case en_US).

Is there a way to determine that fallback occured?

E.g:

When I create locale with Locale locale = new Locale("xx","xx");

then

((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT,locale)).toLocalizedPattern();

returns M/d/yy so it fallbacks to en_US.

So i need something like

locale.isDefault


Solution

  • You can check that the locale is one of the available locales:

    Set<Locale> locales = new HashSet<>();
    Collections.addAll(locales, Locale.getAvailableLocales());
    
    Locale locale = new Locale("xx", "xx");
    System.out.println(locales.contains(locale)); //prints false
    
    locale = new Locale("fr", "FR");
    System.out.println(locales.contains(locale)); //prints true