Skip to content

Commit

Permalink
Add plural support in translation skipping logic
Browse files Browse the repository at this point in the history
Enhanced the translation skipping functionality to handle plural forms. Added new test cases for plural translations in 'ar' and 'fr' locales. Updated helpers and transformation logic to identify and process plural keys correctly.
  • Loading branch information
Aryan-mor committed Aug 15, 2024
1 parent fe60f53 commit 2a02abc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,5 @@ export {
tsConfigLoader,
yamlConfigLoader,
unescape,
isPlural,
}
7 changes: 4 additions & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
mergeHashes,
transferValues,
makeDefaultSort,
isPlural,
} from './helpers.js'
import Parser from './parser.js'

Expand Down Expand Up @@ -272,14 +273,14 @@ export default class i18nTransform extends Transform {
if (typeof value === 'object') {
skipIdenticalsFromCatalog(value, skipIdenticalsLocales, locale)
} else {
if (shouldSkipKey(catalog, skipIdenticalsLocales, locale)) {
if (!isPlural(key) && shouldSkipKey(catalog, skipIdenticalsLocales, locale)) {
if (value === '') {
delete catalog[key]
} else if (value === key) {
this.warn(
'"' +
key +
'" is identical to value and you may want to remove it'
key +
'" is identical to value and you may want to remove it'
)
}
}
Expand Down
8 changes: 7 additions & 1 deletion test/locales/ar/test_skip_identicals.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
"key2": {
"key3": "ar_translation"
},
"key4": "ar_translation"
"key4": "ar_translation",
"key6_few": "key6_few",
"key6_many": "key6_many",
"key6_one": "key6_one",
"key6_other": "key6_other",
"key6_two": "key6_two",
"key6_zero": "key6_zero"
}
4 changes: 3 additions & 1 deletion test/locales/en/test_skip_identicals.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"key2": {
"key3": "en_translation"
},
"key4": "key4"
"key4": "key4",
"key6_one": "en_Key6 one",
"key6_other": "en_Key6 other"
}
4 changes: 3 additions & 1 deletion test/locales/fr/test_skip_identicals.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"key": "fr_translation",
"key2": {
"key3": "fr_translation"
}
},
"key6_one": "fr_Key6 one",
"key6_other": "fr_Key6 other"
}
18 changes: 17 additions & 1 deletion test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,11 @@ describe('parser', () => {
\n
t('test_skip_identicals:key4')
\n
t('test_skip_identicals:key5')`
t('test_skip_identicals:key5')
\n
t('test_skip_identicals:key6',{count:1})
\n
t('test_skip_identicals:key6',{count:2})`
),
path: 'file.js',
})
Expand Down Expand Up @@ -846,6 +850,9 @@ describe('parser', () => {
key2: {
key3: 'en_translation',
},
key4: 'key4',
key6_one: "en_Key6 one",
key6_other: "en_Key6 other"
})
assert.deepEqual(arResult, {
key: 'ar_translation',
Expand All @@ -854,12 +861,21 @@ describe('parser', () => {
},
key4: 'ar_translation',
key5: '',
key6_few: "key6_few",
key6_many: "key6_many",
key6_one: "key6_one",
key6_other: "key6_other",
key6_two: "key6_two",
key6_zero: "key6_zero",
})
assert.deepEqual(frResult, {
key: 'fr_translation',
key2: {
key3: 'fr_translation',
},
key6_many: "",
key6_one: "fr_Key6 one",
key6_other: "fr_Key6 other"
})
done()
})
Expand Down

0 comments on commit 2a02abc

Please sign in to comment.