For example, Locale.US.toString() => "en_US", but new Locale(Locale.US.toString()).toString() => "en_us". To recover the original locale, you have to split the components and then call the constructor, resulting in new Locale("en", "US").
Yes, it's easy to write something like this
public static Locale parseLocale(String locale) {
if (locale == null)
return null;
String[] components = locale.split("_", 3);
if (components.length == 2)
return new Locale(components[0], components[1]);
else if (components.length == 3)
return new Locale(components[0], components[1], components[2]);
else
return new Locale(locale);
}
But that's stupid. Something like that should be a static method in java.util.Locale, though it should probably just throw a NullPointerException instead of returning null if the parameter is null.
No comments:
Post a Comment