// Set up to load the property resource for this locale key, if we can String name = config.replace('.', '/'); if (localeKey.length() > 0) { name += "_" + localeKey; }
name += ".properties"; InputStream is = null; Properties props = new Properties();
// Load the specified property resource if (log.isTraceEnabled()) { log.trace(" Loading resource '" + name + "'"); }
// Initialize variables we will require String localeKey = localeKey(locale); String originalKey = messageKey(localeKey, key); String messageKey = null; String message = null; int underscore = 0; boolean addIt = false; // Add if not found under the original key
// Loop from specific to general Locales looking for this message while (true) {
// Load this Locale's messages if we have not done so yet loadLocale(localeKey);
// Check if we have this key for the current locale key messageKey = messageKey(localeKey, key); synchronized (messages) { message = (String) messages.get(messageKey); if (message != null) { if (addIt) { messages.put(originalKey, message); } return (message); } }
// Strip trailing modifiers to try a more general locale key addIt = true; underscore = localeKey.lastIndexOf("_"); if (underscore < 0) { break; } localeKey = localeKey.substring(0, underscore);
}
// Try the default locale if the current locale is different if (!defaultLocale.equals(locale)) { localeKey = localeKey(defaultLocale); messageKey = messageKey(localeKey, key); loadLocale(localeKey); synchronized (messages) { message = (String) messages.get(messageKey); if (message != null) { messages.put(originalKey, message); return (message); } } }
// As a last resort, try the default Locale 这里可以看到Struts最后将localeKey赋空 这样资源文件就是$Filename.properties了 localeKey = ""; messageKey = messageKey(localeKey, key); loadLocale这个方法将读取资源文件,填入HashMap 这个方法的代码在上面已经列出来了 loadLocale(localeKey); synchronized (messages) { message = (String) messages.get(messageKey); if (message != null) { messages.put(originalKey, message); return (message); } }