From 633c428faba43d7ee1aaff61a9835f4bd9dce0ff Mon Sep 17 00:00:00 2001 From: Oleh Zhmaiev Date: Wed, 11 Oct 2023 12:14:25 +0200 Subject: [PATCH] [ts] add rule to test if translation value is longer than original string --- .../rules/value-is-longer-than-default.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/validator/source/rules/value-is-longer-than-default.ts diff --git a/packages/validator/source/rules/value-is-longer-than-default.ts b/packages/validator/source/rules/value-is-longer-than-default.ts new file mode 100644 index 0000000..fadd2ff --- /dev/null +++ b/packages/validator/source/rules/value-is-longer-than-default.ts @@ -0,0 +1,48 @@ +import { type LifeCycleHooks, type OnTranslationsHook, type Rule } from '~/rules/utils/types.js' + +const type = 'notice' +const MESSAGE_ID = 'value-is-longer-than-default' +const messages = { + [MESSAGE_ID]: 'Translated string is longer than original string.' +} + +// eslint-disable-next-line jsdoc/require-jsdoc +const create = (): LifeCycleHooks => { + // eslint-disable-next-line jsdoc/require-jsdoc + const onTranslations: OnTranslationsHook = ({ report, filePath, key, translations }) => { + const ORIGINAL_LANG = 'en' + const originalString = translations[ORIGINAL_LANG] + + if (originalString !== undefined) { + const longTranslations = Object.entries(translations).filter((t) => t[1].length > originalString.length) + + if (longTranslations.length > 0) { + longTranslations.forEach((t) => { + report({ + filePath, + key, + type, + value: t[1], + messageId: MESSAGE_ID, + message: `Translated string is longer in locale ${t[0]}` + }) + }) + } + } + } + + return { + onTranslations + } +} + +export const rule: Rule = { + create, + meta: { + type, + messages, + docs: { + description: 'Notify if translation string is longer than original string.' + } + } +}