Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to skip identical keys for specified locales #1057

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# X.X.X

- Add skipIdenticals option #1036

# 9.0.2

- Fix cheerio dependency #1045
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ export default {
// {
// lineWidth: -1,
// }

skipIdenticals: [],
// An array of locales to skip adding entries which their keys are identical to their values or their values are empty string.
}
```

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,5 @@ export interface UserConfig {
resetDefaultValueLocale?: string | null
i18nextOptions?: Record<string, unknown> | null
yamlOptions?: Record<string, unknown> | null
skipIdenticals?: string[]
}
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,
}
43 changes: 43 additions & 0 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 @@ -40,6 +41,7 @@ export default class i18nTransform extends Transform {
customValueTemplate: null,
failOnWarnings: false,
yamlOptions: null,
skipIdenticals: [],
}

this.options = { ...this.defaults, ...options }
Expand Down Expand Up @@ -251,6 +253,47 @@ export default class i18nTransform extends Transform {
resetValues[namespace]
)

const shouldSkipKey = (entry, skipIdenticals, local) => {
return skipIdenticals.some((skipLocale) => {
return local === skipLocale
})
}

const skipIdenticalsFromCatalog = (
catalog,
skipIdenticalsLocales,
locale
) => {
if (
typeof skipIdenticalsLocales !== 'object' ||
skipIdenticalsLocales.length === 0
)
return
for (const [key, value] of Object.entries(catalog)) {
if (typeof value === 'object') {
skipIdenticalsFromCatalog(value, skipIdenticalsLocales, locale)
} else {
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'
)
}
}
}
}
}

skipIdenticalsFromCatalog(
newCatalog,
this.options.skipIdenticals,
locale
)

// record values to be reset
// assumes that the 'default' namespace is processed first
if (resetAndFlag && !resetValues[namespace]) {
Expand Down
13 changes: 13 additions & 0 deletions test/locales/ar/test_skip_identicals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"key": "ar_translation",
"key2": {
"key3": "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"
}
9 changes: 9 additions & 0 deletions test/locales/en/test_skip_identicals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"key": "en_translation",
"key2": {
"key3": "en_translation"
},
"key4": "key4",
"key6_one": "en_Key6 one",
"key6_other": "en_Key6 other"
}
8 changes: 8 additions & 0 deletions test/locales/fr/test_skip_identicals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"key": "fr_translation",
"key2": {
"key3": "fr_translation"
},
"key6_one": "fr_Key6 one",
"key6_other": "fr_Key6 other"
}
80 changes: 80 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,86 @@ describe('parser', () => {
i18nextParser.end(fakeFile)
})

it('skips identical keys for specified locales', (done) => {
const i18nextParser = new i18nTransform({
output: 'test/locales/$LOCALE/$NAMESPACE.json',
locales: ['en', 'ar', 'fr'],
skipIdenticals: ['en', 'fr'],
})

const fakeFile = new Vinyl({
contents: Buffer.from(
`t('test_skip_identicals:key')
\n
t('test_skip_identicals:key2.key3')
\n
t('test_skip_identicals:key4')
\n
t('test_skip_identicals:key5')
\n
t('test_skip_identicals:key6',{count:1})
\n
t('test_skip_identicals:key6',{count:2})`
),
path: 'file.js',
})

let enResult, arResult, frResult
i18nextParser.on('data', (file) => {
if (
file.relative.endsWith(path.normalize('en/test_skip_identicals.json'))
) {
enResult = JSON.parse(file.contents)
} else if (
file.relative.endsWith(path.normalize('ar/test_skip_identicals.json'))
) {
arResult = JSON.parse(file.contents)
} else if (
file.relative.endsWith(path.normalize('fr/test_skip_identicals.json'))
) {
frResult = JSON.parse(file.contents)
}
})

i18nextParser.once('end', () => {
assert.deepEqual(enResult, {
key: 'en_translation',
key2: {
key3: 'en_translation',
},
key4: 'key4',
key6_one: "en_Key6 one",
key6_other: "en_Key6 other"
})
assert.deepEqual(arResult, {
key: 'ar_translation',
key2: {
key3: 'ar_translation',
},
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()
})

i18nextParser.end(fakeFile)
})

describe('options', () => {
describe('resetDefaultValueLocale', () => {
it('will not reset (nested) keys if a default value has not changed in the locale', (done) => {
Expand Down