no-duplicate-string shouldn't be triggered by i18next's t function calls
Thanaen opened this issue · comments
I want to provide the following feedback.
When using the i18next library to localize texts, we call a function, named 't', with a string as parameter.
For example:
const helloMessage = i18next.t('messages.hello');
With SonarJS, if a single file calls this 't' function three times with the same parameter, it gets reported as a duplicate string.
While this is technically correct, it doesn't seem to make sense to create a constant for such a case.
What do you think about it?
Would it be possible to "whitelist" i18next's 't' calls in a reliable way? Or is it better to simply eslint-ignore those cases?
eslint-plugin-sonarjs version: 0.10.0
eslint version: 7.32.0
Node.js version: 14.18.0
Rule key: no-duplicate-string
Had the same issue and even after spending a lot of time, could not find a solution that I'd be happy about, but here's what I did for now:
Created a single object to hold all the keys, and refer to the object wherever required.
// keys.js
export const content= {
messages: {
hello: "messages.hello",
}
}
// Somefile.js
import { messages } from "keys"
const helloMessage = i18next.t(messages.hello);
With Typescript, I did the following to make it safer against mistyped keys.
import en from './en.json'; // JSON with message strings
type LanguageKey = keyof typeof en
// This is a conveneince function to prevent accidentally typing wrong keys, and will throw a type error in that case
export const k = (key: LanguageKey) => key;
export const content= {
messages: {
hello: k("messages.hello"),
// hello: k("messages.halo") will throw error if the mistyped key 'halo' is not there in the file
}
} as const;
This issue has been migrated to Jira. ESLINTJS-15