org.nervousync.i18n.MessageResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.nervousync.i18n;
import jakarta.annotation.Nonnull;
import org.nervousync.commons.Globals;
import org.nervousync.utils.ConvertUtils;
import org.nervousync.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
/**
* Internationalization Message Resource Define
* 国际化信息资源定义
*.0
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $ $Date: Jul 19, 2023 16:39:41 $
*/
public final class MessageResource {
/**
* Logger instance
* 日志实例
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Resource information map
* 资源信息映射表
*/
private final Map resourcesMap;
/**
* Cached resource formatter map
* 缓存的资源信息格式化器实例映射表
*/
private final Map cachedFormaterMap;
/**
* Constructor for Resource
* 国际化资源的构造方法
*
* @param properties Resource information properties instance
* 资源信息属性实例对象
*/
public MessageResource(@Nonnull final Properties properties) {
this.resourcesMap = ConvertUtils.toMap(properties);
this.cachedFormaterMap = new HashMap<>();
}
/**
* Update resource messages
* 更新国际化信息内容
*
* @param properties Resource information properties instance
* 资源信息属性实例对象
*/
public void updateResource(@Nonnull final Properties properties) {
ConvertUtils.toMap(properties)
.forEach((key, value) -> {
if (this.resourcesMap.containsKey(key)) {
this.cachedFormaterMap.remove(key);
this.logger.warn("Override resource key: {}, original value: {}, new value: {}",
key, this.resourcesMap.get(key), value);
}
this.resourcesMap.put(key, value);
});
}
/**
* Retrieve internationalization information content and formatted by given collections
* 读取国际化资源信息详情并使用给定的参数集合格式化资源信息
*
* @param messageKey Message identify key
* 信息识别键值
* @param collections given parameters of information formatter
* 用于资源信息格式化的参数
*
* @return Formatted resource information or empty string if not found
* 格式化的资源信息,如果未找到则返回空字符串
*/
public String findMessage(final String messageKey, final Object... collections) {
return Optional.ofNullable(this.retrieveFormatter(messageKey))
.map(messageFormat -> messageFormat.format(collections))
.orElse(Globals.DEFAULT_VALUE_STRING);
}
/**
* Retrieve message formatter instance
* 读取信息格式化器实例对象
*
* @param messageKey Message identify key
* 信息识别键值
*
* @return Retrieved message formatter instance or null
if argument messageKey is null
or empty string
* 读取的信息格式化器实例对象,如果参数 messageKey 未找到或为空字符串,则返回 null
*/
private MessageFormat retrieveFormatter(final String messageKey) {
MessageFormat messageFormat = null;
if (StringUtils.notBlank(messageKey) && this.resourcesMap.containsKey(messageKey)) {
if (this.cachedFormaterMap.containsKey(messageKey)) {
messageFormat = this.cachedFormaterMap.get(messageKey);
} else {
messageFormat = new MessageFormat(this.resourcesMap.get(messageKey));
this.cachedFormaterMap.put(messageKey, messageFormat);
}
}
return messageFormat;
}
}