Hard-Coded String Literals
If your source code contains hard-coded string literals, you can enable the Internationalization code inspections to highlight them. You can then extract those strings into properties files for localization or ignore them if they are not meant to be localized.
To enable highlighting of hard-coded string literals:
- Open the Settings / Preferences dialog (Ctrl+Alt+S), then click Editor and Inspections.
- Select the desired profile, and locate the node Internationalization issues under Java.
- Enable the Hard coded strings inspection to highlight hard-coded string literals in the editor.
- Apply changes and close the dialog. Now the editor will highlight hard-coded string literals, as shown in the screenshot below:
Extracting hard-coded string literals
IntelliJ IDEA provides a special intention action i18nize hard coded string literal to extract string literals into your properties files. You can access the resource bundle using either the java.util.ResourceBundle class or a custom resource bundle class.
To extract a string literal using java.util.ResourceBundle:
-
In your source code, specify the resource bundle that will be used to store the extracted literals in the following form:
private static ResourceBundle <field name> = ResourceBundle.getBundle("<bundle name>");
For example:
private static ResourceBundle myBundle = ResourceBundle.getBundle("com.intellij.fontChooser.FontChooser");
- Click the highlighted string, press Alt+Enter, and select .
-
In the I18nize Hard Coded String Literal dialog box, specify the target properties file, the key and value of the property, and the resource bundle expression. If the
ResourceBundle
field has been declared in the source code (as shown in the step 1), IntelliJ IDEA suggests its name by default. If you haven't declared this field in the source code, you can still define the desired expression immediately in the dialog box. To do that, enter a valid expression of theResourceBundle
type in the Resource bundle expression text box.You can use basic code completion in this text box. Type the class name and press Ctrl+Space after the period to select a method:
Choose the desired method from the list and press Ctrl+Space once more to fill in the arguments:
After that, type the package and resource bundle name in quotes:
- Click OK. The line with the hard-coded string literal is replaced.
- If the resource bundle has been declared in the source code:
- If the resource bundle has been defined in the dialog:
To extract a string literal using a custom resource bundle class:
- Make sure that
redist/annotations.jar
archive under your IntelliJ IDEA installation is added to the module dependencies. - Create a new class in your project, and type the following code:
import org.jetbrains.annotations.PropertyKey; import org.jetbrains.annotations.NonNls; import java.util.ResourceBundle; import java.text.MessageFormat; public class I18nSupport { @NonNls private static final ResourceBundle bundle = ResourceBundle.getBundle ("com.intellij.FontChooser"); public static String i18n_str(@PropertyKey(resourceBundle ="com.intellij.FontChooser")String key,Object... params){ String value =bundle.getString(key); if (params.length >0) return MessageFormat.format(value, params); return value; } }
- In the class that contains hard-coded literal, click the highlighted string, press Alt+Enter, and in the list of intention actions choose I18nize Hardcoded String dialog box opens without a valid resource bundle expression: . The
- Click Edit i18n template. In the File Templates dialog box, change the I18nized Expression to point to the method of your custom resource bundle class:
Click OK to save the updated template and close the dialog box.
- In the Preview section of the I18nize Hardcoded String dialog box, verify the suggested substitution, and click OK. The source code changes accordingly:
Ignoring hard-coded string literals
If you want to ignore a hard-coded string literal, use the Not requiring internationalization annotation.
To ignore a certain hard-coded literal:
- Press Alt+Enter to show the intention actions for the string literal:
- Select Annotate as @NonNls from the list of actions.
- In the Select Path dialog that opens, specify the location where you would like to store the
annotations.xml
file.
Alternatively, you can add the @NonNls
annotation:
If you select the Do not show this dialog in the future check box, you can still choose the annotation style in the Settings dialog (Code Style | Java - Code Generation).