eslint-plugin-format-message
format-message i18n specific rules for ESLint
Quick Start
Install ESLint and this plugin.
npm install --save eslint eslint-plugin-format-message
Then add plugins
section to your ESLint configuration.
{
"plugins": [
"format-message"
]
}
Configure the rules you want to use. These are the defaults.
{
"rules": {
"format-message/literal-pattern": 1,
"format-message/literal-locale": 1,
"format-message/no-invalid-pattern": 2,
"format-message/no-missing-params": [ 2, { "allowNonLiteral": true } ],
"format-message/translations": 0
}
}
Here is another exmple configuration.
{
"rules": {
"format-message/literal-pattern": 2,
"format-message/literal-locale": 2,
"format-message/no-invalid-pattern": 2,
"format-message/no-missing-params": [ 2, { "allowNonLiteral": false } ],
"format-message/translations": [ 2, {
"keyType": "underscored_crc32",
"missingTranslation": "ignore",
"translations": {
"en": "./locales/en.json",
"es": "./locales/es.json",
"pt": "./locales/pt.json",
"pt-BR": "./locales/pt-BR.json"
}
} ]
}
}
Rules
literal-pattern
For the format-message
tools to replace messages with their translations at build time, as well as optimize runtime performance, the message pattern must be a string literal.
By default this is a warning, since the message can still be translated at run time, if you have configured properly with formatMessage.setup(options)
.
literal-locale
If a locale is specified in formatMessage
calls, it must be a literal string so that the translation can be replaced at build time. Most of the time, no locale should be specified, and the current locale is used.
By default this is a warning, since the message can still be translated at run time, if you have configured properly with formatMessage.setup(options)
.
no-invalid-pattern
The message patterns must be valid ICU Message Format syntax, or the call to formatMessage
will throw an error. This rule allows you to fix these errors early.
Since these problems will cause an error to be thrown at run time, by default this rule reports an error.
no-missing-params
If a message pattern requires parameters, missing those parameters will cause an error or malformed message at run time. This rule helps you to quickly find and fix these problems.
Since these problems can cause errors, by default this rule reports an error.
This rule takes an object for an argument. If the object has a truthy allowNonLiteral
property, then passing a variable instead of an object literal is assumed to have all the necessary parameters.
translations
As you translate the message patterns in your app, it is helpful to verify the patterns to make sure none are missing, or have syntax errors.
This rule requires a fair amount of configuration, and so is disabled by default.
The argument is an object with the following properties
keyType
one of literal
, normalized
, underscored
, or underscored_crc32
. This determines how to translate a pattern to the key to lookup the translation.missingTranslation
either ignore
or warning
. If not ignore
, a warning will be reported when a translation is missing.translations
is an object containing a property per locale.
- The value of each property is a string for the json or javascript file path containing the translations for that locale.
- The required locale file should have a top-level property that matches the locale id, and the key-value translation pairs object as the value.
- The file name is resolved relative to current working directory
- example:
"translations": { "en-US": "./en.json" }
, en.json contains: { "en-US": { "course_8a63b4a3": "Course", "quiz_e0dcce8f": "Quiz" } }
translations
can also be a string for the json or javascript file path containing an object with locale properties.- example:
"translations": "./locales"
, locales.json contains: { "en-US": { "course_8a63b4a3": "Course" }, "pt-BR": { "course_8a63b4a3": "Curso" } }
License
This software is free to use under the MIT license. See the LICENSE-MIT file for license text and copyright information.