1 /** 2 * This file Copyright (c) 2003-2017 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.context.MgnlContext; 37 38 import java.util.Locale; 39 40 import org.apache.commons.lang3.LocaleUtils; 41 import org.apache.commons.lang3.StringUtils; 42 import org.slf4j.Logger; 43 import org.slf4j.LoggerFactory; 44 45 /** 46 * Serves i18n content found in a per locale hierarchy structure. 47 * E.g. 48 * <pre> 49 * +my-website 50 * + <strong>en</strong> 51 * + page-1 52 * + page-2 53 * + page-n 54 * + <strong>de</strong> 55 * + page-1 56 * + page-2 57 * + page-n 58 * + <strong>de_CH</strong> 59 * + page-1 60 * + page-2 61 * + page-n 62 * + <strong>it</strong> 63 * + page-1 64 * + page-2 65 * + page-n 66 * </pre> 67 * 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). 68 * 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> 69 * will be detected and content served from here: 70 * <pre> 71 * + my-website 72 * + node-1 73 * + node-2 74 * + <strong>de</strong> 75 * + home-page 76 * </pre> 77 * 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. 78 * If no locale is found in the URI, the default one is assumed. 79 */ 80 public class HierarchyBasedI18nContentSupport extends AbstractI18nContentSupport { 81 82 private static final Logger log = LoggerFactory.getLogger(HierarchyBasedI18nContentSupport.class); 83 84 @Override 85 protected Locale onDetermineLocale() { 86 Locale locale = null; 87 Locale validUnsupportedLocale = null; 88 final String i18nURI = MgnlContext.getAggregationState().getCurrentURI(); 89 log.debug("URI to check for locales is {}", i18nURI); 90 final String[] splitURI = i18nURI.split("/"); 91 final int lastTokenIdx = splitURI.length - 1; 92 for (int i = 0; i < splitURI.length; i++) { 93 String uriToken = splitURI[i]; 94 if (i == lastTokenIdx) { 95 uriToken = StringUtils.substringBefore(uriToken, "."); 96 } 97 locale = determineLocalFromString(uriToken); 98 if (LocaleUtils.isAvailableLocale(locale)) { 99 log.debug("found a valid Locale code {}", uriToken); 100 if (isLocaleSupported(locale)) { 101 break; 102 } 103 // the URI contains a valid Locale code but it is not 104 // supported by the current I18n configuration. 105 // We store it anyway and eventually return it if no exact 106 // match will be found at the end of this loop. 107 validUnsupportedLocale = locale; 108 } 109 locale = null; 110 } 111 112 return locale != null ? locale : validUnsupportedLocale; 113 } 114 115 /* 116 * Just return the uri, no need to add the locale to it 117 * (non-Javadoc) 118 * @see info.magnolia.cms.i18n.AbstractI18nContentSupport#toI18NURI(java.lang.String, java.util.Locale) 119 */ 120 @Override 121 protected String toI18NURI(String uri, Locale locale) { 122 return uri; 123 } 124 125 /* 126 * Just return the uri, no need to remove the locale from it 127 * (non-Javadoc) 128 * @see info.magnolia.cms.i18n.AbstractI18nContentSupport#toRawURI(java.lang.String, java.util.Locale) 129 */ 130 @Override 131 protected String toRawURI(String i18nURI, Locale locale) { 132 return i18nURI; 133 } 134 135 }