IntelliJ IDEA 2018.1 Help

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:

  1. Open the Settings / Preferences dialog (Ctrl+Alt+S), then click Editor and Inspections.
  2. Select the desired profile, and locate the node Internationalization issues under Java.
  3. Enable the Hard coded strings inspection to highlight hard-coded string literals in the editor.
  4. Apply changes and close the dialog. Now the editor will highlight hard-coded string literals, as shown in the screenshot below:
    Highlighted hard-coded string literal

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:

  1. 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");
  2. Click the highlighted string, press Alt+Enter, and select i18nize hard coded string literal.
    Intention action for hard-coded string literals
  3. 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 the ResourceBundle 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:

    i18nHardcodedString2

    Choose the desired method from the list and press Ctrl+Space once more to fill in the arguments:

    i18nHardcodedString2a

    After that, type the package and resource bundle name in quotes:

    i18nHardcodedString2b
  4. Click OK. The line with the hard-coded string literal is replaced.
    • If the resource bundle has been declared in the source code:
      Internationalized string when resource bundle is declared in source code
    • If the resource bundle has been defined in the dialog:
      Internationalized string when resource bundle is defined in the dialog

To extract a string literal using a custom resource bundle class:

  1. Make sure that redist/annotations.jar archive under your IntelliJ IDEA installation is added to the module dependencies.
  2. 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; } }
  3. In the class that contains hard-coded literal, click the highlighted string, press Alt+Enter, and in the list of intention actions choose i18nize hard coded string literal. The I18nize Hardcoded String dialog box opens without a valid resource bundle expression:
    Internationalize hard-coded string dialog
  4. 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:
    Internationalize hard-coded string dialog

    Click OK to save the updated template and close the dialog box.

  5. In the Preview section of the I18nize Hardcoded String dialog box, verify the suggested substitution, and click OK. The source code changes accordingly:
    Internationalize hard-coded string dialog

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:

  1. Press Alt+Enter to show the intention actions for the string literal:
    Intention actions for hard-coded string
  2. Select Annotate as @NonNls from the list of actions.
  3. 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:

Annotation for ignoring hard-coded strings

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).

Last modified: 4 April 2018