Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable in properties file.

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try apache velocity...Txt base template engine...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. Thanks Rob I will try to implement this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic