Switch between languages
From OpenCms Wiki
(Difference between revisions)
(brainstorming what to put on this page) |
|||
(2 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
How to link between the same content page in different languages: | How to link between the same content page in different languages: | ||
(short summary: retrieve current url - replace "language folder" name - link to new url) | (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: | ||
+ | <code lang="Java"> | ||
+ | <%@ page import="java.util.HashMap"%> | ||
+ | <%@ page import="java.util.Map"%> | ||
+ | <%@ page import="java.util.List"%> | ||
+ | <%@ page import="org.opencms.jsp.CmsJspActionElement"%> | ||
+ | <%@ page import="org.opencms.file.CmsObject"%> | ||
+ | <%@ page import="org.opencms.main.CmsException"%> | ||
+ | <%@ page import="org.opencms.flex.CmsFlexController"%> | ||
+ | <%@ page import="org.opencms.file.CmsResourceFilter"%> | ||
+ | <%@ page import="org.opencms.file.CmsResource"%> | ||
+ | |||
+ | <%@ 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><br/> | ||
+ | <a href="<%= cms.link(linksByLocale.get("nl")) %>">nl</a><br/> | ||
+ | <a href="<%= cms.link(linksByLocale.get("fr")) %>">fr</a><br/> | ||
+ | <a href="<%= cms.link(linksByLocale.get("de")) %>">de</a><br/> | ||
+ | </code> | ||
+ | The above could be saved as a separate jsp element and then included in your templates. |
Latest revision as of 09:56, 24 June 2008
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 import="java.util.HashMap"%> <%@ page import="java.util.Map"%> <%@ page import="java.util.List"%> <%@ page import="org.opencms.jsp.CmsJspActionElement"%> <%@ page import="org.opencms.file.CmsObject"%> <%@ page import="org.opencms.main.CmsException"%> <%@ page import="org.opencms.flex.CmsFlexController"%> <%@ page import="org.opencms.file.CmsResourceFilter"%> <%@ page import="org.opencms.file.CmsResource"%> <%@ 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><br/> <a href="<%= cms.link(linksByLocale.get("nl")) %>">nl</a><br/> <a href="<%= cms.link(linksByLocale.get("fr")) %>">fr</a><br/> <a href="<%= cms.link(linksByLocale.get("de")) %>">de</a><br/>
The above could be saved as a separate jsp element and then included in your templates.