Skip to content

Commit

Permalink
Add i18n checks to PR workflows (#8411)
Browse files Browse the repository at this point in the history
* Ignore missing `formats` while checking locale files

Also:
* Add help text and description to `i18n-check`
* Fix malformed translations

Signed-off-by: Miki <[email protected]>

* Add i18n checks to PR workflows

Signed-off-by: Miki <[email protected]>

* Changeset file for PR #8411 created/updated

---------

Signed-off-by: Miki <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>

(cherry picked from commit 428a7c2)
Signed-off-by: Miki <[email protected]>
  • Loading branch information
AMoo-Miki and opensearch-changeset-bot[bot] committed Oct 7, 2024
1 parent c5462c3 commit af3ab27
Show file tree
Hide file tree
Showing 12 changed files with 1,691 additions and 17 deletions.
59 changes: 46 additions & 13 deletions .github/workflows/build_and_test_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ env:
NODE_OPTIONS: "--max-old-space-size=6144 --dns-result-order=ipv4first"

jobs:
build-lint-test:
build-test:
name: Build and Verify on ${{ matrix.name }} (ciGroup${{ matrix.group }})
strategy:
fail-fast: false
Expand Down Expand Up @@ -104,18 +104,6 @@ jobs:
if: matrix.os == 'windows-latest'
run: yarn osd bootstrap || yarn osd bootstrap

- name: Run linter
# ciGroup 1 of unit-tests is shorter and Linux is faster
if: matrix.group == 1 && matrix.os == 'ubuntu-latest'
id: linter
run: yarn lint

- name: Validate NOTICE file
# ciGroup 1 of unit-tests is shorter and Linux is faster
if: matrix.group == 1 && matrix.os == 'ubuntu-latest'
id: notice-validate
run: yarn notice:validate

- name: Run unit tests group ${{ matrix.group }} with coverage
id: unit-tests
run: yarn test:jest:ci:coverage --ci-group=${{ matrix.group }}
Expand All @@ -140,6 +128,51 @@ jobs:
id: integration-tests
run: yarn test:jest_integration:ci

lint-and-validate:
name: Lint and validate
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
registry-url: 'https://registry.npmjs.org'

- name: Setup Yarn
run: |
npm uninstall -g yarn
npm i -g [email protected]
yarn config set network-timeout 1000000 -g
- name: Configure Yarn Cache
run: echo "YARN_CACHE_LOCATION=$(yarn cache dir)" >> $GITHUB_ENV

- name: Initialize Yarn Cache
uses: actions/cache@v3
with:
path: ${{ env.YARN_CACHE_LOCATION }}
key: yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
yarn-
- name: Run bootstrap
run: yarn osd bootstrap

- name: Run linter
id: linter
run: yarn lint

- name: Validate NOTICE file
id: notice-validate
run: yarn notice:validate

- name: Check i18n
id: i18n-check
run: yarn i18n:check

functional-tests:
name: Run functional tests on ${{ matrix.name }} (ciGroup${{ matrix.group }})
strategy:
Expand Down
9 changes: 9 additions & 0 deletions changelogs/fragments/8411.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
infra:
- Add i18n checks to PR workflows ([#8411](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8411))

feat:
- Ignore missing `formats` while checking locale files ([#8411](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8411))
- Add help text and description to `i18n-check` ([#8411](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8411))

fix:
- Fix malformed translations ([#8411](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8411))
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"lint": "yarn run lint:es && yarn run lint:style",
"lint:es": "scripts/use_node scripts/eslint",
"lint:style": "scripts/use_node scripts/stylelint",
"i18n:check": "scripts/use_node scripts/i18n_check --ignore-missing --ignore-unused",
"i18n:extract": "scripts/use_node scripts/i18n_extract.js",
"makelogs": "scripts/use_node scripts/makelogs",
"uiFramework:compileCss": "cd packages/osd-ui-framework && yarn compileCss",
"osd:watch": "scripts/use_node scripts/opensearch_dashboards --dev --logging.json=false",
Expand Down
7 changes: 6 additions & 1 deletion src/dev/i18n/integrate_locale_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface IntegrateOptions {
ignoreIncompatible: boolean;
ignoreUnused: boolean;
ignoreMissing: boolean;
ignoreMissingFormats?: boolean;
config: I18nConfig;
log: ToolingLog;
}
Expand Down Expand Up @@ -211,7 +212,11 @@ export async function integrateLocaleFiles(
) {
const localizedMessages = JSON.parse((await readFileAsync(options.sourceFileName)).toString());
if (!localizedMessages.formats) {
throw createFailError(`Locale file should contain formats object.`);
if (options.ignoreMissingFormats) {
options.log.warning('Missing formats object ignored');

Check warning on line 216 in src/dev/i18n/integrate_locale_files.ts

View check run for this annotation

Codecov / codecov/patch

src/dev/i18n/integrate_locale_files.ts#L216

Added line #L216 was not covered by tests
} else {
throw createFailError(`Locale file should contain formats object.`);

Check warning on line 218 in src/dev/i18n/integrate_locale_files.ts

View check run for this annotation

Codecov / codecov/patch

src/dev/i18n/integrate_locale_files.ts#L218

Added line #L218 was not covered by tests
}
}

const localizedMessagesMap: LocalizedMessageMap = new Map(
Expand Down
13 changes: 11 additions & 2 deletions src/dev/i18n/tasks/check_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface I18nFlags {
ignoreIncompatible: boolean;
ignoreUnused: boolean;
ignoreMissing: boolean;
ignoreMissingFormats: boolean;
}

export function checkCompatibility(
Expand All @@ -47,16 +48,24 @@ export function checkCompatibility(
if (!config) {
throw new Error('Config is missing');
}
const { fix, ignoreIncompatible, ignoreUnused, ignoreMalformed, ignoreMissing } = flags;
const {
fix,
ignoreIncompatible,
ignoreUnused,
ignoreMalformed,
ignoreMissing,
ignoreMissingFormats,
} = flags;
return config.translations.map((translationsPath) => ({
task: async ({ messages }: { messages: Map<string, { message: string }> }) => {
// If `fix` is set we should try apply all possible fixes and override translations file.
// If `fix` is set we should try to apply all possible fixes and override translations file.
await integrateLocaleFiles(messages, {
dryRun: !fix,
ignoreIncompatible: fix || ignoreIncompatible,
ignoreUnused: fix || ignoreUnused,
ignoreMissing: fix || ignoreMissing,
ignoreMalformed: fix || ignoreMalformed,
ignoreMissingFormats,
sourceFileName: translationsPath,
targetFileName: fix ? translationsPath : undefined,
config,
Expand Down
16 changes: 15 additions & 1 deletion src/dev/run_i18n_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { DEFAULT_DIRS_WITH_RC_FILES } from './i18n/constants';

const skipOnNoTranslations = (context: ListrContext) =>
!context.config?.translations?.length && 'No translations found.';

run(
async ({
flags: {
Expand All @@ -54,6 +55,7 @@ run(
'ignore-unused': ignoreUnused,
'include-config': includeConfig,
'ignore-untracked': ignoreUntracked,
'ignore-missing-formats': ignoreMissingFormats,
fix = false,
path,
},
Expand Down Expand Up @@ -121,11 +123,13 @@ run(
ignoreIncompatible: !!ignoreIncompatible,
ignoreUnused: !!ignoreUnused,
ignoreMissing: !!ignoreMissing,
// By default ignore missing formats
ignoreMissingFormats: ignoreMissingFormats !== false,
fix,
},
log
),
{ exitOnError: true }
{ exitOnError: false }
);
},
},
Expand Down Expand Up @@ -154,6 +158,16 @@ run(
flags: {
allowUnexpected: true,
guessTypesForUnexpectedFlags: true,
help: `
--ignore-incompatible Ignore mismatched keys in values and tokens in translations
--ignore-malformed Ignore malformed ICU format usages
--ignore-missing Ignore missing translations in locale files
--ignore-unused Ignore unused translations in locale files
--ignore-untracked Ignore untracked files with i18n labels
--ignore-missing-formats Ignore missing 'formats' key in locale files
(default: true, use --ignore-missing-formats=false to disable)
`,
},
description: 'Checks i18n usage in code and validates translation files',
}
);
Loading

0 comments on commit af3ab27

Please sign in to comment.