Skip to content

Conversation

@Aotumuri
Copy link
Member

Description:

This pull request improves the logic for matching language codes to supported languages.
Now, the function tries to find the most specific language by splitting the input code with “-” or “_”, and searching from the longest combination to the shortest.
If no direct match is found, it searches for all supported language codes that start with the same base (first two letters) and chooses the most specific one.
If nothing matches, it defaults to English.

Please complete the following:

  • I have added screenshots for all UI updates
  • I process any text displayed to the user through translateText() and I've added it to the en.json file
  • I have added relevant tests to the test directory
  • I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced
  • I have read and accepted the CLA aggreement (only required once).

Please put your Discord username so you can be contacted if a bug or regression is found:

aorumuri

@CLAassistant
Copy link

CLAassistant commented Jul 23, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ Aotumuri
❌ scottanderson
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 23, 2025

Walkthrough

The getClosestSupportedLang method in LangSelector was simplified and updated to match language codes using a prefix-based search on hyphenated codes. Language JSON filenames and keys were changed from underscore to hyphen format to align with this new matching logic.

Changes

File(s) Change Summary
src/client/LangSelector.ts Updated import paths and keys in languageMap to hyphenated codes; simplified getClosestSupportedLang to prefix-match language codes.
resources/lang/pt-BR.json, sv-SE.json, zh-CN.json Changed language code format inside JSON files from underscore (e.g., pt_BR) to hyphen (e.g., pt-BR).

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant LangSelector
    participant languageMap

    User->>LangSelector: Request closest supported language for code (e.g., "pt-BR")
    LangSelector->>LangSelector: Extract first two letters of input code
    LangSelector->>LangSelector: Find supported languages starting with those letters
    LangSelector->>LangSelector: Return longest matching supported language or "en" if none found
    LangSelector-->>User: Return closest supported language code
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

Bug Fix, Translation

Suggested reviewers

  • evanpelle
  • scottanderson

Poem

🌐
Codes once split by underscore,
Now hyphens open the door.
Languages matched with care,
Finding closest ones out there.
From "pt-BR" to "sv-SE",
Smooth and simple, easy to see! 🎉

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-07-24T09_00_59_033Z-debug-0.log

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b5c069c and 21e0906.

📒 Files selected for processing (4)
  • resources/lang/pt-BR.json (1 hunks)
  • resources/lang/sv-SE.json (1 hunks)
  • resources/lang/zh-CN.json (1 hunks)
  • src/client/LangSelector.ts (4 hunks)
✅ Files skipped from review due to trivial changes (3)
  • resources/lang/pt-BR.json
  • resources/lang/sv-SE.json
  • resources/lang/zh-CN.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/client/LangSelector.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 sequence diagram to generate a sequence diagram of the changes in 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
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 (1)
src/client/LangSelector.ts (1)

93-118: Consider edge case handling for robustness.

The enhanced logic handles most scenarios well, but consider these edge cases:

  1. Empty string or very short inputs (handled by line 94)
  2. Language codes shorter than 2 characters (the slice(0, 2) on line 108 could be problematic)
  3. Non-alphabetic characters in language codes

Add a guard for the base language extraction:

 const base = lang.slice(0, 2);
+if (base.length < 2) return "en";
 const candidates = Object.keys(this.languageMap).filter((key) =>
   key.startsWith(base),
 );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ad72000 and 213b365.

📒 Files selected for processing (1)
  • src/client/LangSelector.ts (1 hunks)
