From 58fbaa3341fa9f7803dba60b0de1701182ae96d1 Mon Sep 17 00:00:00 2001 From: Avinash Dwarapu Date: Thu, 27 Jul 2023 13:12:39 +0200 Subject: [PATCH] Log warnings if locale is unsupported --- build-tools/tasks/generate-i18n-messages.js | 38 ++++++++++----------- src/i18n/__tests__/dynamic.test.tsx | 15 ++++++++ 2 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 src/i18n/__tests__/dynamic.test.tsx diff --git a/build-tools/tasks/generate-i18n-messages.js b/build-tools/tasks/generate-i18n-messages.js index d04f46498c..cca2958e32 100644 --- a/build-tools/tasks/generate-i18n-messages.js +++ b/build-tools/tasks/generate-i18n-messages.js @@ -8,12 +8,10 @@ const { parse } = require('@formatjs/icu-messageformat-parser'); const { targetPath } = require('../utils/workspace'); const { writeFile } = require('../utils/files'); -const sourceDir = path.resolve(__dirname, '../../src/i18n'); -const sourceMessagesDir = path.resolve(sourceDir, 'messages'); -const destinationDir = path.resolve(targetPath, 'components/i18n'); -const destinationMessagesDir = path.resolve(destinationDir, 'messages'); -const internalDestinationDir = path.resolve(targetPath, 'components/internal/i18n'); -const internalDestinationMessagesDir = path.resolve(internalDestinationDir, 'messages'); +const sourceI18nDir = path.resolve(__dirname, '../../src/i18n'); +const sourceMessagesDir = path.resolve(sourceI18nDir, 'messages'); +const targetI18nDir = path.resolve(targetPath, 'components/i18n'); +const targetMessagesDir = path.resolve(targetI18nDir, 'messages'); const namespace = '@cloudscape-design/components'; const messagesDeclarationFile = `import { I18nProviderProps } from "../provider"; @@ -40,23 +38,21 @@ module.exports = function generateI18nMessages() { allParsedMessages[locale] = { ...(allParsedMessages[locale] ?? {}), ...parsedMessages }; const resultFormat = { [namespace]: { [locale]: parsedMessages } }; - for (const directory of [destinationMessagesDir, internalDestinationMessagesDir]) { - writeFile(path.join(directory, `${subset}.${locale}.json`), JSON.stringify(resultFormat)); - writeFile(path.join(directory, `${subset}.${locale}.d.ts`), messagesDeclarationFile); - writeFile(path.join(directory, `${subset}.${locale}.js`), `export default ${JSON.stringify(resultFormat)}`); - } + writeFile(path.join(targetMessagesDir, `${subset}.${locale}.json`), JSON.stringify(resultFormat)); + writeFile(path.join(targetMessagesDir, `${subset}.${locale}.d.ts`), messagesDeclarationFile); + writeFile(path.join(targetMessagesDir, `${subset}.${locale}.js`), `export default ${JSON.stringify(resultFormat)}`); } // Generate a ".all" file containing all locales. const allResultFormat = { [namespace]: allParsedMessages }; - for (const directory of [destinationMessagesDir, internalDestinationMessagesDir]) { - writeFile(path.join(directory, 'all.all.json'), JSON.stringify(allResultFormat)); - writeFile(path.join(directory, 'all.all.d.ts'), messagesDeclarationFile); - writeFile(path.join(directory, 'all.all.js'), `export default ${JSON.stringify(allResultFormat)}`); - } + writeFile(path.join(targetMessagesDir, 'all.all.json'), JSON.stringify(allResultFormat)); + writeFile(path.join(targetMessagesDir, 'all.all.d.ts'), messagesDeclarationFile); + writeFile(path.join(targetMessagesDir, 'all.all.js'), `export default ${JSON.stringify(allResultFormat)}`); // Generate a dynamic provider function for automatic bundler splitting and imports. const dynamicFile = [ + `import { warnOnce } from '@cloudscape-design/component-toolkit/internal';`, + `import { isDevelopment } from '../internal/is-development';\n`, `export function importMessages(locale) {`, ` switch (locale.toLowerCase()) {`, ...files.flatMap(fileName => { @@ -69,12 +65,16 @@ module.exports = function generateI18nMessages() { ` return import("./messages/${subset}.${locale}.js").then(mod => [mod.default]);`, ]; }), - ` }`, + ` }\n`, + ` if (isDevelopment) {`, + ` warnOnce('importMessages', \`Unknown locale "\${locale}" provided to importMessages\`)`, + ` }\n`, ` return Promise.resolve([]);`, `}`, ].join('\n'); - fs.copyFileSync(path.join(sourceDir, 'dynamic.d.ts'), path.join(destinationDir, 'dynamic.d.ts')); - writeFile(path.join(destinationDir, 'dynamic.js'), dynamicFile); + + fs.copyFileSync(path.join(sourceI18nDir, 'dynamic.d.ts'), path.join(targetI18nDir, 'dynamic.d.ts')); + writeFile(path.join(targetI18nDir, 'dynamic.js'), dynamicFile); return Promise.resolve(); }; diff --git a/src/i18n/__tests__/dynamic.test.tsx b/src/i18n/__tests__/dynamic.test.tsx new file mode 100644 index 0000000000..c28421a370 --- /dev/null +++ b/src/i18n/__tests__/dynamic.test.tsx @@ -0,0 +1,15 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { importMessages } from '../../../lib/components/i18n'; + +afterEach(() => { + jest.restoreAllMocks(); +}); + +it('logs a warning if an unknown locale was provided', async () => { + jest.spyOn(console, 'warn'); + const messages = await importMessages('klh'); + expect(messages).toEqual([]); + expect(console.warn).toHaveBeenCalledWith(`[AwsUi] [importMessages] Unknown locale "klh" provided to importMessages`); +});