Skip to content
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

feat: add locale check script and GitHub Action workflow #3817

Conversation

ChaitanyaZunzurkar
Copy link

@ChaitanyaZunzurkar ChaitanyaZunzurkar commented Mar 6, 2025

Description

Implemented a script to check for missing/inconsistent locale variables.
Integrated the script into a GitHub Action workflow for automated checks.
Ensured compatibility with the existing locale structure (separate JSON files inside en/).
Related issue(s)

Resolves #3777

Summary by CodeRabbit

  • Chores
    • Introduced an automated process that verifies the consistency of localization files to ensure translations remain accurate and reliable.
    • Added a new GitHub Actions workflow to automate locale consistency checks on pull requests.

Copy link
Contributor

coderabbitai bot commented Mar 6, 2025

Walkthrough

This pull request adds an automated locale consistency check to the repository. A new GitHub Actions workflow, triggered on pull requests targeting the main and develop branches, sets up the environment, installs dependencies, and executes a Node.js script. The script (checkLocales.js) reads locale JSON files from the specified directories and compares the primary locale with other locales, logging discrepancies and exiting with an error when inconsistencies are found.

Changes

Files Change Summary
.github/workflows/check-locales.yml Adds a new GitHub Actions workflow triggered on PRs to main/develop. It checks out the repository, sets up Node.js v18, installs dependencies, and runs the locale check.
scripts/checkLocales.js Introduces a Node.js script that validates JSON locale files. It reads the primary locale and compares it with other locale files, logging missing keys or files, and exits with an error if inconsistencies are detected.

Sequence Diagram(s)

sequenceDiagram
    participant PR as "Pull Request"
    participant GH as "GitHub Actions"
    participant Checkout as "actions/checkout"
    participant NodeSetup as "actions/setup-node"
    participant Npm as "npm install"
    participant Script as "checkLocales.js"
    participant Locales as "Locale Files"

    PR->>GH: Trigger workflow on PR to main/develop
    GH->>Checkout: Checkout repository
    Checkout-->>GH: Repository checked out
    GH->>NodeSetup: Setup Node.js v18 environment
    NodeSetup-->>GH: Environment ready
    GH->>Npm: Install dependencies
    Npm-->>GH: Dependencies installed
    GH->>Script: Execute locale check
    Script->>Locales: Read primary & target locale JSON files
    Locales-->>Script: Return locale data
    Script-->>GH: Log results and exit on error/success
Loading

Poem

I'm a coding bunny on a merry spree,
Hopping through workflows so light and free.
Checking locales with a twitch of my ear, 🐰
Spotting mismatches far and near.
With JSON magic in every byte,
I cheer each change from morn till night!

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

Copy link

netlify bot commented Mar 6, 2025

Deploy Preview for asyncapi-website ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1a4c08e
🔍 Latest deploy log https://app.netlify.com/sites/asyncapi-website/deploys/67c9b73d15265f000812f60f
😎 Deploy Preview https://deploy-preview-3817--asyncapi-website.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Mar 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (d6a213a) to head (1a4c08e).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #3817   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           21        21           
  Lines          667       667           
  Branches       113       113           
=========================================
  Hits           667       667           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (5)
.github/workflows/check-locales.yml (3)

15-15: Update the GitHub Actions checkout action to v4.

The checkout action version v3 is outdated. GitHub recommends using the latest version for improved security and features.

-        uses: actions/checkout@v3
+        uses: actions/checkout@v4
🧰 Tools
🪛 actionlint (1.7.4)

15-15: the runner of "actions/checkout@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)


18-18: Update the GitHub Actions setup-node action to v4.

The setup-node action version v3 is outdated. GitHub recommends using the latest version for improved security and features.

-        uses: actions/setup-node@v3
+        uses: actions/setup-node@v4
🧰 Tools
🪛 actionlint (1.7.4)

18-18: the runner of "actions/setup-node@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)


22-23: Consider implementing dependency caching.

Adding caching for Node.js dependencies can significantly speed up your workflow execution time.

      - name: Install Dependencies
+       id: cache
+       uses: actions/cache@v3
+       with:
+         path: '**/node_modules'
+         key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
+         restore-keys: |
+           ${{ runner.os }}-node-
+       
+     - name: Install Dependencies (if needed)
+       if: steps.cache.outputs.cache-hit != 'true'
        run: npm install
scripts/checkLocales.js (2)

6-7: Consider making the directory paths configurable.

Hardcoding paths might make the script less flexible if the project structure changes in the future.

-const LOCALES_DIR = path.join(__dirname, '../public/locales'); 
-const PRIMARY_LOCALE = 'en';
+// Allow overriding via environment variables
+const LOCALES_DIR = process.env.LOCALES_DIR || path.join(__dirname, '../public/locales'); 
+const PRIMARY_LOCALE = process.env.PRIMARY_LOCALE || 'en';
+
+console.log(`Checking locales in: ${LOCALES_DIR}`);
+console.log(`Primary locale: ${PRIMARY_LOCALE}`);

47-48: Consider checking for extra keys in locale files.

Currently, the script only checks for missing keys, but doesn't verify if the locale files contain unnecessary extra keys that don't exist in the primary locale.

    const missingKeys = Object.keys(primaryData).filter(key => !(key in localeData));
