From b8c8504231158fb75ae35f324e0e68deaa08a910 Mon Sep 17 00:00:00 2001 From: Vince Ricosti Date: Wed, 1 Sep 2021 13:55:22 +0200 Subject: [PATCH 1/7] When noEmptyTranslation is true, missing keys are added with the key as default translation --- README.md | 5 ++++- src/config-file/vue-i18n-extract.config.ts | 3 ++- src/create-report/index.ts | 5 +++-- src/create-report/language-files.ts | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c253fe80..338beec3 100644 --- a/README.md +++ b/README.md @@ -78,10 +78,13 @@ This will print out a table of missing keys in your language files, as well as u --ci // Boolean // The process will exit with exitCode=1 if at least one translation key is missing or unused (useful if it is part of a CI pipeline). - --separator // String // Use if you want to override the separator used when parsing locale identifiers. Default is `.`. + +--noEmptyTranslation +// Boolean +// Use if you want to generate a default translated string by using the key itself ``` ## Config File diff --git a/src/config-file/vue-i18n-extract.config.ts b/src/config-file/vue-i18n-extract.config.ts index ca3640d5..f9b00fcf 100644 --- a/src/config-file/vue-i18n-extract.config.ts +++ b/src/config-file/vue-i18n-extract.config.ts @@ -6,5 +6,6 @@ export default { add: false, remove: false, ci: false, - separator: '.' + separator: '.', + noEmptyTranslation: false }; diff --git a/src/create-report/index.ts b/src/create-report/index.ts index 8d09d49e..04759f09 100644 --- a/src/create-report/index.ts +++ b/src/create-report/index.ts @@ -13,7 +13,8 @@ export async function createI18NReport (options: ReportOptions): Promise { const languageFileContent = JSON.parse(languageFile.content); missingKeys.forEach(item => { if (item.language && languageFile.fileName.includes(item.language) || !item.language) { - dot.str(item.path, '', languageFileContent); + dot.str(item.path, noEmptyTranslation ? item.path : '', languageFileContent); } }); From 44bd2874606219187642663082432ded97f8e94b Mon Sep 17 00:00:00 2001 From: Vince Ricosti Date: Fri, 3 Sep 2021 10:51:26 +0200 Subject: [PATCH 2/7] Now use a string to specify on which local you want a default translation --- README.md | 5 ++++- src/config-file/vue-i18n-extract.config.ts | 2 +- src/create-report/language-files.ts | 5 +++-- src/types.ts | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 338beec3..971314d5 100644 --- a/README.md +++ b/README.md @@ -83,8 +83,11 @@ This will print out a table of missing keys in your language files, as well as u // Use if you want to override the separator used when parsing locale identifiers. Default is `.`. --noEmptyTranslation -// Boolean +// String // Use if you want to generate a default translated string by using the key itself +// Example 1: '' => DO NOT generate default strings +// Example 2: '*' => generate default string for ALL locales +// Example 3: 'en' => generate default strings ONLY for en locale ``` ## Config File diff --git a/src/config-file/vue-i18n-extract.config.ts b/src/config-file/vue-i18n-extract.config.ts index f9b00fcf..833f2a2f 100644 --- a/src/config-file/vue-i18n-extract.config.ts +++ b/src/config-file/vue-i18n-extract.config.ts @@ -7,5 +7,5 @@ export default { remove: false, ci: false, separator: '.', - noEmptyTranslation: false + noEmptyTranslation: '' }; diff --git a/src/create-report/language-files.ts b/src/create-report/language-files.ts index 2672a74c..2ec68898 100644 --- a/src/create-report/language-files.ts +++ b/src/create-report/language-files.ts @@ -60,13 +60,14 @@ export function extractI18NLanguageFromLanguageFiles (languageFiles: SimpleFile[ }, {}); } -export function writeMissingToLanguageFiles (parsedLanguageFiles: SimpleFile[], missingKeys: I18NItem[], dot: DotObject.Dot = Dot, noEmptyTranslation = false): void { +export function writeMissingToLanguageFiles (parsedLanguageFiles: SimpleFile[], missingKeys: I18NItem[], dot: DotObject.Dot = Dot, noEmptyTranslation = ''): void { parsedLanguageFiles.forEach(languageFile => { const languageFileContent = JSON.parse(languageFile.content); missingKeys.forEach(item => { if (item.language && languageFile.fileName.includes(item.language) || !item.language) { - dot.str(item.path, noEmptyTranslation ? item.path : '', languageFileContent); + const addDefTrans = (noEmptyTranslation) && ((noEmptyTranslation === '*') || (noEmptyTranslation === item.language)); + dot.str(item.path, addDefTrans ? item.path : '', languageFileContent); } }); diff --git a/src/types.ts b/src/types.ts index a078e69b..10324c03 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,6 +6,7 @@ export type ReportOptions = { remove?: boolean; ci?: boolean; separator?: string; + noEmptyTranslation?: string; } export type SimpleFile = { From f4f22f6b141a09de286499dd4fc568394cdbe406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Fri, 13 May 2022 10:18:17 -0400 Subject: [PATCH 3/7] set default value createI18NReport --- src/create-report/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/create-report/index.ts b/src/create-report/index.ts index 5c99d84c..1f14bfc4 100644 --- a/src/create-report/index.ts +++ b/src/create-report/index.ts @@ -15,7 +15,7 @@ export async function createI18NReport (options: ReportOptions): Promise Date: Fri, 13 May 2022 10:30:08 -0400 Subject: [PATCH 4/7] add tests for noEmpyTranslations --- tests/unit/create-report/index.spec.ts | 31 +++++++++++++++++++ .../unit/create-report/language-files.spec.ts | 21 ++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/unit/create-report/index.spec.ts b/tests/unit/create-report/index.spec.ts index 528c09d4..542d9c0e 100644 --- a/tests/unit/create-report/index.spec.ts +++ b/tests/unit/create-report/index.spec.ts @@ -75,6 +75,37 @@ describe('file: create-report/index', () => { languageFileActions.readLanguageFiles(options.languageFiles), expectedI18NReport.missingKeys, Dot, + '', + ); + expect(consoleInfoSpy).toHaveBeenLastCalledWith('\nThe missing keys have been added to your language files.'); + }); + + it('Write missing keys to language files without empty translation', async () => { + options.add = true; + options.noEmptyTranslation = '*'; + const writeMissingSpy: jest.SpyInstance = jest.spyOn(languageFileActions, 'writeMissingToLanguageFiles'); + writeMissingSpy.mockImplementation(() => jest.fn()); + await createI18NReport(options); + expect(writeMissingSpy).toHaveBeenCalledWith( + languageFileActions.readLanguageFiles(options.languageFiles), + expectedI18NReport.missingKeys, + Dot, + '*', + ); + expect(consoleInfoSpy).toHaveBeenLastCalledWith('\nThe missing keys have been added to your language files.'); + }); + + it('Write missing keys to language files without empty translation for a single locale', async () => { + options.add = true; + options.noEmptyTranslation = 'en'; + const writeMissingSpy: jest.SpyInstance = jest.spyOn(languageFileActions, 'writeMissingToLanguageFiles'); + writeMissingSpy.mockImplementation(() => jest.fn()); + await createI18NReport(options); + expect(writeMissingSpy).toHaveBeenCalledWith( + languageFileActions.readLanguageFiles(options.languageFiles), + expectedI18NReport.missingKeys, + Dot, + 'en', ); expect(consoleInfoSpy).toHaveBeenLastCalledWith('\nThe missing keys have been added to your language files.'); }); diff --git a/tests/unit/create-report/language-files.spec.ts b/tests/unit/create-report/language-files.spec.ts index fab6b7bd..f300ce03 100644 --- a/tests/unit/create-report/language-files.spec.ts +++ b/tests/unit/create-report/language-files.spec.ts @@ -33,6 +33,26 @@ describe('file: create-report/language-files', () => { expect(writeFileSyncSpy).toHaveBeenCalledTimes(3); expect(writeFileSyncSpy.mock.calls[0][1]).toContain('missing'); }); + + it('Writes missing keys with no empty translation to language files', () => { + const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync'); + writeFileSyncSpy.mockImplementation(() => jest.fn()); + const dotStrSpy = jest.spyOn(dot, 'str'); + writeMissingToLanguageFiles(readLanguageFiles(languageFiles), expectedI18NReport.missingKeys, dot, '*'); + expect(dotStrSpy).toHaveBeenCalledTimes(78); + expect(writeFileSyncSpy).toHaveBeenCalledTimes(6); + expect(writeFileSyncSpy.mock.calls[0][1]).toContain('missing'); + }); + + it('Writes missing keys with no empty translation for single locale to language files', () => { + const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync'); + writeFileSyncSpy.mockImplementation(() => jest.fn()); + const dotStrSpy = jest.spyOn(dot, 'str'); + writeMissingToLanguageFiles(readLanguageFiles(languageFiles), expectedI18NReport.missingKeys, dot, 'en'); + expect(dotStrSpy).toHaveBeenCalledTimes(117); + expect(writeFileSyncSpy).toHaveBeenCalledTimes(9); + expect(writeFileSyncSpy.mock.calls[0][1]).toContain('missing'); + }); }); describe('function: removeUnusedFromLanguageFiles', () => { @@ -48,4 +68,3 @@ describe('file: create-report/language-files', () => { }); }); }) - From 6bf0dad2c1cc6100ab380e6797dca7becc2a5f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Fri, 13 May 2022 11:00:02 -0400 Subject: [PATCH 5/7] Rename variable to be clearer --- src/create-report/language-files.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/create-report/language-files.ts b/src/create-report/language-files.ts index bb969438..37f02b72 100644 --- a/src/create-report/language-files.ts +++ b/src/create-report/language-files.ts @@ -65,8 +65,8 @@ export function writeMissingToLanguageFiles (parsedLanguageFiles: SimpleFile[], missingKeys.forEach(item => { if (item.language && languageFile.fileName.includes(item.language) || !item.language) { - const addDefTrans = (noEmptyTranslation) && ((noEmptyTranslation === '*') || (noEmptyTranslation === item.language)); - dot.str(item.path, addDefTrans ? item.path : '', languageFileContent); + const addDefaultTranslation = (noEmptyTranslation) && ((noEmptyTranslation === '*') || (noEmptyTranslation === item.language)); + dot.str(item.path, addDefaultTranslation ? item.path : '', languageFileContent); } }); From 2321bdf1d8955c567b6513e9e37e16573a330aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Fri, 13 May 2022 11:01:14 -0400 Subject: [PATCH 6/7] Update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1178f070..9f633b37 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ This will print out a table of missing keys in your language files, as well as u --ci // Boolean // The process will exit with exitCode=1 if at least one translation key is missing or unused (useful if it is part of a CI pipeline). + --separator // String // Use if you want to override the separator used when parsing locale identifiers. Default is `.`. @@ -92,7 +93,7 @@ This will print out a table of missing keys in your language files, as well as u --noEmptyTranslation // String // Use if you want to generate a default translated string by using the key itself -// Example 1: '' => DO NOT generate default strings +// Example 1: '' => DO NOT generate default strings (default) // Example 2: '*' => generate default string for ALL locales // Example 3: 'en' => generate default strings ONLY for en locale ``` From a9b5a0237248756bfb5704f35d5535daba4dfdfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilsen=20Hern=C3=A1ndez?= Date: Fri, 13 May 2022 11:19:07 -0400 Subject: [PATCH 7/7] Add new option to binary --- bin/vue-i18n-extract.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/vue-i18n-extract.js b/bin/vue-i18n-extract.js index d8472c23..538b9609 100755 --- a/bin/vue-i18n-extract.js +++ b/bin/vue-i18n-extract.js @@ -39,6 +39,10 @@ cli '--exclude ', 'Use if you want to exclude a key. It can be used multiple times to exclude any amount of keys on the output' ) + .option( + '--noEmptyTranslation', + 'Use if you want to generate a default translated string by using the key itself' + ) .action((options) => { createI18NReport(resolveConfig(options)); });