I have some trouble with adding multi-language to my site.
It works fine on the .jsp file with jstl, but when I try to get the translations with Java code, it ignores the Locale parameter, and uses the Client browsers locale.
I have:
Two properties files:
dictionary.properties, containing: validate.firstname.empty=You have to enter a first name
dictionary_nl.properties, containing: validate.firstname.empty=U heeft geen voornaam ingevuld
And this Java code:
public List<String> validate(UserForm user, Locale locale)
{
List<String> errors = new ArrayList<>();
ResourceBundle resources = ResourceBundle.getBundle("dictionary", new Locale("en"));
if (user.getFirstName() == null || user.getFirstName().trim().isEmpty())
{
errors.add(resources.getString("validate.firstname.empty"));
}
return errors;
}
For testing purposes, I inserted a new instance of Locale to the getBundle
, but it still returns the dutch translation if my browser is set to dutch, and the English translation if the browser is set to English.
Actually getBundle() doesn't ignore the Locale, it just appears to ignore it in your configuration.
Because there is no dictionary_en.properties
file in your configuration, the Locale for language "en" will have no effect in the ResourceBundle file search.
Test with new Locale("nl")
instead, and you'll always get the Dutch translation whatever the language set in your browser.
More details in the documentation:
http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader%29
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With