Search code examples
javalocale

How to validate a locale in java?


I read a file in an application that specifies a language code:

public void setResources(String locale) {

    // validate locale
    // ULocale lo = new ULocale(locale);
    // System.out.println(lo.getDisplayCountry());

}

that must be in the format: <ISO 639 language code>_<ISO 3166 region code> eg. en_UK, en_US etc. Is it possible to validate that the locale string is valid before continuing?


Solution

  • Now I just do:

    ULocale uLocale = new ULocale(locale);
    if (uLocale.getCountry() != null && !uLocale.getCountry().isEmpty()) {
      ...
    }
    

    which works fine.