Wednesday, 31 July 2013

Simple Internationalization in Liferay

Simple Internationalization in Liferay


I will make it short and simple
  1. Create the language bundle in src/content folder with files like Language.properties, Language_ar_SA.properties etc.
  2. For portlet api to use this bundle so that it can set the title etc as per the language selected, you need to set this bundle in portlet.xml file using
      <resource-bundle>content.Language</resource-bundle>
    
      
  3. Then in bundle, you can set javax.portlet.title property for portlet to pick up based on the language selected.
  4. In jsps, for properties used in your use cases you can write the following:
      <fmt:setLocale value="<%=themeDisplay.getLanguageId() %>"/>
    
    <fmt:setBundle basename="content.Language" />
    
    <h1><fmt:message key="profile.label"/></h1>
    
  5. In your portlet class you can do the following:
         ThemeDisplay display = (ThemeDisplay) request
                    .getAttribute(WebKeys.THEME_DISPLAY)
       System.out.println(display.getLanguageId());
    
       ResourceBundle rb = ResourceBundle.getBundle("content.Language",
                    LocaleUtil.fromLanguageId(display.getLanguageId()));// LocaleUtil.fromLanguageId("ar"));
       String label = rb.getString("profile.label");
    
       System.out.println(label);

No comments:

Post a Comment