IntelliJ IDEA 2021.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.

Enable highlighting of hard-coded string literals

  1. Open the Settings / Preferences dialog Ctrl+Alt+S, expand Editor and click 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.

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("awesomeBundle");
  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, 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, 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. To do that, enter a valid expression of the ResourceBundle type in the Resource bundle expression field.

    The Internationalize Hardcoded String dialog
  4. Click OK. The line with the hard-coded string literal is replaced.

    • If the resource bundle has been declared in the source code:

      System.out.println(myBundle.getString("hello.world"));

    • If the resource bundle has been defined in the dialog:

      System.out.println(ResourceBundle.getBundle("awesomeBundle").getString("hello.world"));

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 Java class in your project, and paste 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 the 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 opens without a valid resource bundle expression.

  4. Click Edit i18n template. In the File Templates dialog, change the I18nized Expression to point to the method of your custom resource bundle class.

    File Templates dialog

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

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

    System.out.println(I18nSupport.i18n_str("hello.world"));

Ignoring hard-coded string literals

If you want to ignore a hard-coded string literal, use the Not requiring internationalization annotation.

  1. Press Alt+Enter to show the intention actions for the string literal.

  2. Select Annotate as @NonNls from the list of actions.

  3. Specify the location where you would like to store the annotations.xml file.

Last modified: 06 July 2021