0

I want to implment a maven plugin that can generate i18n resource boundle from source according special emum class with special field commont like this:

public enum MyColor implements I18nAble {

    /**
     * @zh_CN 红色
     * @en_US red
     */
    RED,

    /**
     * @zh_CN 蓝色
     * @en_US blue
     */
    BLUE,

    /**
     * @zh_CN 绿色
     * @en_US green
     */
    GREEN,

    String i18nCode;

    MyColor(String code) {
         this.i18nCode = "color." + code;
    }

    @Override
    public String getCode(){
        return i18nCode;
    }
}

when mvn package, it generate some xxx.properties files like:

color.red=red
color.blue=blue
color.green=green
color.red=红色
color.blue=蓝色
color.green=绿色

Now I have generated keys already, but I can't extract the comment accurately.(I want to use jdk.javadoc.* rather than regex)

jdk: jdk11 not jdk1.8

lym
  • 1

1 Answers1

0

If you have the ability to parse the code, try this sample:

String locale = "@zh_CN";
Pattern pattern = Pattern.compile(locale + "\\s+(.+)\n(.|\\s)*?\\*/\\s+(\\w+)");
Matcher matcher = pattern.matcher(YOUR_SOURCE_CODE_HERE);

while (matcher.find()) {
    String commentColor = matcher.group(1);
    String colorEnum = matcher.group(3).toLowerCase();
    System.out.println(MessageFormat.format("color.{0}={1}", colorEnum, commentColor));
}

Result output:

color.red=红色
color.blue=蓝色
color.green=绿色

To get english comments just set the locale variable to @en_US.

You can test the expression here: https://regexr.com/59mp1

Ilya Lysenko
  • 1,772
  • 15
  • 24