Switch between languages

From OpenCms Wiki
Revision as of 14:57, 13 November 2006 by Bart.vandendriessche (Talk | contribs)
Jump to: navigation, search

How to link between the same content page in different languages: (short summary: retrieve current url - replace "language folder" name - link to new url)

Switch between siblings

One way to switch between languages is to check if the currently active content has a sibling, and if it does, link to that. An example of how you could implement this:

   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
   
   <%
       CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
       CmsObject cmso          = CmsFlexController.getCmsObject(request);
       String strPagename      = cms.getRequestContext().getUri();
       String homePage         = "/playground/index.html";
       Map<String, String> linksByLocale = new HashMap<String, String>();
       linksByLocale.put("en", homePage);
       linksByLocale.put("de", homePage);
       linksByLocale.put("fr", homePage);
       linksByLocale.put("nl", homePage);
       try {
           List<CmsResource> siblings = cmso.readSiblings(strPagename, CmsResourceFilter.ALL);
           for (CmsResource sibling : siblings) {
               String uri = cmso.getSitePath(sibling);
               linksByLocale.put(cmso.readPropertyObject(sibling, "locale", true).getValue(), uri);
           }
       } catch (CmsException e) {
   
       }
   %>    
       <a href="<%= cms.link(linksByLocale.get("en")) %>">en</a>
<a href="<%= cms.link(linksByLocale.get("nl")) %>">nl</a>
<a href="<%= cms.link(linksByLocale.get("fr")) %>">fr</a>
<a href="<%= cms.link(linksByLocale.get("de")) %>">de</a>

The above could be saved as a separate jsp element and then included in your templates.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox