简体   繁体   English

Android:有没有一种简单的方法可以找到我项目中的所有字符串?

[英]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. 您仍然需要自己进行实际的重构,因为您仍需要命名所有提取的字符串。


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: 您可以运行自定义lint检查 - 仅查找布局文件中的硬编码文本:

  1. Menu -> Analyze -> Inspect Code 菜单 - >分析 - >检查代码
  2. selects desired scope (layouts, for instance) 选择所需的范围(例如布局)

    在此输入图像描述

  3. under 'Inspection profile', select a new custom profile (...) 在“检查配置文件”下,选择一个新的自定义配置文件(...)

  4. Duplicate an existing profile, and un-check all inspections EXCEPT 'HardcodedText' 复制现有配置文件,并取消检查所有检查除“HardcodedText”之外

在此输入图像描述

在此输入图像描述

  1. Run this inspection, and all your hardcoded strings will conveniently be collected into a single list. 运行此检查,所有硬编码字符串将方便地收集到一个列表中。

我相信Eclipse的Google CodePro插件在某处有这个功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android中重命名项目的简便方法 - Easy way to rename a project in android 如何在 Android Studio 的项目中找到所有硬编码字符串 - How can I find all hard coded strings in my project in Android Studio 如何在我的android项目中找到所有String文字并将其移动到strings.xml文件 - How to find all String literal in my android project and move it to strings.xml file 有没有简单的方法来找出Android项目中未使用的字符串? - Is there any simple way to find out unused strings in Android project? Android有一个简单的方法备份您的Android项目? - Android is there a easy way to back up your android project? 是否有一种简单的方法可以使用LibGDX与iOS项目共享Android翻译? - Is there an easy way to share Android translations with an iOS project using LibGDX? 陀螺仪Android轻松实现 - Gyroscope android in a easy way 有没有一种简单的方法将我的苹果应用程序转换为Android应用程序? - Is there an easy way to convert my apple apps into android apps? 如何在 Android Studio IDE 中找到我项目中所有未使用的方法? - How can I find all unused methods of my project in the Android Studio IDE? 我如何构建搜索系统以查找项目中需要android权限的所有引用? - How can I build a search system to find all references that require an android permission in my project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM