private String findMessageByLocale(String key, Locale locale) { String file = messageBasename + (locale == null ? "" : "_" + locale) + messageSuffix; EncodingProperties properties = messageCache.get(file); if ((properties == null || reloadable) && engine.hasResource(file)) { if (properties == null) { properties = new EncodingProperties(); EncodingProperties old = messageCache.putIfAbsent(file, properties); if (old != null) { properties = old; } } try { Resource resource = engine.getResource(file); if (properties.getLastModified() < resource.getLastModified()) { String encoding = (StringUtils.isEmpty(messageEncoding) ? "UTF-8" : messageEncoding); properties.load(resource.openStream(), encoding, resource.getLastModified()); } } catch (IOException e) { if (logger != null && logger.isErrorEnabled()) { logger.error( "Failed to load httl message file " + file + " with locale " + locale + ", cause: " + e.getMessage(), e); } } } if (properties != null) { String value = properties.getProperty(key); if (StringUtils.isNotEmpty(value)) { return value; } } if (locale != null) { return findMessageByLocale(key, LocaleUtils.getParentLocale(locale)); } return null; }
public String message(String key, Locale locale, Object[] args) { if (StringUtils.isEmpty(key) || messageBasename == null) { return key; } if (locale == null) { locale = getLocale(); } String value = findMessageByLocale(key, locale); if (StringUtils.isNotEmpty(value)) { if (args != null && args.length > 0) { if ("string".equals(messageFormat)) { return String.format(value, args); } else { return MessageFormat.format(value, args); } } else { return value; } } return key; }