🧠 Learnings (1)
📓 Common learnings
Learnt from: andrewNiziolek
PR: openfrontio/OpenFrontIO#1007
File: resources/lang/de.json:115-115
Timestamp: 2025-06-02T14:27:37.609Z
Learning: For OpenFrontIO project: When localization keys are renamed in language JSON files, the maintainers separate technical changes from translation content updates. They wait for community translators to update the actual translation values rather than attempting to translate in the same PR. This allows technical changes to proceed while ensuring accurate translations from native speakers.
Learnt from: scottanderson
PR: openfrontio/OpenFrontIO#949
File: resources/lang/en.json:8-10
Timestamp: 2025-05-30T03:53:52.231Z
Learning: For the OpenFrontIO project, do not suggest updating translation files in resources/lang/*.json except for en.json. The project has a dedicated translation team that handles all other locale files.
Learnt from: Aotumuri
PR: openfrontio/OpenFrontIO#1357
File: resources/lang/de.json:523-540
Timestamp: 2025-07-12T08:41:35.101Z
Learning: In OpenFrontIO project localization files, always check the en.json source file before flagging potential spelling errors in other language files, as some keys may intentionally use non-standard spellings that need to be consistent across all translations.
Learnt from: andrewNiziolek
PR: openfrontio/OpenFrontIO#1007
File: resources/lang/he.json:138-138
Timestamp: 2025-06-02T14:27:23.893Z
Learning: andrewNiziolek's project uses community translation for internationalization. When updating map names or other user-facing text, they update the keys programmatically but wait for community translators to provide accurate translations in each language rather than using machine translations.
Learnt from: scottanderson
PR: openfrontio/OpenFrontIO#1171
File: src/client/LangSelector.ts:20-20
Timestamp: 2025-06-16T00:12:31.955Z
Learning: ISO 3166-1 country codes in OpenFrontIO language files should use uppercase format (e.g., pt_BR, not pt_br) to maintain consistency with the established naming convention and ISO standards.
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: andrewNiziolek
PR: openfrontio/OpenFrontIO#1007
File: resources/lang/de.json:115-115
Timestamp: 2025-06-02T14:27:37.609Z
Learning: For OpenFrontIO project: When localization keys are renamed in language JSON files, the maintainers separate technical changes from translation content updates. They wait for community translators to update the actual translation values rather than attempting to translate in the same PR. This allows technical changes to proceed while ensuring accurate translations from native speakers.
Learnt from: scottanderson
PR: openfrontio/OpenFrontIO#949
File: resources/lang/en.json:8-10
Timestamp: 2025-05-30T03:53:52.231Z
Learning: For the OpenFrontIO project, do not suggest updating translation files in resources/lang/*.json except for en.json. The project has a dedicated translation team that handles all other locale files.
Learnt from: Aotumuri
PR: openfrontio/OpenFrontIO#1357
File: resources/lang/de.json:523-540
Timestamp: 2025-07-12T08:41:35.101Z
Learning: In OpenFrontIO project localization files, always check the en.json source file before flagging potential spelling errors in other language files, as some keys may intentionally use non-standard spellings that need to be consistent across all translations.
Learnt from: andrewNiziolek
PR: openfrontio/OpenFrontIO#1007
File: resources/lang/he.json:138-138
Timestamp: 2025-06-02T14:27:23.893Z
Learning: andrewNiziolek's project uses community translation for internationalization. When updating map names or other user-facing text, they update the keys programmatically but wait for community translators to provide accurate translations in each language rather than using machine translations.
Learnt from: scottanderson
PR: openfrontio/OpenFrontIO#1171
File: src/client/LangSelector.ts:20-20
Timestamp: 2025-06-16T00:12:31.955Z
Learning: ISO 3166-1 country codes in OpenFrontIO language files should use uppercase format (e.g., pt_BR, not pt_br) to maintain consistency with the established naming convention and ISO standards.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: 🏗️ Build
🔇 Additional comments (1)
src/client/LangSelector.ts (1)

108-115: Smart fallback logic with good specificity handling.

The base language fallback mechanism is well-designed. It correctly:

  • Uses the first 2 characters as the base language code
  • Filters candidates that start with the base
  • Sorts by length descending to prefer more specific matches (like "pt_BR" over "pt")

This aligns well with your language map structure and handles cases where users might have "pt-PT" but you only support "pt_BR".

@github-project-automation github-project-automation bot moved this from Triage to Development in OpenFront Release Management Jul 23, 2025
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: 0

🧹 Nitpick comments (1)
src/client/LangSelector.ts (1)

97-103: Good enhancement, but consider supporting underscore delimiters too.

The new logic correctly handles hyphen-delimited language codes and progressively tries shorter combinations. However, it only splits on hyphens while the AI summary mentions both hyphens and underscores should be supported.

Consider updating to handle both delimiter types:

-    if (lang.includes("-")) {
-      const parts = lang.split("-");
+    const delimiters = ["-", "_"];
+    for (const delimiter of delimiters) {
+      if (lang.includes(delimiter)) {
+        const parts = lang.split(delimiter);
         for (let i = parts.length; i > 0; i--) {
           const code = parts.slice(0, i).join("_");
           if (code in this.languageMap) return code;
         }
+      }
+    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 213b365 and b5c069c.

📒 Files selected for processing (1)
  • src/client/LangSelector.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: andrewNiziolek
PR: openfrontio/OpenFrontIO#1007
File: resources/lang/de.json:115-115
Timestamp: 2025-06-02T14:27:37.609Z
Learning: For OpenFrontIO project: When localization keys are renamed in language JSON files, the maintainers separate technical changes from translation content updates. They wait for community translators to update the actual translation values rather than attempting to translate in the same PR. This allows technical changes to proceed while ensuring accurate translations from native speakers.
Learnt from: scottanderson
PR: openfrontio/OpenFrontIO#949
File: resources/lang/en.json:8-10
Timestamp: 2025-05-30T03:53:52.231Z
Learning: For the OpenFrontIO project, do not suggest updating translation files in resources/lang/*.json except for en.json. The project has a dedicated translation team that handles all other locale files.
Learnt from: Aotumuri
PR: openfrontio/OpenFrontIO#1534
File: src/client/LangSelector.ts:97-106
Timestamp: 2025-07-23T12:36:35.331Z
Learning: In OpenFrontIO's LangSelector.ts, the getClosestSupportedLang method always joins language code parts with underscores ("_") because all keys in the languageMap use underscore format (e.g., pt_BR, sv_SE, zh_CN). This normalization ensures consistency regardless of whether the input language code uses hyphens or underscores as delimiters.
Learnt from: Aotumuri
PR: openfrontio/OpenFrontIO#1357
File: resources/lang/de.json:523-540
Timestamp: 2025-07-12T08:41:35.101Z
Learning: In OpenFrontIO project localization files, always check the en.json source file before flagging potential spelling errors in other language files, as some keys may intentionally use non-standard spellings that need to be consistent across all translations.
src/client/LangSelector.ts (9)

Learnt from: Aotumuri
PR: #1534
File: src/client/LangSelector.ts:97-106
Timestamp: 2025-07-23T12:36:35.331Z
Learning: In OpenFrontIO's LangSelector.ts, the getClosestSupportedLang method always joins language code parts with underscores ("_") because all keys in the languageMap use underscore format (e.g., pt_BR, sv_SE, zh_CN). This normalization ensures consistency regardless of whether the input language code uses hyphens or underscores as delimiters.

Learnt from: andrewNiziolek
PR: #1007
File: resources/lang/de.json:115-115
Timestamp: 2025-06-02T14:27:37.609Z
Learning: For OpenFrontIO project: When localization keys are renamed in language JSON files, the maintainers separate technical changes from translation content updates. They wait for community translators to update the actual translation values rather than attempting to translate in the same PR. This allows technical changes to proceed while ensuring accurate translations from native speakers.

Learnt from: Aotumuri
PR: #1357
File: resources/lang/zh_cn.json:527-539
Timestamp: 2025-07-12T08:42:02.109Z
Learning: In OpenFrontIO project, the territory pattern key "embelem" is the correct spelling used in all language files, not "emblem". This is the technical identifier that appears in en.json and must be consistent across all localization files.

Learnt from: Aotumuri
PR: #1357
File: resources/lang/de.json:523-540
Timestamp: 2025-07-12T08:41:35.101Z
Learning: In OpenFrontIO project localization files, always check the en.json source file before flagging potential spelling errors in other language files, as some keys may intentionally use non-standard spellings that need to be consistent across all translations.

Learnt from: scottanderson
PR: #1171
File: src/client/LangSelector.ts:20-20
Timestamp: 2025-06-16T00:12:31.955Z
Learning: ISO 3166-1 country codes in OpenFrontIO language files should use uppercase format (e.g., pt_BR, not pt_br) to maintain consistency with the established naming convention and ISO standards.

Learnt from: scottanderson
PR: #949
File: resources/lang/en.json:8-10
Timestamp: 2025-05-30T03:53:52.231Z
Learning: For the OpenFrontIO project, do not suggest updating translation files in resources/lang/*.json except for en.json. The project has a dedicated translation team that handles all other locale files.

Learnt from: andrewNiziolek
PR: #1007
File: resources/lang/he.json:138-138
Timestamp: 2025-06-02T14:27:23.893Z
Learning: andrewNiziolek's project uses community translation for internationalization. When updating map names or other user-facing text, they update the keys programmatically but wait for community translators to provide accurate translations in each language rather than using machine translations.

Learnt from: scottanderson
PR: #786
File: src/client/TerritoryPatternsModal.ts:337-338
Timestamp: 2025-06-22T05:48:19.241Z
Learning: In src/client/TerritoryPatternsModal.ts, the bit shifting operators (<<) used in coordinate calculations with decoder.getScale() are intentional and should not be changed to multiplication. The user scottanderson confirmed this is functioning as intended.

Learnt from: Aotumuri
PR: #1357
File: resources/lang/ja.json:0-0
Timestamp: 2025-07-12T06:35:10.457Z
Learning: In OpenFrontIO project, "giantworldmap" is the correct localization key name for the giant world map, used consistently across all language files and TypeScript code. Do not suggest renaming this key.

🔇 Additional comments (1)
src/client/LangSelector.ts (1)

105-112: Excellent fallback logic with smart candidate selection.

The base language matching with length-based sorting is a solid approach that prioritizes more specific language variants (like pt_BR over pt). The logic correctly handles cases where no exact match exists.

Comment on lines 97 to 103
if (lang.includes("-")) {
const parts = lang.split("-");
for (let i = parts.length; i > 0; i--) {
const code = parts.slice(0, i).join("_");
if (code in this.languageMap) return code;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just rename the files now.

@Aotumuri Aotumuri requested a review from a team as a code owner July 24, 2025 08:59
@Aotumuri Aotumuri added this to the v24 milestone Jul 24, 2025
@Aotumuri Aotumuri self-assigned this Jul 24, 2025
@Aotumuri Aotumuri removed this from the v24 milestone Jul 24, 2025
@scottanderson scottanderson added this to the v24 milestone Jul 25, 2025
@scottanderson scottanderson enabled auto-merge (squash) July 25, 2025 00:24
@scottanderson scottanderson disabled auto-merge July 25, 2025 18:08
@scottanderson scottanderson merged commit 1c6aa2b into openfrontio:main Jul 25, 2025
7 of 8 checks passed
@github-project-automation github-project-automation bot moved this from Development to Complete in OpenFront Release Management Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

3 participants