My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Localization  
Localization in Tapestry 5.
Featured, Phase-Implementation
Updated Feb 4, 2010 by m.h.sh...@gmail.com

for more about localization see http://tapestry.apache.org/tapestry5/tapestry-core/guide/localization.html

Localization in Tapestry 5

Localization is all about getting the right text to the user, in the right language.

Localization support is well integrated into Tapestry. Tapestry allows you to easily separate the text you present to your users from the rest of your application ... pull it out of your Java code and even out of your component templates. You can then translate your messages into other languages and let Tapestry put everything together.

Hints & Tips

How To Support New Languages ?

  • If you want to support New languages in you application, you most add following code to your AppModule class to contribute application new locales and languages :
  •     public static void contributeApplicationDefaults(
                  MappedConfiguration<String, String> configuration) {
    
            configuration.add("tapestry.supported-locales", "en,fa,fr");
        }
this example tells that three locales -English(en) , Persian(fa) and French(fr)- most be supported by application.

Page and Component Message Catalog

  • Like most of Java applications Tapestry use Java property files for localize messages.
Each component and page may have a message catalog. A message catalog is a set of property files (PageName.properties, PageName_fr.properties...). These property files are the same format used by java.util.ResourceBundle, just lines of key=value. These files are packaged with the component's pages HTML template.
note that message catalog name most be its page or component name

Application Message Catalog

  • If the file WEB-INF/AppName.properties exists in the context, it will be used as an application-wide message catalog. The AppName is derived from the name of the filter inside the web.xml file. The search for the file is case sensitive. The properties file may be localized.
Individual pages and components can override the values defined in this message catalog. in other word if you used a message in a component, Tapestry will search Component Message Catalog, Components Parent Message Catalog, Page Message Catalog and Application Message Catalog respectively for resolve message value.

How To Localize Validation Messages ?

  • For localize validation error messages, you most add a message catalog in org.apache.tapestry.internal package. this message catalog name is ValidationMessages
if you want to put this message catalog in other package you most add following code to contribute application validation messages source :
 public void contributeValidationMessagesSource(Configuration<String> configuration) {
        configuration.add("path/to/your/package/ValidationMessages.properties");
    }

How To Access Messages From Templates ?

  • For access a message from a template you most use message binding prefix (message:) just before your message name in template.
  • <input type="submit" value="${message:save}"/>
    ...
    <h1>${message:welcome}</h1>

How To Switch Between Application Locales ?

  • For change locale in your application you can use PersistentLocale class
  • here is a example of changing locale with PersistentLocale :
        @Inject 
        private PersistentLocale localeService;
    
        @OnEvent(value = "action", component = "locale")
        void changeLocale() {
            if ("fr".equalsIgnoreCase(localeService.get().getLanguage())) {
                localeService.set(new Locale("en"));
            } else {
                localeService.set(new Locale("fr"));
            }
        }
    

for more about localization see http://tapestry.apache.org/tapestry5/tapestry-core/guide/localization.html

good luck.

Comment by xsuperov...@gmail.com, Oct 24, 2008

How do you overwrite the Locale inside AppModule?? E.g. assigning different locales to domain names or subdomains ?

Comment by xsuperov...@gmail.com, Oct 24, 2008

How can you access PersistentLocale? from inside the AppModule??

Comment by gabriel....@gmail.com, Jul 16, 2009

Please note that the ValidationMessages?.properties file should be in the package org.apache.tapestry5.internal and not org.apache.tapestry.internal (tapestry5 instead of just tapestry).

Comment by mohd...@gmail.com, Feb 27, 2010

Hi, I've been trying to create an example to switch between languages in Tapestry 5. I've used the code above and got a NuulPointerException?. did I miss something? by the way, I am new to Tapestry 5.

Thank you Mohamed Omar

Comment by gerardo...@gmail.com, Aug 20, 2010

Great documentation

Comment by s.camb...@gmail.com, Dec 28, 2011

How to avois adding locale prefix in the URL like http://host/fr/home or http://host/en/home ? I found that I should 'add configuration.add(SymbolConstants?.ENCODE_LOCALE_INTO_PATH, "false");' to the AppModule? class and use LinkCreationListener2? or LinkCreationListener?(if Tapestry < 5.2.). But how?


Sign in to add a comment
Powered by Google Project Hosting