How to internationalize Java source code?

EDIT . I completely rewrote the question, as it seems that I was not clear enough in the first two versions. Thanks for the suggestions so far.

I would like to internationalize the source code for a training project (note, not the runtime application). Here is an example (in Java):

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

in English, and then the French version has something like:

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

etc. And then you have something like a properties file somewhere to edit these translations using common tools, such as:

com.foo.class.comment1=A comment
com.foo.class.method1=doSomething
com.foo.class.string1=Something was done successfully

and for other languages:

com.foo.class.comment1=Un commentaire
com.foo.class.method1=faitQuelqueChose
com.foo.class.string1=Quelque chose a été fait avec succès.

, (, , ). Eclipse. , , ( , ), (-), ( /).

, ( AlexS):

, , .

+5
6

( , ).

public String m37hod_1() {
  System.out.println(m355a6e_1);
}

:

m37hod_1=doSomething
m355a6e_1="Something was done successfully"

, . , , .

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>

, - ( , ), .

+2

- .

ANTLR ; , . . "Life After Parsing" . Eclipse "AST" , Eclipse ; , "doSomething" ( ), ( , , , Java ).

DMS Software Reengineering Toolkit . DMS Java ( ), , / ( ).

, , " ", (? Google Translate?), . , ( , , ), , , .

, AST; , . (ANTLR Eclipse ).

, . DMS . , ANTLR Java, Eclipse AST; , .

(, , , ) ; (, , ). , AST , , DMS : , , . :

   pattern local_for_loop_index(i: IDENTIFIER, t: type, e: expression, e2: expression, e3:expression): for_loop_header
         = "for (\t \i = \e,\e2,\e3)"

IDENTIFIER, ; ( , , , DMS node). , 10-20 , .

, - . ; .

. . , , (, ). , ; , , , , . ( DMS ANTLR. , Eclipse ADT , "", , .).

. :

  • , ; , ; DMS , , ,
  • , ? . Java "" , , . ( ,

DMS. , Eclipse AST . , ANTLR , - Java. , , - . YMMV.

, , , "foo.java" "class foo {...}", .java. ( ), , , (DMS ).

, . ( ), ( , ), ; .

+2

, , .

Eclipse, .

  • .
  • :
    • : "" ( "" > " " > "" ) "" > "" (Alt + Shift + R). , .
    • : "" > "", "/*" "//". .
    • :
      • Source > Externalize string, .
      • > "Messages.getString()".
      • .
      • " > /", "//\$NON-NLS-.*\$" .
+1

.properties, :

Locale locale = new Locale(language, country);
ResourceBundle  captions= ResourceBundle.getBundle("Messages",locale);

, Java Messages.properties ( Java)

, Message.properties( ), Messages_de.properties ..

: http://docs.oracle.com/javase/tutorial/i18n/intro/steps.html

, . , getUnternehmen(), , . , .

Javadoc , . SO.

0

/ , java , ResourceBundle. oracle

Eclipse funtionnality ( "Externalize String", ).

, , - , , ...

0

, freemarker templates ( , velocity).

doSomething.tml

/** ${lang['doSomething.comment']} */
public String ${lang['doSomething.methodName']}() {
    System.out.println("${lang['doSomething.message']}");
}

lang_en.prop

doSomething.comment=A comment
doSomething.methodName=doSomething
doSomething.message=Something was done successfully

And then merge the template with each prop language file during build (using Ant / Gradle / Maven, etc.)

0
source

All Articles