[英]Android: is there an easy way to find all the Strings in my project?
I want to scan my Android project for all hardcoded Strings so I can localize the project, putting the Strings in strings.xml. 我想扫描我的Android项目中的所有硬编码字符串,以便我可以本地化项目,将字符串放在strings.xml中。 I see an option in Eclipse to 'Externalize Strings...' but it isn't specific to Android.
我在Eclipse中看到了一个“Externalize Strings ...”的选项,但它不是特定于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.
我有很多文件,我试图避免手动浏览每一个文件。
I would just open up a terminal window(putty if you are on windows) at the root directory of your project. 我会在你项目的根目录下打开一个终端窗口(如果你在windows上就是putty)。 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. 当然接下来的部分是将Strings实际替换为Strings.xml,也可以编写脚本,但可能需要更多的修补。
I was searching for this myself. 我自己在寻找这个。 There is a nice way of doing this:
有一个很好的方法:
Right click on "src" directory -> Source -> Externalize Strings 右键单击“src”目录 - > 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. 这与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. Ctrl + 1 - > Android - >提取Android字符串。
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"; }
您将获得所有字符串代码及其值在“str”变量中。
Just configure eclipse to show all the string in code: Configuring eclipse to complain for hard coded strings for android xml preferences 只需配置eclipse以显示代码中的所有字符串: 配置eclipse以抱怨android xml首选项的硬编码字符串
And replace them one by one with eclipse refactor 并使用eclipse重构逐个替换它们
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: 您可以运行自定义lint检查 - 仅查找布局文件中的硬编码文本:
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' 复制现有配置文件,并取消检查所有检查除“HardcodedText”之外
我相信Eclipse的Google CodePro插件在某处有这个功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.