diff --git a/contribute/internationalization.md b/contribute/internationalization.md index 5ca730ab835..8729f8175f5 100644 --- a/contribute/internationalization.md +++ b/contribute/internationalization.md @@ -22,14 +22,40 @@ Grafana uses the [i18next](https://www.i18next.com/) framework for managing tran ```jsx import { Trans } from 'app/core/internationalization'; +const SearchTitle = ({ term }) => Results for {{ term }}; +``` + +Prefer using `` for JSX children, and `t()` for props and other JavaScript usage. + +There may be cases where you need to interpolate variables inside other components in the translation. + +If the nested component is displaying the variable only (e.g. to add emphasis or color), the best solution is to create a new wrapping component: + +```jsx +import { Trans } from 'app/core/internationalization'; +import { Text } from '@grafana/ui'; + +const SearchTerm = ({ term }) => {term}; + const SearchTitle = ({ term }) => ( - Results for {{ term }} + Results for ); ``` -Prefer using `` for JSX children, and `t()` for props and other JavaScript usage. +However there are also cases where the nested component might be displaying additional text which also needs to be translated. In this case, you can use the `values` prop to explicitly pass variables to the translation, and reference them as templated strings in the markup. For example: + +```jsx +import { Trans } from 'app/core/internationalization'; +import { Text } from '@grafana/ui'; + +const SearchTitle = ({ term }) => ( + + Results for {'{{ myVariable }}'} and this translated text is also in green + +); +``` When translating in `grafana-ui`, use a relative path to import `` and `t()` from `src/utils/i18n`. diff --git a/packages/grafana-eslint-rules/README.md b/packages/grafana-eslint-rules/README.md index 060db7211d9..2f73d46a529 100644 --- a/packages/grafana-eslint-rules/README.md +++ b/packages/grafana-eslint-rules/README.md @@ -133,16 +133,23 @@ Check if strings are marked for translation. ```tsx // Bad ❌ -const SearchTitle = ({ term }) => ( -
- Results for {term} -
-); +const SearchTitle = ({ term }) =>
Results for {term}
; -//Good ✅ +// Good ✅ +const SearchTitle = ({ term }) => Results for {{ term }}; + +// Good ✅ (if you need to interpolate variables inside nested components) +const SearchTerm = ({ term }) => {term}; const SearchTitle = ({ term }) => ( - Results for {{ term }} + Results for + +); + +// Good ✅ (if you need to interpolate variables and additional translated strings inside nested components) +const SearchTitle = ({ term }) => ( + + Results for {'{{ myVariable }}'} and this translated text is also in green ); ```