-
Notifications
You must be signed in to change notification settings - Fork 737
CONSOLE-5428: follow up la migration du i18next-cli #16926
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
Open
logonoff
wants to merge
2
commits into
openshift:main
Choose a base branch
from
logonoff:i18n-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| find frontend -type d -name 'node_modules' -prune -exec rm -rf {} \; | ||
| rm -rf frontend/.cache-loader | ||
| rm -rf frontend/public/dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| .cache-loader | ||
| .yarn | ||
| node_modules | ||
| public/dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
frontend/i18n-scripts/ConsoleExtensionsI18nextCliPlugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* eslint-disable no-console */ | ||
| import { readFile } from 'node:fs/promises'; | ||
| import chalk from 'chalk'; | ||
| import { parse } from 'comment-json'; | ||
| import type { Plugin } from 'i18next-cli'; | ||
|
|
||
| export interface ConsoleExtensionsI18nextCliPluginOptions { | ||
| /** | ||
| * Paths to the console-extensions.json file, assumed to be encoded as UTF-8. | ||
| * | ||
| * Defaults to `console-extensions.json` in the current working directory. | ||
| */ | ||
| paths?: string[]; | ||
| } | ||
|
|
||
| /** | ||
| * A [i18next-cli] plugin to extract translation keys from `console-extensions.json` files. | ||
| * | ||
| * Keys matching the format `%namespace~key%` are extracted. | ||
| * | ||
| * NOTE: `i18next-cli` does not appear to fully respect semver. Compatibility is only | ||
| * known if you are using the exact same version of `i18next-cli` that Console is using. | ||
| * | ||
| * @returns a [i18next-cli] plugin instance | ||
| * | ||
| * [i18next-cli]: https://github.com/i18next/i18next-cli | ||
| */ | ||
| export const ConsoleExtensionsI18nextCliPlugin = ({ | ||
| paths = ['console-extensions.json'], | ||
| }: ConsoleExtensionsI18nextCliPluginOptions = {}): Plugin => ({ | ||
| name: 'console-extensions', | ||
|
|
||
| async onEnd(keys) { | ||
| const files = await Promise.all( | ||
| paths.map((path) => | ||
| readFile(path, 'utf-8').catch(() => { | ||
| console.warn(chalk.yellowBright(`Warning: Could not read file at ${path}. Skipping.`)); | ||
| return '{}'; | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| for (const [idx, content] of files.entries()) { | ||
| const extracted: { key: string }[] = []; | ||
|
|
||
| try { | ||
| parse( | ||
| content, | ||
| (_key, value) => { | ||
| if (typeof value === 'string') { | ||
| const match = value.match(/^%(.+)%$/); | ||
| if (match && match[1]) { | ||
| extracted.push({ key: match[1] }); | ||
| } | ||
| } | ||
| return value; | ||
| }, | ||
| true, | ||
| ); | ||
| } catch (e) { | ||
| console.error(`Failed to parse ${paths[idx]}:`, e); | ||
| throw e; | ||
| } | ||
|
|
||
| for (const { key: fullKey } of extracted) { | ||
| const sep = fullKey.indexOf('~'); | ||
| if (sep > 0 && sep < fullKey.length - 1) { | ||
| const ns = fullKey.slice(0, sep); | ||
| const key = fullKey.slice(sep + 1); | ||
| keys.set(`${ns}:${key}`, { key, defaultValue: key, ns }); | ||
| } else { | ||
| console.warn(chalk.yellowBright(`Invalid key format in ${paths[idx]}: ${fullKey}`)); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| }); | ||
96 changes: 96 additions & 0 deletions
96
frontend/i18n-scripts/__tests__/ConsoleExtensionsI18nextCliPlugin.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { readFile } from 'node:fs/promises'; | ||
| import type { ExtractedKey } from 'i18next-cli'; | ||
| import { | ||
| ConsoleExtensionsI18nextCliPlugin, | ||
| type ConsoleExtensionsI18nextCliPluginOptions, | ||
| } from '../ConsoleExtensionsI18nextCliPlugin'; | ||
|
|
||
| jest.mock('node:fs/promises', () => ({ | ||
| readFile: jest.fn(), | ||
| })); | ||
|
|
||
| const mockedReadFile = readFile as unknown as jest.Mock; | ||
|
|
||
| function getOnEnd(options?: ConsoleExtensionsI18nextCliPluginOptions) { | ||
| const { onEnd } = ConsoleExtensionsI18nextCliPlugin(options); | ||
| if (!onEnd) { | ||
| throw new Error('onEnd is not defined'); | ||
| } | ||
| return onEnd; | ||
| } | ||
|
|
||
| describe('ConsoleExtensionsI18nextCliPlugin', () => { | ||
| let keys: Map<string, ExtractedKey>; | ||
|
|
||
| beforeEach(() => { | ||
| keys = new Map(); | ||
| mockedReadFile.mockReset(); | ||
| jest.spyOn(console, 'warn').mockImplementation(); | ||
| jest.spyOn(console, 'error').mockImplementation(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should fail gracefully with invalid JSON', async () => { | ||
| mockedReadFile.mockResolvedValue('{"key": "%test~value%", invalid}'); | ||
| const onEnd = getOnEnd({ paths: ['test.json'] }); | ||
| await expect(onEnd(keys)).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should parse strings matching pattern `^%.+%$`', async () => { | ||
| mockedReadFile.mockResolvedValue( | ||
| '{"nope": false, "foo": "%ns~bar%", "test": ["%ns~arr1%", "%ns~arr2%", "arr3"]}', | ||
| ); | ||
| const onEnd = getOnEnd({ paths: ['test.json'] }); | ||
| await onEnd(keys); | ||
| expect([...keys.values()]).toEqual([ | ||
| { key: 'bar', defaultValue: 'bar', ns: 'ns' }, | ||
| { key: 'arr1', defaultValue: 'arr1', ns: 'ns' }, | ||
| { key: 'arr2', defaultValue: 'arr2', ns: 'ns' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should parse json with comments', async () => { | ||
| mockedReadFile.mockResolvedValue( | ||
| `{"nope": false, | ||
| // comment | ||
| "foo": "%ns~bar%", "test": ["%ns~arr1%", | ||
| // comment | ||
| "%ns~arr2%", "arr3"]}`, | ||
| ); | ||
| const onEnd = getOnEnd({ paths: ['test.json'] }); | ||
| await onEnd(keys); | ||
| expect([...keys.values()]).toEqual([ | ||
| { key: 'bar', defaultValue: 'bar', ns: 'ns' }, | ||
| { key: 'arr1', defaultValue: 'arr1', ns: 'ns' }, | ||
| { key: 'arr2', defaultValue: 'arr2', ns: 'ns' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should warn on keys without namespace separator', async () => { | ||
| mockedReadFile.mockResolvedValue('{"key": "%nonamespace%"}'); | ||
| const onEnd = getOnEnd({ paths: ['test.json'] }); | ||
| await onEnd(keys); | ||
| expect(keys.size).toBe(0); | ||
| expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Invalid key format')); | ||
| }); | ||
|
|
||
| it('should treat everything after the first ~ as the key', async () => { | ||
| mockedReadFile.mockResolvedValue('{"key": "%ns~key~suffix%"}'); | ||
| const onEnd = getOnEnd({ paths: ['test.json'] }); | ||
| await onEnd(keys); | ||
| expect([...keys.values()]).toEqual([ | ||
| { key: 'key~suffix', defaultValue: 'key~suffix', ns: 'ns' }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should warn and skip when file cannot be read', async () => { | ||
| mockedReadFile.mockRejectedValue(new Error('ENOENT')); | ||
| const onEnd = getOnEnd({ paths: ['missing.json'] }); | ||
| await onEnd(keys); | ||
| expect(keys.size).toBe(0); | ||
| expect(console.warn).toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.