mojohaus / jaxb2-maven-plugin

JAXB2 Maven Plugin
https://www.mojohaus.org/jaxb2-maven-plugin/
Apache License 2.0
106 stars 77 forks source link

Local setting 'zh_CN' error #112

Closed BertOnline closed 6 years ago

BertOnline commented 6 years ago

When the local setting in the configuration tab is 'zh' or 'zh_CN' or 'zh_TW', the generated source comment is in English. org.codehaus.mojo.jaxb2.shared.environment.locale.LocaleFacet#createFor method returns 'zh_cn' instead of 'zh_CN', so the constructor will set the property 'newLocale' to 'zh_cn', causing the Locale to fail to match the 'zh_CN' and eventually using the default value 'en'.

BertOnline commented 6 years ago

The problematic method:

public static LocaleFacet createFor(final String localeString, final Log log) throws MojoExecutionException {
    // Check sanity
    Validate.notNull(log, "log");
    Validate.notEmpty(localeString, "localeString");

    final StringTokenizer tok = new StringTokenizer(localeString, ",", false);
    final int numTokens = tok.countTokens();
    if (numTokens > 3 || numTokens == 0) {
        throw new MojoExecutionException("A localeString must consist of up to 3 comma-separated parts on the "
                + "form <language>[,<country>[,<variant>]]. Received incorrect value '" + localeString + "'");
    }

    Locale locale = null;
    switch (numTokens) {
        case 3:
            locale = new Locale(tok.nextToken().trim(), tok.nextToken().trim(), tok.nextToken().trim());
            break;

        case 2:
            locale = new Locale(tok.nextToken().trim(), tok.nextToken().trim());
            break;

        default:
            locale = new Locale(tok.nextToken().trim());
            break;
    }

    // All done.
    return new LocaleFacet(log, locale);
}
BertOnline commented 6 years ago

Temporary solution:

public static LocaleFacet createFor(final String localeString, final Log log) throws MojoExecutionException {

    // Check sanity
    Validate.notNull(log, "log");
    Validate.notEmpty(localeString, "localeString");

    final StringTokenizer tok = new StringTokenizer(localeString, ",", false);
    final int numTokens = tok.countTokens();
    if (numTokens > 3 || numTokens == 0) {
        throw new MojoExecutionException("A localeString must consist of up to 3 comma-separated parts on the "
                + "form <language>[,<country>[,<variant>]]. Received incorrect value '" + localeString + "'");
    }

    Locale locale = null;
    switch (numTokens) {
        case 3:
            locale = new Locale(tok.nextToken().trim(), tok.nextToken().trim(), tok.nextToken().trim());
            break;

        case 2:
            locale = new Locale(tok.nextToken().trim(), tok.nextToken().trim());
            break;

        default:
            locale = new Locale(tok.nextToken().trim());
            break;
    }

    // ###############################
    if ("zh_CN".equals(localeString) || "zh".equals(localeString)) {
        locale = Locale.SIMPLIFIED_CHINESE;
    }
    if ("zh_TW".equals(localeString)) {
        locale = Locale.TRADITIONAL_CHINESE;
    }
    // ###############################

    // All done.
    return new LocaleFacet(log, locale);
}

}

BertOnline commented 6 years ago

The root cause is the java.util.Locale#convertOldISOCodes method, language = LocaleUtils.toLowerString(language).intern();

lennartj commented 6 years ago

This is a problematic implementation within the JDK, as far as I can tell. However, this is a fairly simple problem to solve.