13

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 Strings...' but it isn't specific to Android.

I know you can refactor individual strings but I'd like to refactor all the Strings at once. I've got a ton of files, and I'm trying to avoid going through each one manually.

Community
  • 1
  • 1
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187

6 Answers6

13

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.

broschb
  • 4,976
  • 4
  • 35
  • 52
  • 3
    You could also just do this from within Eclipse. Choose Search->File. Then paste "[^"\r\n]*" in the text field and make sure "Regular expression" is checked. The search results are better organized this way. – rplankenhorn Jan 14 '14 at 19:12
  • 1
    rplankenhorn's suggestion works well in androidstudio also. cmd+shft+f and select "regular expression" – accordionfolder Jan 29 '14 at 02:37
  • I got this: zsh: no matches found: --include=*.java – Yi Jiang Aug 31 '16 at 03:52
  • @RobertYiJiang my guess is it has to do with using wildcards in zsh, take a look here http://superuser.com/questions/584249/using-wildcards-in-commands-with-zsh – broschb Aug 31 '16 at 04:18
  • I got it on my Mac: grep -r --include="*.java" '"[^"\r\n]*"' ./src > ./strings.txt – Yi Jiang Aug 31 '16 at 06:16
7

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.

Lior Iluz
  • 26,213
  • 16
  • 65
  • 114
Shirane85
  • 2,255
  • 1
  • 26
  • 38
1

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.

Miki
  • 440
  • 4
  • 7
0

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

Community
  • 1
  • 1
shem
  • 4,686
  • 2
  • 32
  • 43
0

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.


EDIT:

This is the shorter and better answer:

  1. Menu -> Analyze -> Run Inspection by name... (even has a shortcut if you feel nimble)
  2. Type "Hardcoded Text", select scope and run.

You can run a custom lint inspections - to find only hardcoded texts in layout files:

  1. Menu -> Analyze -> Inspect Code
  2. selects desired scope (layouts, for instance)

    enter image description here

  3. under 'Inspection profile', select a new custom profile (...)

  4. Duplicate an existing profile, and un-check all inspections EXCEPT 'HardcodedText'

enter image description here

enter image description here

  1. Run this inspection, and all your hardcoded strings will conveniently be collected into a single list.
Mardann
  • 1,953
  • 1
  • 16
  • 23
-1

I believe the Google CodePro addon for Eclipse has this feature in there somewhere

Kurru
  • 14,180
  • 18
  • 64
  • 84