org.thymeleaf.messageresolver.StandardMessageResolutionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thymeleaf Show documentation
Show all versions of thymeleaf Show documentation
Modern server-side Java template engine for both web and standalone environments
/*
* =============================================================================
*
* Copyright (c) 2011-2018, The THYMELEAF team (http://www.thymeleaf.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.thymeleaf.messageresolver;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.thymeleaf.exceptions.TemplateInputException;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.templateresource.ITemplateResource;
import org.thymeleaf.util.StringUtils;
/**
*
* @author Daniel Fernández
*
* @since 3.0.0
*
*/
final class StandardMessageResolutionUtils {
private static final Map EMPTY_MESSAGES = Collections.emptyMap();
private static final String PROPERTIES_FILE_EXTENSION = ".properties";
private static final Object[] EMPTY_MESSAGE_PARAMETERS = new Object[0];
static Map resolveMessagesForTemplate(final ITemplateResource templateResource, final Locale locale) {
// Let the resource tell us about its 'base name'
final String resourceBaseName = templateResource.getBaseName();
if (resourceBaseName == null || resourceBaseName.length() == 0) {
// No way to compute base name -> no messages
return EMPTY_MESSAGES;
}
// Compute all the resource names we should use: *_gl_ES-gheada.properties, *_gl_ES.properties, _gl.properties...
// The order here is important: as we will let values from more specific files overwrite those in less specific,
// (e.g. a value for gl_ES will have more precedence than a value for gl). So we will iterate these resource
// names from less specific to more specific.
final List messageResourceNames = computeMessageResourceNamesFromBase(resourceBaseName, locale);
// Build the combined messages
Map combinedMessages = null;
for (final String messageResourceName : messageResourceNames) {
try {
final ITemplateResource messageResource = templateResource.relative(messageResourceName);
final Reader messageResourceReader = messageResource.reader();
if (messageResourceReader != null) {
final Properties messageProperties = readMessagesResource(messageResourceReader);
if (messageProperties != null && !messageProperties.isEmpty()) {
if (combinedMessages == null) {
combinedMessages = new HashMap(20);
}
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy