Skip to content

Commit

Permalink
feat(write): Add default value for new translations
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike committed Jun 25, 2023
1 parent 03c987c commit d3d10e4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module.exports = {
// Optional: Default namespace if none given in translation
defaultNamespace: 'common',
// Optional: Namespaces
namespaces: ['common', 'other']
namespaces: ['common', 'other'],
// Default value for new translations
defaultValue: '__MISSING_TRANSLATION__'
}
```
10 changes: 6 additions & 4 deletions src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const generateNewTranslations = async (
const _translationKeys = translationKeys[namespace] || []
const _existingTranslations = existingTranslations[language]?.[namespace]?.translations || {}
for (const translationKey of _translationKeys) {
translations = await writeTranslationStructure(translationKey, _existingTranslations, translations)
translations = await writeTranslationStructure(translationKey, _existingTranslations, translations, options)
}

return translations
Expand All @@ -29,7 +29,8 @@ const mapTranslationStructure = (
export const writeTranslationStructure = async (
translationKey: string,
existingTranslations: TranslationStructure,
translations: TranslationStructure
translations: TranslationStructure,
options: I18nExtractOptions
): Promise<TranslationStructure> => {
const contextIdx = translationKey.indexOf('.')
if (contextIdx > -1) {
Expand All @@ -38,12 +39,13 @@ export const writeTranslationStructure = async (
translations[context] = await writeTranslationStructure(
nestedKey,
mapTranslationStructure(existingTranslations[context]),
mapTranslationStructure(translations[context])
mapTranslationStructure(translations[context]),
options
)
} else if (typeof existingTranslations[translationKey] === 'string') {
translations[translationKey] = existingTranslations[translationKey]
} else {
translations[translationKey] = '__MISSING_TRANSLATION__'
translations[translationKey] = options.defaultValue || '__MISSING_TRANSLATION__'
}
return translations
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface I18nExtractOptions {
languages: Language[]
defaultNamespace?: Namespace
namespaces?: Namespace[]
defaultValue?: string
}
36 changes: 27 additions & 9 deletions tests/merge.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { describe, expect, it } from 'vitest'
import { writeTranslationStructure } from '@/merge'
import { I18nExtractOptions } from '@/types'

/**
* writeTranslationStructure
*/
describe('writeTranslationStructure', () => {
const defaultOptions: I18nExtractOptions = {
input: ['src/**'],
output: 'examples/default/locales/{{lng}}.json',
languages: ['de', 'en-GB']
}

it('should write simple translation key', async () => {
expect(await writeTranslationStructure('key_1', {
key_1: 'Key 1',
key_2: 'Key 2'
}, {})).toStrictEqual({
}, {}, defaultOptions)).toStrictEqual({
key_1: 'Key 1',
})
})
Expand All @@ -19,7 +26,7 @@ describe('writeTranslationStructure', () => {
key_1: 'Key 1'
}, {
key_2: 'Key 2'
})).toStrictEqual({
}, defaultOptions)).toStrictEqual({
key_1: 'Key 1',
key_2: 'Key 2'
})
Expand All @@ -28,17 +35,28 @@ describe('writeTranslationStructure', () => {
it('should write simple translation key with missing translation', async () => {
expect(await writeTranslationStructure('key_1', {
key_2: 'Key 2'
}, {})).toStrictEqual({
}, {}, defaultOptions)).toStrictEqual({
key_1: '__MISSING_TRANSLATION__',
})
})

it('should write simple translation key with missing translation', async () => {
expect(await writeTranslationStructure('key_1', {
key_2: 'Key 2'
}, {}, {
...defaultOptions,
defaultValue: '__NEW_KEY__'
})).toStrictEqual({
key_1: '__NEW_KEY__',
})
})

it('should write missing translation key if different context', async () => {
expect(await writeTranslationStructure('key_1', {
key_1: {
nested: '1'
}
}, {})).toStrictEqual({
}, {}, defaultOptions)).toStrictEqual({
key_1: '__MISSING_TRANSLATION__',
})
})
Expand All @@ -49,7 +67,7 @@ describe('writeTranslationStructure', () => {
nested: 'Nested translation',
other: 'other'
}
}, {})).toStrictEqual({
}, {}, defaultOptions)).toStrictEqual({
context: {
nested: 'Nested translation'
}
Expand All @@ -65,7 +83,7 @@ describe('writeTranslationStructure', () => {
}
}
}
}, {})).toStrictEqual({
}, {}, defaultOptions)).toStrictEqual({
context: {
nested: {
more: {
Expand All @@ -81,7 +99,7 @@ describe('writeTranslationStructure', () => {
context: {
other: 'other'
}
}, {})).toStrictEqual({
}, {}, defaultOptions)).toStrictEqual({
context: {
nested: '__MISSING_TRANSLATION__'
}
Expand All @@ -98,7 +116,7 @@ describe('writeTranslationStructure', () => {
context: {
other: 'other'
}
})).toStrictEqual({
}, defaultOptions)).toStrictEqual({
key: 'key',
context: {
nested: 'Nested translation',
Expand All @@ -114,7 +132,7 @@ describe('writeTranslationStructure', () => {
}
}, {
context: 'Context'
})).toStrictEqual({
}, defaultOptions)).toStrictEqual({
context: {
nested: 'Nested translation'
}
Expand Down

0 comments on commit d3d10e4

Please sign in to comment.