Android中如何查找字符串翻译?服务器错误码翻译查询方法
Hey there! Let me break down the practical ways to find the corresponding translation for your server-sent error code strings when you don't have a string ID to work with.
1. Use Android Studio's Global Search (Quickest for One-Off Queries)
This is the simplest approach if you're dealing with a single error code or just a few:
- Open your Android project in Android Studio.
- Press
Ctrl+Shift+F(Windows/Linux) orCmd+Shift+F(Mac) to open the Find in Path dialog. - Enter the exact error code string you received from the server (e.g.,
"ERROR_NETWORK_TIMEOUT"). - In the Scope dropdown, select
Project Filesand narrow it down to theresdirectory (to avoid searching unrelated files). - Check the Match case box if your error code is case-sensitive (which it usually is for string resources).
When you run the search, you'll see results pointing to the <string> tags in your strings.xml files (including localized versions like values-zh/strings.xml if they exist). Each result will show you the string's name (which is effectively the hidden ID you didn't have) and the translated content you need.
2. Write a Helper Function for Bulk Queries
If you have a bunch of error codes to look up, or if you need to do this programmatically at runtime, you can create a quick helper function to scan your app's string resources:
Kotlin Example
fun getTranslationForErrorCode(context: Context, errorCode: String): String? { val resources = context.resources val packageName = context.packageName // Get all string resource entry names from your strings.xml val stringResXmlId = resources.getIdentifier("strings", "xml", packageName) val stringEntryNames = resources.getResourceEntryNames(stringResXmlId) // Iterate through each string resource to find a match for (entryName in stringEntryNames) { val stringResId = resources.getIdentifier(entryName, "string", packageName) val resourceValue = resources.getString(stringResId) // Match the error code to the resource's value (adjust this logic if needed) // If your error code is the string's NAME instead of VALUE, compare entryName to errorCode if (resourceValue == errorCode) { // Return the translated string (uses the device's current locale automatically) return resources.getString(stringResId) } } // Return null or a default message if no match is found return "Unknown error: $errorCode" }
Notes on the Code
- Adjust the matching logic: If your server's error code matches the name of the string resource (e.g., server sends
"err_network_timeout"and your string hasname="err_network_timeout"), replaceresourceValue == errorCodewithentryName.equals(errorCode, ignoreCase = false). - This function automatically uses the device's current locale, so it'll pull the correct translation from the appropriate
values-xx/strings.xmlfile.
3. Manually Scan Resource Files (For Edge Cases)
If for some reason the search isn't working (e.g., the error code is buried in a formatted string or a plural resource), you can manually navigate to your res directory:
- Open
res/values/strings.xml(and localized variants) in the editor. - Use the editor's built-in search (
Ctrl+F/Cmd+F) to look for the error code string. - Pay attention to resources like
<plurals>or<string-array>if your error code might be part of those.
Key Tips
- Case Sensitivity: Android string resource names and values are case-sensitive, so make sure your search matches exactly.
- Localized Resources: Don't forget to check localized
values-xxdirectories if you need translations for specific regions. - Default Fallback: If no translation exists for a locale, Android will automatically fall back to the default
values/strings.xmlcontent.
内容的提问来源于stack exchange,提问作者MiguelSlv

