Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,32 @@
</distributionManagement>

<properties>
<slf4jVersion>1.7.36</slf4jVersion>
<project.build.outputTimestamp>2020-01-20T18:52:37Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>2.1.1</version>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>2.2.0</version>
<scope>provided</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.0</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>
<version>3.0.0</version>
<artifactId>plexus-testing</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand All @@ -79,6 +81,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
158 changes: 56 additions & 102 deletions src/main/java/org/codehaus/plexus/i18n/DefaultI18N.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,63 @@
* limitations under the License.
*/

import java.lang.reflect.Field;
import javax.inject.Named;
import javax.inject.Singleton;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
*/
@Component(role = I18N.class)
public class DefaultI18N extends AbstractLogEnabled implements I18N, Initializable {
@Named
@Singleton
public class DefaultI18N implements I18N {

private final Logger log = LoggerFactory.getLogger(DefaultI18N.class);
private static final Object[] NO_ARGS = new Object[0];

private HashMap bundles;
private Map<String, Map<Locale, ResourceBundle>> bundles;

private String[] bundleNames;

private String defaultBundleName;

private Locale defaultLocale = Locale.getDefault();

private String defaultLanguage = Locale.getDefault().getLanguage();

private String defaultCountry = Locale.getDefault().getCountry();

private boolean devMode;

public DefaultI18N() {
initialize();
}

public DefaultI18N(String[] bundleNames) {
this.bundleNames = bundleNames != null ? bundleNames.clone() : new String[0];
initialize();
}
// ----------------------------------------------------------------------
// Accessors
// ----------------------------------------------------------------------

public String getDefaultLanguage() {
return defaultLanguage;
return Locale.getDefault().getLanguage();
}

public String getDefaultCountry() {
return defaultCountry;
return Locale.getDefault().getCountry();
}

public String getDefaultBundleName() {
return defaultBundleName;
}

public String[] getBundleNames() {
return (String[]) bundleNames.clone();
return bundleNames.clone();
}

public ResourceBundle getBundle() {
Expand Down Expand Up @@ -111,25 +115,7 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
// ----------------------------------------------------------------------

if (devMode) {
try {
Class klass = ResourceBundle.getBundle(bundleName).getClass().getSuperclass();

Field field = klass.getDeclaredField("cacheList");

field.setAccessible(true);

// SoftCache cache = (SoftCache) field.get( null );
//
// cache.clear();

Object cache = field.get(null);

cache.getClass().getDeclaredMethod("clear", null).invoke(cache, null);

field.setAccessible(false);
} catch (Exception e) {
// Intentional
}
ResourceBundle.clearCache();
}

if (locale == null) {
Expand All @@ -139,13 +125,12 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
// Find/retrieve/cache bundle.
ResourceBundle rb;

HashMap bundlesByLocale = (HashMap) bundles.get(bundleName);
Map<Locale, ResourceBundle> bundlesByLocale = bundles.get(bundleName);

if (bundlesByLocale != null) {
// Cache of bundles by locale for the named bundle exists.
// Check the cache for a bundle corresponding to locale.
rb = (ResourceBundle) bundlesByLocale.get(locale);

rb = bundlesByLocale.get(locale);
if (rb == null) {
// Not yet cached.
rb = cacheBundle(bundleName, locale);
Expand All @@ -161,16 +146,15 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
* @see I18N#getLocale(String)
*/
public Locale getLocale(String header) {
if (!StringUtils.isEmpty(header)) {
if (header != null && !header.isEmpty()) {
I18NTokenizer tok = new I18NTokenizer(header);

if (tok.hasNext()) {
return (Locale) tok.next();
return tok.next();
}
}

// Couldn't parse locale.
return defaultLocale;
return Locale.getDefault();
}

public String getString(String key) {
Expand All @@ -187,7 +171,6 @@ public String getString(String key, Locale locale) {
*/
public String getString(String bundleName, Locale locale, String key) {
String value;

if (locale == null) {
locale = getLocale(null);
}
Expand All @@ -198,11 +181,8 @@ public String getString(String bundleName, Locale locale, String key) {
value = getStringOrNull(rb, key);

// Look for text in list of default bundles.
if (value == null && bundleNames.length > 0) {
String name;
for (int i = 0; i < bundleNames.length; i++) {
name = bundleNames[i];

if (value == null) {
for (String name : bundleNames) {
if (!name.equals(bundleName)) {
rb = getBundle(name, locale);

Expand All @@ -218,27 +198,20 @@ public String getString(String bundleName, Locale locale, String key) {
}

if (value == null) {
String loc = locale.toString();

String mesg =
"Noticed missing resource: " + "bundleName=" + bundleName + ", locale=" + loc + ", key=" + key;

getLogger().debug(mesg);

log.debug("Noticed missing resource: bundleName={}, locale={}, key={}", bundleName, locale, key);
// Just send back the key, we don't need to throw an exception.

value = key;
}

return value;
}

public String format(String key, Object arg1) {
return format(defaultBundleName, defaultLocale, key, new Object[] {arg1});
return format(defaultBundleName, Locale.getDefault(), key, new Object[] {arg1});
}

public String format(String key, Object arg1, Object arg2) {
return format(defaultBundleName, defaultLocale, key, new Object[] {arg1, arg2});
return format(defaultBundleName, Locale.getDefault(), key, new Object[] {arg1, arg2});
}

/**
Expand Down Expand Up @@ -275,28 +248,15 @@ public String format(String bundleName, Locale locale, String key, Object[] args
if (args == null) {
args = NO_ARGS;
}

// FIXME: after switching to JDK 1.4, it will be possible to clean
// this up by providing the Locale along with the string in the
// constructor to MessageFormat. Until 1.4, the following workaround
// is required for constructing the format with the appropriate locale:
MessageFormat messageFormat = new MessageFormat("");
messageFormat.setLocale(locale);
messageFormat.applyPattern(value);

return messageFormat.format(args);
return new MessageFormat(value, locale).format(args);
}

/**
* Called the first time the Service is used.
*/
public void initialize() throws InitializationException {
bundles = new HashMap();

defaultLocale = new Locale(defaultLanguage, defaultCountry);

public void initialize() {
bundles = new HashMap<>();
initializeBundleNames();

if ("true".equals(System.getProperty("PLEXUS_DEV_MODE"))) {
devMode = true;
}
Expand All @@ -307,8 +267,7 @@ public void initialize() throws InitializationException {
// ----------------------------------------------------------------------

protected void initializeBundleNames() {
// System.err.println("cfg=" + getConfiguration());
if (defaultBundleName != null && defaultBundleName.length() > 0) {
if (defaultBundleName != null && !defaultBundleName.isEmpty()) {
// Using old-style single bundle name property.
if (bundleNames == null || bundleNames.length <= 0) {
bundleNames = new String[] {defaultBundleName};
Expand All @@ -333,18 +292,15 @@ protected void initializeBundleNames() {
* @throws MissingResourceException Bundle not found.
*/
private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale) throws MissingResourceException {
HashMap bundlesByLocale = (HashMap) bundles.get(bundleName);

ResourceBundle rb = (bundlesByLocale == null ? null : (ResourceBundle) bundlesByLocale.get(locale));
Map<Locale, ResourceBundle> bundlesByLocale = bundles.get(bundleName);

ResourceBundle rb = (bundlesByLocale == null ? null : bundlesByLocale.get(locale));
if (rb == null) {
bundlesByLocale = (bundlesByLocale == null ? new HashMap(3) : new HashMap(bundlesByLocale));

bundlesByLocale = (bundlesByLocale == null ? new HashMap<>(3) : new HashMap<>(bundlesByLocale));
try {
rb = ResourceBundle.getBundle(bundleName, locale);
} catch (MissingResourceException e) {
rb = findBundleByLocale(bundleName, locale, bundlesByLocale);

if (rb == null) {
throw (MissingResourceException) e.fillInStackTrace();
}
Expand All @@ -353,11 +309,8 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
if (rb != null) {
// Cache bundle.
bundlesByLocale.put(rb.getLocale(), rb);

HashMap bundlesByName = new HashMap(bundles);

Map<String, Map<Locale, ResourceBundle>> bundlesByName = new HashMap<>(bundles);
bundlesByName.put(bundleName, bundlesByLocale);

this.bundles = bundlesByName;
}
}
Expand All @@ -380,30 +333,31 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
* <p>Since we're really just guessing at possible bundles to use,
* we don't ever throw <code>MissingResourceException</code>.</p>
*/
private ResourceBundle findBundleByLocale(String bundleName, Locale locale, Map bundlesByLocale) {
private ResourceBundle findBundleByLocale(
String bundleName, Locale locale, Map<Locale, ResourceBundle> bundlesByLocale) {
ResourceBundle rb = null;

if (!StringUtils.isNotEmpty(locale.getCountry()) && defaultLanguage.equals(locale.getLanguage())) {
/*
category.debug("Requested language '" + locale.getLanguage() +
"' matches default: Attempting to guess bundle " +
"using default country '" + defaultCountry + '\'');
*/
Locale withDefaultCountry = new Locale(locale.getLanguage(), defaultCountry);
rb = (ResourceBundle) bundlesByLocale.get(withDefaultCountry);
if (locale.getCountry() != null
&& !locale.getCountry().isEmpty()
&& Locale.getDefault().getLanguage().equals(locale.getLanguage())) {
Locale withDefaultCountry =
new Locale(locale.getLanguage(), Locale.getDefault().getCountry());
rb = bundlesByLocale.get(withDefaultCountry);
if (rb == null) {
rb = getBundleIgnoreException(bundleName, withDefaultCountry);
}
} else if (!StringUtils.isNotEmpty(locale.getLanguage()) && defaultCountry.equals(locale.getCountry())) {
Locale withDefaultLanguage = new Locale(defaultLanguage, locale.getCountry());
rb = (ResourceBundle) bundlesByLocale.get(withDefaultLanguage);
} else if (locale.getLanguage() != null
&& !locale.getLanguage().isEmpty()
&& Locale.getDefault().getCountry().equals(locale.getCountry())) {
Locale withDefaultLanguage = new Locale(Locale.getDefault().getLanguage(), locale.getCountry());
rb = bundlesByLocale.get(withDefaultLanguage);
if (rb == null) {
rb = getBundleIgnoreException(bundleName, withDefaultLanguage);
}
}

if (rb == null && !defaultLocale.equals(locale)) {
rb = getBundleIgnoreException(bundleName, defaultLocale);
if (rb == null && !Locale.getDefault().equals(locale)) {
rb = getBundleIgnoreException(bundleName, Locale.getDefault());
}

return rb;
Expand Down
Loading