Hello everybody,
I am adding i18next-scanner to my Grafana plugin after implementing the i18next internationalization . However, when I run the command npm run node i18n.config.js
, it does not generate the required keys automatically for me in the translation JSON files. Does anyone know what I might be missing?
Here is my i18n.ts
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import en from './locales/en/en.json';
import de from './locales/de/de.json';
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
debug: true,
fallbackLng: 'en',
resources: {
en: {
translation: en,
},
de: {
translation: de,
},
},
ns: ['translation'],
});
export default i18n;
My i18n.config.js
const scanner = require('i18next-scanner');
scanner({
input: ['./src/**/*.ts', './src/**/*.tsx'],
output: './src/locales/{{lng}}/{{lng}}.json',
options: {
debug: true,
removeUnusedKeys: true,
sort: true,
lngs: ['en', 'de'],
nsSeparator: ':',
keySeparator: '.',
},
defaultValue: '',
});
My script at package.json to run it:
"scripts": {
"i18n:scan": "node i18n.config.js",
}
The translations files are at:
./src/locales/en/en.json for english
./src/locales/de/de.json for german and so on…
At my component .tsx file, I am using the translation like:
import React, { useState } from 'react';
import { PanelProps } from '@grafana/data';
import { SimpleOptions } from 'types';
import i18n from 'i18n';
import { useTranslation, Trans } from 'react-i18next';
const lngs: any = {
en: { nativeName: 'English' },
de: { nativeName: 'Deutsch' },
};
interface Props extends PanelProps<SimpleOptions> {}
export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) => {
const { t } = useTranslation();
const [count, setCounter] = useState(0);
return (
<>
<div
className={cx(
styles.wrapper,
css`
width: ${width}px;
height: ${height}px;
`
)}
>
<header className="App-header">
<div>
<p>0- {t('header.part0')}</p>
<p>1- {t('header.part1')}</p>
{Object.keys(lngs).map((lng) => (
<button
key={lng}
style={{
fontWeight: i18n.resolvedLanguage === lng ? 'bold' : 'normal',
}}
type="submit"
onClick={() => {
i18n.changeLanguage(lng);
setCounter(count + 1);
}}
>
{lngs[lng].nativeName}
</button>
))}
</div>
<div>
{/* the variable must be count */}
<p>2- {t('header.part2')}</p>
<i>{t('counter', { count })}</i>
</div>
</header>
</div>
</>
);
};
When I run the script, it does not add any keys to my JSON translation files. Does anyone know why? I do not receive any error messages; it is as if it’s executed but does not find any tasks to perform, and I receive no warnings or errors. I would appreciate it if someone could point out what I am missing.
Cheers,
Turtles