Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.language;
import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.cache.PortalCache;
import com.liferay.portal.kernel.cache.PortalCacheHelperUtil;
import com.liferay.portal.kernel.cache.PortalCacheManagerNames;
import com.liferay.portal.kernel.cache.PortalCacheMapSynchronizeUtil;
import com.liferay.portal.kernel.cache.PortalCacheMapSynchronizeUtil.Synchronizer;
import com.liferay.portal.kernel.cookies.CookiesManagerUtil;
import com.liferay.portal.kernel.cookies.constants.CookiesConstants;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.language.Language;
import com.liferay.portal.kernel.language.LanguageWrapper;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.CompanyConstants;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.GroupConstants;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.resource.bundle.ResourceBundleLoader;
import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.FastDateFormatConstants;
import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ObjectValuePair;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Time;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsValues;
import java.io.Serializable;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.portlet.PortletConfig;
import javax.portlet.PortletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Provides various translation related functionalities for language keys
* specified in portlet configurations and portal resource bundles.
*
*
* You can disable translations by setting the
* translations.disabled property to true in
* portal.properties.
*
*
*
* Depending on the context passed into these methods, the lookup might be
* limited to the portal's resource bundle (e.g. when only a locale is passed),
* or extended to include an individual portlet's resource bundle (e.g. when a
* request object is passed). A portlet's resource bundle overrides the portal's
* resources when both are present.
*
*
* @author Brian Wing Shun Chan
* @author Andrius Vitkauskas
* @author Eduardo Lundgren
*/
public class LanguageImpl implements Language, Serializable {
public void afterPropertiesSet() {
_companyLocalesPortalCache = PortalCacheHelperUtil.getPortalCache(
PortalCacheManagerNames.MULTI_VM,
_COMPANY_LOCALES_PORTAL_CACHE_NAME);
PortalCacheMapSynchronizeUtil.synchronize(
_companyLocalesPortalCache, _companyLocalesBags,
_removeSynchronizer);
_groupLocalesPortalCache = PortalCacheHelperUtil.getPortalCache(
PortalCacheManagerNames.MULTI_VM, _GROUP_LOCALES_PORTAL_CACHE_NAME);
PortalCacheMapSynchronizeUtil.synchronize(
_groupLocalesPortalCache, _groupLanguageCodeLocalesMapMap,
_removeSynchronizer);
PortalCacheMapSynchronizeUtil.synchronize(
_groupLocalesPortalCache, _groupLanguageIdLocalesMap,
_removeSynchronizer);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholder (e.g. {0}) is replaced with the
* argument, following the standard Java {@link ResourceBundle} notion of
* index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param argument the single argument to be substituted into the pattern
* and translated, if possible
* @return the translated pattern, with the argument substituted in for the
* pattern's placeholder
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
LanguageWrapper argument) {
return format(
httpServletRequest, pattern, new LanguageWrapper[] {argument},
true);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholder (e.g. {0}) is replaced with the
* argument, following the standard Java {@link ResourceBundle} notion of
* index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param argument the single argument to be substituted into the pattern
* and translated, if possible
* @param translateArguments whether the argument is translated
* @return the translated pattern, with the argument substituted in for the
* pattern's placeholder
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
LanguageWrapper argument, boolean translateArguments) {
return format(
httpServletRequest, pattern, new LanguageWrapper[] {argument},
translateArguments);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholders (e.g. {0}, {1},
* {2}, etc.) are replaced with the arguments, following the
* standard Java {@link ResourceBundle} notion of index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param arguments the arguments to be substituted into the pattern and
* translated, if possible
* @return the translated pattern, with the arguments substituted in for the
* pattern's placeholders
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
LanguageWrapper[] arguments) {
return format(httpServletRequest, pattern, arguments, true);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholders (e.g. {0}, {1},
* {2}, etc.) are replaced with the arguments, following the
* standard Java {@link ResourceBundle} notion of index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param arguments the arguments to be substituted into the pattern
* @param translateArguments whether the arguments are translated
* @return the translated pattern, with the arguments substituted in for the
* pattern's placeholders
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
LanguageWrapper[] arguments, boolean translateArguments) {
if (PropsValues.TRANSLATIONS_DISABLED) {
return pattern;
}
String value = null;
try {
pattern = get(httpServletRequest, pattern);
if (ArrayUtil.isNotEmpty(arguments)) {
Object[] formattedArguments = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
if (translateArguments) {
formattedArguments[i] = StringBundler.concat(
arguments[i].getBefore(),
get(httpServletRequest, arguments[i].getText()),
arguments[i].getAfter());
}
else {
formattedArguments[i] = StringBundler.concat(
arguments[i].getBefore(), arguments[i].getText(),
arguments[i].getAfter());
}
}
value = _decorateMessageFormat(
httpServletRequest, pattern, formattedArguments);
}
else {
value = pattern;
}
}
catch (Exception exception) {
if (_log.isWarnEnabled()) {
_log.warn(exception);
}
}
return value;
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholder (e.g. {0}) is replaced with the
* argument, following the standard Java {@link ResourceBundle} notion of
* index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param argument the single argument to be substituted into the pattern
* and translated, if possible
* @return the translated pattern, with the argument substituted in for the
* pattern's placeholder
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
Object argument) {
return format(
httpServletRequest, pattern, new Object[] {argument}, true);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholder (e.g. {0}) is replaced with the
* argument, following the standard Java {@link ResourceBundle} notion of
* index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param argument the single argument to be substituted into the pattern
* and translated, if possible
* @param translateArguments whether the argument is translated
* @return the translated pattern, with the argument substituted in for the
* pattern's placeholder
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern, Object argument,
boolean translateArguments) {
return format(
httpServletRequest, pattern, new Object[] {argument},
translateArguments);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholders (e.g. {0}, {1},
* {2}, etc.) are replaced with the arguments, following the
* standard Java {@link ResourceBundle} notion of index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param arguments the arguments to be substituted into the pattern and
* translated, if possible
* @return the translated pattern, with the arguments substituted in for the
* pattern's placeholders
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
Object[] arguments) {
return format(httpServletRequest, pattern, arguments, true);
}
/**
* Returns the translated pattern using the current request's locale or, if
* the current request locale is not available, the server's default locale.
*
*
* The lookup is done on the portlet configuration first, and if it's not
* found, it is done on the portal's resource bundle. If a translation for a
* given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholders (e.g. {0}, {1},
* {2}, etc.) are replaced with the arguments, following the
* standard Java {@link ResourceBundle} notion of index based substitution.
*
*
* @param httpServletRequest the request used to determine the current
* locale
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param arguments the arguments to be substituted into the pattern
* @param translateArguments whether the arguments are translated
* @return the translated pattern, with the arguments substituted in for the
* pattern's placeholders
*/
@Override
public String format(
HttpServletRequest httpServletRequest, String pattern,
Object[] arguments, boolean translateArguments) {
if (PropsValues.TRANSLATIONS_DISABLED) {
return pattern;
}
String value = null;
try {
pattern = get(httpServletRequest, pattern);
if (ArrayUtil.isNotEmpty(arguments)) {
for (int i = 0; i < arguments.length; i++) {
if (translateArguments) {
arguments[i] = get(
httpServletRequest, arguments[i].toString());
}
}
value = _decorateMessageFormat(
httpServletRequest, pattern, arguments);
}
else {
value = pattern;
}
}
catch (Exception exception) {
if (_log.isWarnEnabled()) {
_log.warn(exception);
}
}
return value;
}
/**
* Returns the translated pattern using the locale or, if the locale is not
* available, the server's default locale.
*
*
* The lookup is done on the portal's resource bundle. If a translation for
* a given key does not exist, this method returns the requested key as the
* translation.
*
*
*
* The substitute placeholders (e.g. {0}, {1},
* {2}, etc.) are replaced with the arguments, following the
* standard Java {@link ResourceBundle} notion of index based substitution.
*
*
* @param locale the locale to translate to
* @param pattern the key to look up in the current locale's resource file.
* The key follows the standard Java resource specification.
* @param arguments the arguments to be substituted into the pattern
* @return the translated pattern, with the arguments substituted in for the
* pattern's placeholders
*/
@Override
public String format(
Locale locale, String pattern, List