+    const extraKeys = Object.keys(localeData).filter(key => !(key in primaryData));
+    
+    if (extraKeys.length > 0) {
+        console.warn(`Extra keys in ${folder}/${file} (not in primary locale): ${JSON.stringify(extraKeys, null, 2)}`);
+        // Optionally set hasErrors = true if you want to treat extra keys as errors
+    }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d6a213a and 0c6686c.

📒 Files selected for processing (2)
  • .github/workflows/check-locales.yml (1 hunks)
  • scripts/checkLocales.js (1 hunks)
🧰 Additional context used
🪛 actionlint (1.7.4)
.github/workflows/check-locales.yml

15-15: the runner of "actions/checkout@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)


18-18: the runner of "actions/setup-node@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
ChaitanyaZunzurkar and others added 3 commits March 6, 2025 20:24
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
scripts/checkLocales.js (3)

1-2: Disable ESLint rules with caution.

These ESLint disables suppress important code quality checks. Consider addressing the underlying formatting issues instead of disabling the rules entirely. If disabling is necessary, add a comment explaining why.


49-50: Consider checking for nested objects in locale files.

The current implementation only checks for top-level keys. Many locale files use nested structures, which this implementation won't validate correctly.

const missingKeys = Object.keys(primaryData).filter(key => !(key in localeData));

+ // Helper function to recursively check nested objects
+ const checkNestedObjects = (primary, locale, parentKey = '') => {
+   let nestedMissingKeys = [];
+   
+   Object.keys(primary).forEach(key => {
+     const currentKey = parentKey ? `${parentKey}.${key}` : key;
+     
+     if (typeof primary[key] === 'object' && primary[key] !== null && 
+         typeof locale[key] === 'object' && locale[key] !== null) {
+       // Recursively check nested objects
+       nestedMissingKeys = [...nestedMissingKeys, 
+         ...checkNestedObjects(primary[key], locale[key], currentKey)];
+     } else if (!(key in locale)) {
+       nestedMissingKeys.push(currentKey);
+     }
+   });
+   
+   return nestedMissingKeys;
+ };
+
+ // Check for nested missing keys
+ const nestedMissingKeys = checkNestedObjects(primaryData, localeData);
+ if (nestedMissingKeys.length > 0) {
+   console.error(`Missing nested keys in ${folder}/${file}: ${JSON.stringify(nestedMissingKeys, null, 2)}`);
+   hasErrors = true;
+ }

1-82: Consider adding additional validation features.

The script currently only checks for missing keys but doesn't validate other important aspects:

  1. Keys in target locales that don't exist in the primary locale (potentially outdated)
  2. Value type mismatches between locales (e.g., string vs. object)
  3. Placeholder consistency (e.g., {name} exists in one locale but not another)

Would you like me to provide implementation suggestions for these additional validations?

🧰 Tools
🪛 Biome (1.9.4)

[error] 59-59: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 59-60: Expected a statement but instead found '}'.

Expected a statement here.

(parse)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c6686c and 1a4c08e.

📒 Files selected for processing (1)
  • scripts/checkLocales.js (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
scripts/checkLocales.js

[error] 59-59: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 59-60: Expected a statement but instead found '}'.

Expected a statement here.

(parse)

🔇 Additional comments (6)
scripts/checkLocales.js (6)

6-7: LGTM! The constants are well-defined.

The script correctly defines the locales directory path and primary locale constant.


9-16: The error handling in readJson is now properly implemented.

The function correctly reads and parses JSON files, with proper error handling using console.error.


18-30: Good implementation of folder existence checks.

The function correctly checks if both primary and target locale folders exist before proceeding, with appropriate error messages and return values.


32-34: LGTM! Effective file filtering.

The code correctly identifies and filters JSON files in both directories.


35-58: The error accumulation approach is well-implemented.

The code now properly accumulates errors instead of exiting on the first issue, which provides better visibility into all locale inconsistencies at once.


62-80: Overall flow for checking locales and reporting errors is well-structured.

The script effectively:

  1. Identifies all locale directories
  2. Checks each non-primary locale against the primary one
  3. Collects all errors before showing a final status
  4. Exits with the appropriate code based on validation results

This ensures that all locale issues are reported at once, making it easier for developers to fix them.

Comment on lines +59 to +60
});
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix syntax error in the checkLocale function.

There's an extra closing parenthesis and curly brace that will cause a syntax error.

  return !hasErrors;
- });
};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
});
};
return !hasErrors;
};
🧰 Tools
🪛 Biome (1.9.4)

[error] 59-59: Expected a semicolon or an implicit semicolon after a statement, but found none

An explicit or implicit semicolon is expected here...

...Which is required to end this statement

(parse)


[error] 59-60: Expected a statement but instead found '}'.

Expected a statement here.

(parse)

@akshatnema
Copy link
Member

This Issue is under bounty program. Hence PR can't be generated directly.

@akshatnema akshatnema closed this Mar 6, 2025
@ChaitanyaZunzurkar
Copy link
Author

Could you please guide me on the correct process to contribute? Should I get prior approval before submitting a PR on bounty program ?

@ChaitanyaZunzurkar ChaitanyaZunzurkar deleted the feat/check-locales-script branch March 7, 2025 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add script to have all inplace variable in each locale file
3 participants