aspose file tools*
The moose likes JSF and the fly likes Using properties file in source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Using properties file in source file" Watch "Using properties file in source file" New topic
Author

Using properties file in source file

Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
I have made a server application and now i want it to support multiple languages. I have looked over the net and found that solution is properties file.

Now i have the next problem, while i can change jsf pages via messages, i want also to change the language of my hardcoded strings in source file also using this same properties file. Can this be accomplished?
roby george
Ranch Hand

Joined: Sep 08, 2010
Posts: 31
Yes you can use properties file for multi language, but in that case you have to pick all text from properties file, dont wtite any text direct in source file.
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Let me be more clear - I have JSF page and a bean behind it. Some of the content of the page is adjusted dynamically by methods of the bean, eg i wite some text to the page. I also want that text to be language dependent.

How do i pick that text from properties file?
roby george
Ranch Hand

Joined: Sep 08, 2010
Posts: 31
So i think you need to use some langugae convertor, which convert your text onthe fly and display that on your pgae, Propertis file only use when you have fixed text.

clear me if i m wrong...
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4


where certainI18nString is a property in your backing bean containing your internationalized string.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
Dieter Quickfend wrote:
where certainI18nString is a property in your backing bean containing your internationalized string.


I want all my strings to be in .propeties file - the ones i hardcoded in .java files, and the ones i use on jsf pages. I dont want to have something in .properties file(for static jsf values) and something in other txt file (for values that bean returns that are shown on the page).

its like this:



First is static value user that is same for all users. Second is country of origin of user which can be different for every user. If we have user named Bob, from England, following would be written for him:

User: England

If we switch language to Italian it sholud write:

Utente: Inghilterra

And both of these words should be in .properties file.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I just did a quick search on JSF i18n and it linked me to this post on the ranch

Full details on i18n with JSF can be found in The JSF Tutorial

But basically
- put ALL your translatable text into a message properties file
- import your properties file into the JSF page with the <f:loadBundle> tag
- anywhere you want to display the text from the properties bundle, use the <h:outputText> tag



That would print out a translated "user" string, and then the value of the beans "country" straight from the bean.

If you also wanted to translate the country (as you did in your example), you would have to make the country stored in the bean a country code to look it up in the properties file.


cheers,
Stefan
Vladislav Simovic
Ranch Hand

Joined: Feb 03, 2011
Posts: 37
I also did some research of my own in the meantime and i found quite good tutorial:

http://webcache.googleusercontent.com/search?q=cache:BOkPVeCsP5gJ:www.techcubetalk.com/2009/01/how-to-load-a-properties-file-in-java/+java+how+to+access+properties+file+from+from+runtime&cd=2&hl=en&ct=clnk&source=www.google.com
 
 
subject: Using properties file in source file