Skip to content

Commit

Permalink
Log warnings if locale is unsupported
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashbot committed Jul 27, 2023
1 parent 3a1ed77 commit 58fbaa3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
38 changes: 19 additions & 19 deletions build-tools/tasks/generate-i18n-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 => {
Expand All @@ -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();
};
15 changes: 15 additions & 0 deletions src/i18n/__tests__/dynamic.test.tsx
Original file line number Diff line number Diff line change
@@ -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`);
});

0 comments on commit 58fbaa3

Please sign in to comment.