1 /** 2 * This file Copyright (c) 2003-2010 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.cms.i18n; 35 36 import info.magnolia.cms.core.Content; 37 import info.magnolia.cms.core.NodeData; 38 import info.magnolia.context.MgnlContext; 39 40 import java.util.Locale; 41 42 import org.apache.commons.lang.LocaleUtils; 43 import org.apache.commons.lang.StringUtils; 44 import org.slf4j.Logger; 45 import org.slf4j.LoggerFactory; 46 47 /** 48 * Serves i18n content found in a per locale hierarchy structure. 49 * E.g. 50 * <pre> 51 * +my-website 52 * + <strong>en</strong> 53 * + page-1 54 * + page-2 55 * + page-n 56 * + <strong>de</strong> 57 * + page-1 58 * + page-2 59 * + page-n 60 * + <strong>de_CH</strong> 61 * + page-1 62 * + page-2 63 * + page-n 64 * + <strong>it</strong> 65 * + page-1 66 * + page-2 67 * + page-n 68 *</pre> 69 * The locale is inferred by analyzing the URI and checking whether it contains a valid Java locale code (see definition of valid <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Locale.html">Java locale</a> code). 70 * This code can be at whatever position in the URI, not necessarily the first one. For example, given the following URI <em>/my-website/node-1/node-2/de/home-page.html</em>, the locale <em>de</em> 71 * will be detected and content served from here: 72 * <pre> 73 * + my-website 74 * + node-1 75 * + node-2 76 * + <strong>de</strong> 77 * + home-page 78 * </pre> 79 * Also the last element in the URI, typically a page, can be used to determine the locale. E.g. given the URI <em>/foo/bar/it.html</em> a <em>it</em> locale will be determined. 80 * If no locale is found in the URI, the default one is assumed. 81 * @author fgrilli 82 * 83 */ 84 public class HierarchyBasedI18nContentSupport extends AbstractI18nContentSupport { 85 86 private static final Logger log = LoggerFactory.getLogger(HierarchyBasedI18nContentSupport.class); 87 @Override 88 protected Locale onDetermineLocale() { 89 Locale locale = null; 90 Locale validUnsupportedLocale = null; 91 final String i18nURI = MgnlContext.getAggregationState().getCurrentURI(); 92 log.debug("URI to check for locales is {}", i18nURI); 93 final String[] splitURI = i18nURI.split("/"); 94 final int lastTokenIdx = splitURI.length - 1; 95 for (int i = 0; i < splitURI.length; i++) { 96 String uriToken = splitURI[i]; 97 if (i == lastTokenIdx) { 98 uriToken = StringUtils.substringBefore(uriToken, "."); 99 } 100 locale = determineLocalFromString(uriToken); 101 if (LocaleUtils.isAvailableLocale(locale)) { 102 log.debug("found a valid Locale code {}", uriToken); 103 if (isLocaleSupported(locale)) { 104 break; 105 } else { 106 // the URI contains a valid Locale code but it is not 107 // supported by the current I18n configuration. 108 // We store it anyway and eventually return it if no exact 109 // match will be found at the end of this loop. 110 validUnsupportedLocale = locale; 111 } 112 } 113 locale = null; 114 } 115 116 return locale != null ? locale : validUnsupportedLocale; 117 } 118 119 /* 120 * Just return the uri, no need to add the locale to it 121 * (non-Javadoc) 122 * @see info.magnolia.cms.i18n.AbstractI18nContentSupport#toI18NURI(java.lang.String, java.util.Locale) 123 */ 124 @Override 125 protected String toI18NURI(String uri, Locale locale) { 126 return uri; 127 } 128 129 /* 130 * Just return the uri, no need to remove the locale from it 131 * (non-Javadoc) 132 * @see info.magnolia.cms.i18n.AbstractI18nContentSupport#toRawURI(java.lang.String, java.util.Locale) 133 */ 134 @Override 135 protected String toRawURI(String i18nURI, Locale locale) { 136 return i18nURI; 137 } 138 139 @Override 140 public NodeData getNodeData(Content node, String name) { 141 // return the node data 142 return node.getNodeData(name); 143 } 144 }