Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 725
el vis Avatar asked Apr 25 '26 16:04

el vis


1 Answers

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
like image 97
assylias Avatar answered Apr 27 '26 07:04

assylias