RubyMine 2018.1 Help

Extracting Hard-Coded String Literals

Introduction

RubyMine suggests RailsI18nInspection inspection that detects hard coded string literals in the source code, and intention action that enables you to extract them into properties files, and replace with property keys.

RubyMine recognizes string literals in the files of the various types, in particular:

  • In Ruby and Rails scripts.
  • In views.
  • In HAML files, where string literals can appear without quotes.
  • Inside tags (for example, <p>some text</p>) of HTML files.
  • In Cucumber step definitions.

Extracting string literals

To extract a hardcoded string

  1. Make sure that the locale file to be used as the target, is created.
  2. Open the desired file in the editor, click the string to be extracted, press Alt+Enter, and in the list of available intention actions choose I18n string value:
    ruby i18nExtractHardCodedString1
  3. In the Create Property dialog box, select the target locale file, and specify the new property key and value.
    ruby extractHardCodedStringToProperty

    A key-value pair is created in the specified target locale, and the hardcoded string in question is replaced with reference:

    ruby_i18nExtractHardCodedString.png
Last modified: 1 August 2018