I want to scan my Android project for all hardcoded Strings so I can localize the project, putting the Strings in strings.xml. I see an option in Eclipse to \'Externalize String
I was searching for this myself. There is a nice way of doing this:
Right click on "src" directory -> Source -> Externalize Strings
This will find all Non-Externalize Strings and let you Externalize them.
Hope this will help :)
Edit:
This is not same as the Android -> Extract Android String.
But it can help you find all the Non-Externalize strings. Then, on each string you will have to perform:
Ctrl + 1 -> Android -> Extract Android String.
Field[] fields = R.string.class.getDeclaredFields(); // or Field[] fields = R.string.class.getFields();
String str = "";
for (int i =0; i < fields.length; i++) {
int resId = getResources().getIdentifier(fields[i].getName(), "string", getPackageName());
str += fields[i].getName() + " = ";
if (resId != 0) {
str += getResources().getString(resId);
}
str += "\n";
}
You will get all codes of strings with its values in "str" variable.
I believe the Google CodePro addon for Eclipse has this feature in there somewhere
NOTE: This solutions can help with HALF your problem- finding all hardcoded strings. You will still need to do the actual refactoring yourself because you still need to name all extracted strings.
This is the shorter and better answer:
You can run a custom lint inspections - to find only hardcoded texts in layout files:
selects desired scope (layouts, for instance)
under 'Inspection profile', select a new custom profile (...)
Duplicate an existing profile, and un-check all inspections EXCEPT 'HardcodedText'
I would just open up a terminal window(putty if you are on windows) at the root directory of your project. You can then run something like the following:
grep -r --include=*.java '="[^"\r\n]*"' ./* --to look for something like this String test="text";, a string with =""
or
grep -r --include=*.java '"[^"\r\n]*"' ./* -- to look for any string, anything enclosed in ""
Of course the next part is to actually substitute the Strings into the Strings.xml, that could be scripted out as well, but may take more tinkering.
Just configure eclipse to show all the string in code: Configuring eclipse to complain for hard coded strings for android xml preferences
And replace them one by one with eclipse refactor