4

I came across a task to internationalise the project which including the code (now 80% of the variable names are in Italian, which need to be changed to English). The translation part is fine, but how to find a way to refactor the code is driving me nuts. Almost every IDE in the market has such refactoring function, but none of them work automatically or programmably.

My question is, do they just use the regex to scan through all the files in the project and then refactor them or follow some sophisticated rules?

10
  • It's not clear what you're asking. If it's "how do I automatically all Italian variable names into English variable names?", then the answer is another question: "How would an IDE be expected to know what substitutions to perform?". Commented Aug 28, 2014 at 22:01
  • 2
    No regexes, don't even try. Eclipse uses ANTLR but what I'd do is get the Eclipse source code, find the refactoring tools in there and see if you can build a plugin that calls them automatically. Writing an Eclipse plugin is relatively easy, so it shouldn't take long. Commented Aug 28, 2014 at 22:01
  • 1
    @OliCharlesworth I understand it: basically OP wants to mass rename fields/methods based on a mapping file. Commented Aug 28, 2014 at 22:02
  • I think IntelliJ has a feature that allows you to extract all hard-coded string literals in your code into a property file, then you can clone it into a second property file for all your Italian translations. See: jetbrains.com/idea/webhelp/… Commented Aug 28, 2014 at 22:04
  • 2
    @biziclop Hmmm... When you use the refactoring tool in Eclipse to rename an identifier, I believe it can find references to that particular variable or method, so that it can rename just those references and avoid renaming others with the same name. But if he wants to do a mass rename based on a file, a tool wouldn't be able to distinguish between two identifiers with the same name anyway. So maybe this could be done simply (maybe even with a 40-year-old tool like sed)? Commented Aug 28, 2014 at 22:07

2 Answers 2

3

I have used a combination of JavaParser and another tool I forget the name of which allowed me to trace usages of all variables in classes from the compiled .class/.jar file.

The JavaParser is certainly a tool that could do the job. I will take a look tomorrow what the other componnet was. Could have been Class Dependency Analyser.

Sign up to request clarification or add additional context in comments.

Comments

3

OP appears to want to do mass renaming.

While not the tool you might expect, you can use our Java obfuscator to achieve this. This solution does mass renaming ignoring scopes, but I suspect that works fine for this.

Many source obfuscators take identifiers and scramble them in a consistent fashion. This means somewhere in the obfuscator, there is map from legacy identifiers to obfuscated identifiers, e.g., "foo" -> "bar" meaning "replace 'foo' by 'bar'".

Our obfuscator reads such a map before it starts (and that's the key to a solution). This map was presumably generated by a previous obfuscation step; reading and reusing such a map ensures that any incremental/additional obfuscation is done in the same way as as the original.

If what you do is run our obfuscator on a set of source files, you get a complete map of every identifier and what it was mapped to (randomly), in a text file looking like (as above):

 foo -> i3718234 fas -> h823478 ... 

If you then run the obfuscator again and feed it the same souces and this map file, it will (by design) map the identifiers the same way, producing the same result as the first time.

To do OPs task, one could run our obfuscator on his code to get:

 italianID1 -> i23432 italianID2 -> g232343 ... 

Using a text editor, he could modify this file to:

 italianID1 -> englishID1 italianID2 -> englishID2 ... 

Names he doesnt want touched are edited (obviously) to this form:

 foo -> foo bar -> bar 

(A shorthand for this, is to simply leave off the "-> id" part. "foo" by itself is interpreted as "foo -> foo").

Now running the obfuscator will rename all the italian IDs to englishIDs.

Check out my bio for a link. Its a commercial tool, but its probably cheaper than a day of OP's labor and I think it solves his problem, waaaaay easier than building his own refactoring tool.

A downside: I think he'll be forced to reformat his code. That's OK, the obfuscator includes a reformatter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.