wood burning stoves 2.0*
The moose likes Java in General and the fly likes Variable in properties file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Variable in properties file." Watch "Variable in properties file." New topic
Author

Variable in properties file.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Hi All,

For localization purpose I am using properties file in my java application. I have few values with variables for example:

I have {0} number of bags


In above case {0} represents the number of bags. I was just thinking if instead of 0 I can say {numberOfBags}. is it possible by any way? I have tried it but didn't work.




Pawan Chopra
SCJP - DuMmIeS mInD
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

try apache velocity...Txt base template engine...


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

Class java.text.MessageFormat can do things like that for you - look it up in the API documentation. Also lookup class java.util.ResourceBundle, which is Java's standard mechanism for dealing with localized messages.

pawan chopra wrote:I have tried it but didn't work.

What have you tried and why did it not work? ItDoesntWorkIsUseless


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

pawan chopra wrote:In above case {0} represents the number of bags. I was just thinking if instead of 0 I can say {numberOfBags}. is it possible by any way?

Is numberOfBags another property in the file? If so, you can definitely get this to work. Well, I could

I won't give you my full class, but here are a few hints:
- Get the result of getProperty(key).
- Use a Pattern to get all the templates.
- For each template, use a recursive call to get their values and replace the value.

One thing: take care of loops. Consider you have two properties, "property1 = Value of property2 is {property2}" and "property2 = Value of property1 is {property1}". Now you want to get the value of property1. For this you need the value of property2. For this you need the value of property1. Oops. Note that this is the simplest form of looping; it can use many intermediate properties to form a loop.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Thanks everyone. Thanks Rob I will try to implement this.
 
GeeCON Prague 2014
 
subject: Variable in properties file.