• Post Reply Bookmark Topic Watch Topic
  • New Topic

Internationalization in multiple classes  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have been busy implementing internationalization in a program with about 10 classes. Thing is: I got it implemented in the startup class and in that class it works. But can anyone please explain to me how I can implement it in other classes as well? I can't find anything about this on the web (strangly). How do I get the right resourcebundle connected to the language AND also have the class read it? ---> only thing I can think of is to make a methode setTaal(){ ResourceBundle.getBundle("gui.Bundle", new Locale("nl", "NL"));} and call upon it in every method in every class...surely there must be a better way??? Please help me out, I have been going nuts over this for many hours now

This is what I ve got so far:

STARTUP CLASSE


So I would like to also internationalize the following class. I already did labels "(taal.getString("OngeldigException"));" on the strings. But how do I get the ResourceBundle taal implemented without having to do this is every single method?

 
Greenhorn
Posts: 25
AngularJS Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If i got you right, your objective is to reach de defined locale on the main method on every class right?

You can use the methods Locale.setDefault(Locale newLocale) and Locale.getDefault() to change the JVM defined locale!

Oracle Reference wrote:The default locale of your application is determined in three ways. First, unless you have explicitly changed the default, the Locale.getDefault() method returns the locale that was initially determined by the Java Virtual Machine (JVM) when it first loaded. That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.language, user.country, and user.variant system properties.

Third, your application can call the Locale.setDefault(Locale) method. The setDefault(Locale aLocale) method lets your application set a systemwide (actually VM-wide) resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.


Hope it helps
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you ever read the Java® Tutorials, because there is a section about internationalisation.

Don't throw plain simple Exception for incorrect arguments. Throw IllegalArgumentException or NullPointerException (←links) instead.
 
Saloon Keeper
Posts: 7994
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would prefer to pass the locale as an argument, or as part of some configuration object which is then passed. This makes unit testing your classes much easier. This is called Dependency Injection.

You shouldn't use exceptions to handle normal application flow. It is to be expected that users can enter a bad user name and password combination. If this is the case, have the method geefSpeler() just return null (or even better, an empty Optional). In the client method() you can determine what to do with that situation.

If you do decide to use exceptions, make them into specific types. *Never* throw a generic Exception.

User password should also be stored using a salt and hash. Here's how it should look like:

Avoid using String to pass passwords around. Passwords should always be passed as character arrays, because you can clear them, while Strings are immutable and may linger in memory.
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:User password should also be stored using a salt and hash.

I agree, but I would use JBCrypt to do the dirty work instead of using your own code.
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the answers! I got it right by passing arguments to the constructors and than setting the resourcebundle in de constructor as mentioned several times here

Campbell Ritchie I checked that several times, but I could only find sources which explained how to implement it on 1 class programmes. Which this is not;) So thats why I came knocking on the door of good old Coderanch!

Also, I am not a full fledged programmer yet, so the tips about sending around passwords and proper use of exceptions are very welcome!

Again thanks, this really helps me forward (and saves me another evening of frustration & a neglected girlfriend;))

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!