Java i18n: use one ResourceBundle accessor per package, or one for whole project?
-
20-09-2019 - |
Question
I don't know anything about internationalization.
I have a project with several packages. I am using Eclipse's built-in "Externalize Strings" wizard to extract String constants in my classes to a properties file and replace those Strings with calls to a static accessor. So instead of
System.out.println("Hello, world!");
I end up with
System.out.println(Messages.getString("MyKey"));
and a Messages
utility class in every package (which provides a static getString
method for getting the desired String from a ResourceBundle [in this case a .properties
file]). Is it best to have a Messages
class in every package, or have a single Messages
class for the entire project?
Solution
I'm in favor of a global localization file. You will have duplicate keys ("OK", "Cancel") which would require duplication or nesting, and interfacing with outside localization people is easier if the resources are consolidated.
OTHER TIPS
I would keep one-per-package. This way maintenance/refactoring/unit-testing is much easier.
Also, there are no global dependencies on a single file.