Skip to content

Commit

Permalink
[ts] add rule to test if translation value is longer than original st…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
grasword committed Oct 11, 2023
1 parent 085e0e3 commit 633c428
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/validator/source/rules/value-is-longer-than-default.ts
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.'
}
}
}

0 comments on commit 633c428

Please sign in to comment.