如何国际化java源代码?

der*_*ryb 5 eclipse antlr internationalization abstract-syntax-tree

编辑:我完全重写了这个问题,因为在我的前两个版本中我似乎不够清楚.感谢您的建议到目前为止.

我想国际化教程项目的源代码(请注意,而不是运行时应用程序).这是一个例子(在Java中):

/** A comment */
public String doSomething() {
  System.out.println("Something was done successfully");
}
Run Code Online (Sandbox Code Playgroud)

用英语,然后将法语版本改为:

/** Un commentaire */
public String faitQuelqueChose() {
  System.out.println("Quelque chose a été fait avec succès.");
}
Run Code Online (Sandbox Code Playgroud)

等等.然后在某处使用常规工具编辑这些翻译的属性文件,例如:

com.foo.class.comment1=A comment
com.foo.class.method1=doSomething
com.foo.class.string1=Something was done successfully
Run Code Online (Sandbox Code Playgroud)

和其他语言:

com.foo.class.comment1=Un commentaire
com.foo.class.method1=faitQuelqueChose
com.foo.class.string1=Quelque chose a été fait avec succès.
Run Code Online (Sandbox Code Playgroud)

我试图找到最简单,最有效和不显眼的方法,用最少量的手动咕噜声工作(除了明显翻译实际文本).最好在Eclipse下工作.例如,原始代码将用英语编写,然后外部化(属性,最好保持原始源不变),翻译(人工)然后重新生成(作为单独的源文件/项目).

我找到的一些路径(除了AlexS建议的):

我很惊讶没有一个工具可以做到这一点.

Ale*_*exS 2

我会使用唯一的字符串作为方法名称(或任何您想要替换为本地化版本的内容)。

public String m37hod_1() {
  System.out.println(m355a6e_1);
}
Run Code Online (Sandbox Code Playgroud)

然后我会为每种语言定义一个属性文件,如下所示:

m37hod_1=doSomething
m355a6e_1="Something was done successfully"
Run Code Online (Sandbox Code Playgroud)

然后我会编写一个小程序来解析源文件并替换字符串。所以一切都在日食之外。

或者我也会使用 ant 任务Replace和 propertyfiles,而不是独立的翻译程序。像这样的东西:

<replace 
    file="${src}/*.*"
    value="defaultvalue"
    propertyFile="${language}.properties">
  <replacefilter 
    token="m37hod_1" 
    property="m37hod_1"/>
  <replacefilter 
    token="m355a6e_1" 
    property="m355a6e_1"/>
</replace>
Run Code Online (Sandbox Code Playgroud)

使用其中一种方法,您不必在教程中解释任何有关本地化的内容(除非您愿意),而是可以专注于您的真正主题。