packages/kbn-eslint-plugin-i18n/README.mdx
@kbn/eslint-plugin-i18n is an ESLint plugin providing custom ESLint rules to help validating code in the Kibana repo in the area of translations.
The aim of this package is to help engineers type less and have a nicer experience.
If a rule does not behave as you expect or you have an idea of how these rules can be improved, please reach out to the Observability Knowledge Team or the Kibana Operations team.
@kbn/i18n/strings_should_be_translated_with_i18nThis rule warns engineers to translate their strings by using i18n.translate from the @kbn/i18n package.
It provides an autofix that takes into account the context of the translatable string in the JSX tree to generate a translation ID.
This rule kicks in on:
label and aria-label) which expect a translated value.This code:
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx
import React from 'react';
import { EuiText } from '@elastic/eui';
function MyComponent() {
return (
<EuiText>You know, for search</EuiText>
)
}
will be autofixed with:
import React from 'react';
import { i18n } from '@kbn/i18n';
import { EuiText } from '@elastic/eui';
function MyComponent() {
return (
<EuiText>
{i18n.translate('xpack.observability.myComponent.textLabel', { defaultMessage: 'You know, for search' } )}
</EuiText>
)
}
If i18n has not been imported yet, the autofix will automatically add the import statement as well.
A JSXText element or JSXAttribute label or aria-label of which the value is:
EuiCode or EuiBetaBadge component,!@#$%^&*(){} or numbers,are exempt from this rule.
If this rule kicks in on a string value that you don't like, you can escape it by wrapping the string inside a JSXExpression: {'my escaped value'}.
@kbn/i18n/strings_should_be_translated_with_formatted_messageThis rule warns engineers to translate their strings by using <FormattedMessage> from the @kbn/i18n-react package.
It provides an autofix that takes into account the context of the translatable string in the JSX tree and to generate a translation ID.
This rule kicks in on:
label and aria-label) which expect a translated value.A JSXText element or JSXAttribute label or aria-label of which the value is:
EuiCode or EuiBetaBadge component,!@#$%^&*(){} or numbers,are exempt from this rule.
If this rule kicks in on a string value that you don't like, you can escape it by wrapping the string inside a JSXExpression: {'my escaped value'}.
@kbn/i18n/i18n_translate_should_start_with_the_right_idThis rule checks every instance of i18n.translate() if the first parameter passed:
It checks the repo for the i18nrc.json and /x-pack/i18nrc.json files and determines what the right i18n identifier should be.
If the parameter is missing or does not start with the right i18n identifier, it can autofix the parameter.
This rule is useful when defining translated values in plain functions (non-JSX), but it works in JSX as well.
This code:
// Filename: /x-pack/plugins/observability_solution/observability/public/my_function.ts
function myFunction() {
const translations = [
{
id: 'copy';
label: i18n.translate()
}
]
}
will be autofixed with:
import { i18n } from '@kbn/i18n';
function myFunction() {
const translations = [
{
id: 'copy';
label: i18n.translate('xpack.observability.myFunction.', { defaultMessage: '' })
}
]
}
If i18n has not been imported yet, the autofix will automatically add the import statement as well.