-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ts] add rule to test if translation value is longer than original st…
…ring
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/validator/source/rules/value-is-longer-than-default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.' | ||
} | ||
} | ||
} |