Reputation: 391
I have a Language
class, for example, which will contain constant attributes of type String
, which will be used all over the program to print x
or y
Strings
based on a setting when launching the program.
How could I do this? I throught of using Enum
, but I lack experience with them and failed to apply them correctly; tried diferent classes which inherit Language
and must specify the values of each String
, but that looked like a home-made Enum
.
As a plus, I'd love to have an XML file containing each String
content, but I have absolutely no idea of grabbing values of an XML file in java (I probably know how to do them separatedly). If possible, a little example or documentation.
Thanks.
Upvotes: 0
Views: 63
Reputation: 22233
You can use properties files, i.e. naming them en-GB.properties, en-US.properties etc, this is how the file looks like:
en-GB.properties
file=File
edit=Edit
it-IT.properties
file=File
edit=Modifica
The string before the '=' symbol is the property name, the string after it is the property value, this is the code to access it:
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("it-IT.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("file")); //prints "File"
System.out.println(prop.getProperty("edit"));//prints "Modifica"
} catch (IOException ex) {
ex.printStackTrace();
}
Hope this helps
Upvotes: 1