Tip for familiarizing yourself with the codebase of a GUI application: grep the source code for strings you see in the GUI to find the code that handles that part of the application.
@be Great tip! Works for CLI tools too: Why is it throwing this error? Grep the error message, or parts of the error message.
@taylan What do you use for translations? In Qt, the canonical string goes directly in the source code and the translations are handled separately.
@be It’s the Android SDK.
Strings are defined in XML files, identified by a name attribute. The names become the fields of an auto-generated class, their values being integers that identify the string at runtime and retrieved through the getString() method of a Context instance.
So in src/main/res/values-de/strings.xml I might have:
<string name="settings_title">Einstellungen</string>
And in src/main/java/my/cool/app/ui/SettingsActivity.java I could have:
protected void onCreate() { TextView titleTextView = getView(R.id.settingsTitleTextView); // We inherit from Activity which inherits from Context. // So we can just call getString() directly here. titleTextView.setText(getString(R.string.settings_title)); // Actually TextView.setText() allows a string ID directly for convenience. titleTextView.setText(R.string.settings_title); }