Skip to content

feat!: introduce TranslationsConfig#691

Merged
tbouffard merged 2 commits intomainfrom
refactor/introduce_TranslationsConfig
Feb 25, 2025
Merged

feat!: introduce TranslationsConfig#691
tbouffard merged 2 commits intomainfrom
refactor/introduce_TranslationsConfig

Conversation

@tbouffard
Copy link
Member

@tbouffard tbouffard commented Feb 24, 2025

Move the configuration related to Translations that was previously stored in Client to a new TranslationsConfig object.
This makes things more explicit and consistent with the other focussed configuration objects.

BREAKING CHANGES: All elements that were in Client to manage the configuration of Translations have moved to TranslationsConfig:

  • Client.defaultLanguage to TranslationsConfig.getDefaultLanguage
  • Client.setDefaultLanguage to TranslationsConfig.setDefaultLanguage
  • Client.language to TranslationsConfig.getLanguage
  • Client.setLanguage to TranslationsConfig.setLanguage
  • Client.languages to TranslationsConfig.getLanguages
  • Client.setLanguages to TranslationsConfig.setLanguages

Notes

Covers #688

Summary by CodeRabbit

  • New Features

    • Introduced a centralized translation configuration module for streamlined language management and resource handling.
    • Added a function to reset translation configurations to default values.
  • Refactor

    • Enhanced internationalization architecture to ensure consistent language settings and enforce a fixed version identifier.
  • Documentation

    • Updated guides to reflect the new translation setup, including details on configuring language resources and migration instructions.
    • Added documentation for the new TranslationsConfig and resetTranslationsConfig function.

Move the configuration related to `Translations` that was previously stored in `Client` to a new `TranslationsConfig` object.
This makes things more explicit and consistent with the other focussed configuration objects.

BREAKING CHANGES: All elements that were in `Client` to manage the configuration of `Translations` have moved to `TranslationsConfig`:
  - `Client.defaultLanguage` to `TranslationsConfig.getDefaultLanguage`
  - `Client.setDefaultLanguage` to `TranslationsConfig.setDefaultLanguage`
  - `Client.language` to `TranslationsConfig.getLanguage`
  - `Client.setLanguage` to `TranslationsConfig.setLanguage`
  - `Client.languages` to `TranslationsConfig.getLanguages`
  - `Client.setLanguages` to `TranslationsConfig.setLanguages`
@tbouffard tbouffard added the enhancement New feature or request label Feb 24, 2025
@coderabbitai
Copy link

coderabbitai bot commented Feb 24, 2025

Walkthrough

This pull request reorganizes the internationalization configuration in maxGraph by relocating several properties and methods from the Client class to a dedicated TranslationsConfig module. The Client’s language-related functionalities are removed, and the version constant is moved to constants. Import paths for the Translations module have been revised across various files to reflect its new location. In addition, resource key initialization conditions have been updated to rely on TranslationsConfig.isEnabled(), and new tests and documentation entries have been added to support the changes and document migration steps.

Changes

File(s) Change Summary
CHANGELOG.md Moved Client internationalization properties/methods to TranslationsConfig; moved Client.VERSION to constants; updated ManhattanConnector configuration notes.
packages/core/__tests__/i18n/config.test.ts Added new Jest test to verify the resetTranslationsConfig functionality.
packages/core/src/Client.ts Removed static internationalization properties and methods (language, setLanguage, defaultLanguage, setDefaultLanguage, languages, setLanguages) from Client.
packages/core/src/editor/Editor.ts, packages/core/src/view/Graph.ts, packages/core/src/view/GraphSelectionModel.ts, packages/core/src/view/GraphView.ts, packages/core/src/view/handler/ElbowEdgeHandler.ts, packages/core/src/view/mixins/FoldingMixin.ts Replaced Client.language–based conditionals with TranslationsConfig.isEnabled() for resource key initialization; updated imports to include TranslationsConfig where needed.
packages/core/src/editor/EditorPopupMenu.ts, packages/core/src/gui/MaxForm.ts, packages/core/src/gui/MaxWindow.ts, packages/core/src/serialization/codecs/editor/EditorCodec.ts, packages/core/src/serialization/codecs/editor/EditorToolbarCodec.ts, packages/core/src/view/geometry/node/StencilShape.ts, packages/core/src/view/mixins/TooltipMixin.ts, packages/core/src/view/mixins/ValidationMixin.ts, packages/core/src/view/other/Multiplicity.ts Updated import paths for Translations module from ../util/Translations to ../i18n/Translations.
packages/core/src/i18n/Translations.ts Updated import paths, added resource documentation, and replaced Client-based language logic with TranslationsConfig getters.
packages/core/src/i18n/config.ts New file providing TranslationsConfig with methods (get/set Language, languages, defaultLanguage) and the resetTranslationsConfig function.
packages/core/src/index.ts Modified exports: updated Translations export path and added exports from TranslationsConfig.
packages/html/.storybook/preview.ts Added a call to resetTranslationsConfig within the resetMaxGraphConfigs function.
packages/ts-example-without-defaults/vite.config.js, packages/ts-example/vite.config.js Incremented chunkSizeWarningLimit values (440→441 and 444→445) in Vite configurations.
packages/website/docs/usage/global-configuration.md, packages/website/docs/usage/i18n.md, packages/website/docs/usage/migrate-from-mxgraph.md Updated documentation to reflect new TranslationsConfig usage, revised internationalization migration guides, and introduced resetTranslationsConfig.

Sequence Diagram(s)

sequenceDiagram
    participant Comp as Client Component (e.g., Editor)
    participant TC as TranslationsConfig
    participant T as Translations Module

    Comp->>TC: isEnabled()
    alt Translations Enabled
        Comp->>T: load language resources
    else Translations Disabled
        Comp->>Comp: Assign default/empty resource values
    end
Loading
sequenceDiagram
    participant Test as Test Suite
    participant TC as TranslationsConfig

    Test->>TC: Set custom language/config values
    Test->>TC: resetTranslationsConfig()
    Test->>Test: Verify configuration reverts to original values
Loading

Possibly related PRs

Suggested labels

refactor


📜 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 b9722ce and d181223.

📒 Files selected for processing (1)
  • packages/website/docs/usage/i18n.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/website/docs/usage/i18n.md

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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. (Beta)
  • @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.

@tbouffard tbouffard marked this pull request as ready for review February 24, 2025 18:56
Copy link

@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 (7)
packages/website/docs/usage/global-configuration.md (2)

21-21: LGTM! Fix the list indentation.

The addition of TranslationsConfig is well-documented. However, the indentation should be consistent with other list items.

-  - `TranslationsConfig` (since 0.16.0): for the configuration of `Translations`.
+- `TranslationsConfig` (since 0.16.0): for the configuration of `Translations`.
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

21-21: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)


33-33: Fix the list indentation.

The indentation should be consistent with other list items.

-  - `resetTranslationsConfig` (since 0.16.0)
+- `resetTranslationsConfig` (since 0.16.0)
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

33-33: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

packages/core/src/view/Graph.ts (1)

386-388: Use of TranslationsConfig.isEnabled() for resource resolution.

This replacement ensures that translated resource keys appear only when translations are globally enabled. If an empty string is not desired as a fallback, consider displaying a user-friendly message or logging a warning.

packages/core/src/editor/Editor.ts (1)

314-320: Documentation updates for loading editor-specific resources.

These lines clearly illustrate how to load built-in and Editor-specific translations. Ensure the snippet remains up to date with any further changes to the resource paths or configuration processes.

packages/core/src/i18n/config.ts (3)

24-38: Consider removing @ts-ignore by properly typing the original values.

Instead of using @ts-ignore, consider initializing originalValues with proper typing:

-// @ts-ignore the properties will be added dynamically when calling shallowCopy
-const originalValues: TranslationsConfigValuesType = {};
+const originalValues: TranslationsConfigValuesType = {
+  defaultLanguage: '',
+  language: '',
+  languages: []
+};

94-112: Consider adding input validation for setLanguages.

The languages setter should validate that the input array contains only valid language codes.

 setLanguages(value: string[] | null | undefined): void {
   if (!isNullish(value)) {
+    // Validate that each language code follows ISO 639-1 format
+    const isValidLanguageCode = (code: string) => /^[a-z]{2}(-[A-Z]{2})?$/.test(code);
+    if (!value.every(isValidLanguageCode)) {
+      throw new Error('Invalid language code format. Expected ISO 639-1 format (e.g., "en", "en-US")');
+    }
     values.languages = value;
   }
 }

114-129: Consider adding input validation for setDefaultLanguage.

The default language setter should validate that the input is a valid language code.

 setDefaultLanguage(value: string | undefined | null): void {
+  const newValue = !isNullish(value) ? value : 'en';
+  // Validate that the language code follows ISO 639-1 format
+  if (!/^[a-z]{2}(-[A-Z]{2})?$/.test(newValue)) {
+    throw new Error('Invalid language code format. Expected ISO 639-1 format (e.g., "en", "en-US")');
+  }
-  values.defaultLanguage = !isNullish(value) ? value : 'en';
+  values.defaultLanguage = newValue;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9f18fba and b9722ce.

📒 Files selected for processing (27)
  • CHANGELOG.md (1 hunks)
  • packages/core/__tests__/i18n/config.test.ts (1 hunks)
  • packages/core/src/Client.ts (0 hunks)
  • packages/core/src/editor/Editor.ts (5 hunks)
  • packages/core/src/editor/EditorPopupMenu.ts (1 hunks)
  • packages/core/src/gui/MaxForm.ts (1 hunks)
  • packages/core/src/gui/MaxWindow.ts (1 hunks)
  • packages/core/src/i18n/Translations.ts (5 hunks)
  • packages/core/src/i18n/config.ts (1 hunks)
  • packages/core/src/index.ts (1 hunks)
  • packages/core/src/serialization/codecs/editor/EditorCodec.ts (1 hunks)
  • packages/core/src/serialization/codecs/editor/EditorToolbarCodec.ts (1 hunks)
  • packages/core/src/view/Graph.ts (2 hunks)
  • packages/core/src/view/GraphSelectionModel.ts (2 hunks)
  • packages/core/src/view/GraphView.ts (2 hunks)
  • packages/core/src/view/geometry/node/StencilShape.ts (1 hunks)
  • packages/core/src/view/handler/ElbowEdgeHandler.ts (2 hunks)
  • packages/core/src/view/mixins/FoldingMixin.ts (2 hunks)
  • packages/core/src/view/mixins/TooltipMixin.ts (1 hunks)
  • packages/core/src/view/mixins/ValidationMixin.ts (1 hunks)
  • packages/core/src/view/other/Multiplicity.ts (1 hunks)
  • packages/html/.storybook/preview.ts (2 hunks)
  • packages/ts-example-without-defaults/vite.config.js (1 hunks)
  • packages/ts-example/vite.config.js (1 hunks)
  • packages/website/docs/usage/global-configuration.md (2 hunks)
  • packages/website/docs/usage/i18n.md (1 hunks)
  • packages/website/docs/usage/migrate-from-mxgraph.md (2 hunks)
💤 Files with no reviewable changes (1)
  • packages/core/src/Client.ts
✅ Files skipped from review due to trivial changes (11)
  • packages/core/src/serialization/codecs/editor/EditorCodec.ts
  • packages/ts-example-without-defaults/vite.config.js
  • packages/ts-example/vite.config.js
  • packages/core/src/gui/MaxForm.ts
  • packages/core/src/view/mixins/ValidationMixin.ts
  • packages/core/src/gui/MaxWindow.ts
  • packages/core/src/editor/EditorPopupMenu.ts
  • packages/core/src/view/geometry/node/StencilShape.ts
  • packages/core/src/serialization/codecs/editor/EditorToolbarCodec.ts
  • packages/core/src/view/mixins/TooltipMixin.ts
  • packages/core/src/view/other/Multiplicity.ts
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
packages/website/docs/usage/global-configuration.md

21-21: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)


33-33: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

🪛 LanguageTool
packages/website/docs/usage/migrate-from-mxgraph.md

[grammar] ~366-~366: Nouns are not usually modified by plural nouns. Is it possible that you meant to use the singular or possessive form here?
Context: ...y keep the property in Translations - languages properties are replaced by getters in `...

(PLURAL_MODIFIER)

CHANGELOG.md

[grammar] ~28-~28: This phrase is duplicated. You should probably use “to to” only once.
Context: ...nsConfig: - Client.defaultLanguagetoTranslationsConfig.getDefaultLanguage -Client.setDefaultLanguagetoTranslationsConfig.setDefaultLanguage -Client.languagetoTranslationsConfig.getLanguage -Client.setLanguagetoTranslationsConfig.setLanguage -C...

(PHRASE_REPETITION)

🔇 Additional comments (32)
packages/html/.storybook/preview.ts (1)

12-12: LGTM! The changes align with the PR objectives.

The addition of resetTranslationsConfig to the reset sequence ensures that translation configurations are properly reset between stories, maintaining consistency with other configuration resets.

Also applies to: 38-38

packages/core/src/view/mixins/FoldingMixin.ts (1)

26-26: LGTM! The changes align with the PR objectives.

The migration from Client.language to TranslationsConfig.isEnabled() maintains the same functionality while aligning with the new centralized translations configuration approach.

Also applies to: 66-66

packages/core/src/index.ts (1)

133-134: LGTM! The changes align with the PR objectives.

The export path updates and new exports correctly reflect the reorganization of translation-related code into a dedicated i18n directory, making the new TranslationsConfig available to consumers.

packages/core/src/view/GraphView.ts (1)

48-48: LGTM! The changes align with the PR objectives.

The migration from Client.language to TranslationsConfig.isEnabled() for resource key initialization maintains the same functionality while aligning with the new centralized translations configuration approach.

Also applies to: 124-124, 132-132

packages/core/__tests__/i18n/config.test.ts (2)

28-42: LGTM! Well-structured test implementation.

The test follows good practices:

  • Captures the original configuration state
  • Modifies multiple configuration values
  • Verifies that the reset restores the original state

1-15: ⚠️ Potential issue

Update the copyright year.

The copyright year is set to 2025, which is incorrect as it's beyond the current year.

-Copyright 2025-present The maxGraph project Contributors
+Copyright 2024-present The maxGraph project Contributors

Likely an incorrect or invalid review comment.

packages/website/docs/usage/i18n.md (1)

21-27: LGTM! Clear resource file format documentation.

The added documentation clearly illustrates the resource file format with a helpful example.

packages/core/src/view/GraphSelectionModel.ts (2)

26-26: LGTM! Clean import addition.

The import of TranslationsConfig is correctly placed with other imports.


81-81: LGTM! Improved initialization logic.

The change from Client.language !== 'none' to TranslationsConfig.isEnabled() makes the code more semantic and clearer in its intent.

Also applies to: 89-89

packages/core/src/view/Graph.ts (2)

63-63: Solid import for translation config.

Importing TranslationsConfig aligns well with the new i18n approach for consistent configuration across the codebase.


396-398: Similar i18n resource initialization.

Using TranslationsConfig.isEnabled() keeps translation toggles consistent. The empty-string fallback is fine unless there's a requirement for default messaging.

packages/core/src/view/handler/ElbowEdgeHandler.ts (2)

23-24: Updated imports to the new i18n modules.

These updated paths and the additional TranslationsConfig import are consistent with the ongoing reorganization for improved i18n support.


60-62: Conditional resource assignment using i18n config.

Good replacement from Client.language checks to TranslationsConfig.isEnabled(). It ensures a central toggle controls the resource availability for the tooltip text.

packages/core/src/editor/Editor.ts (9)

23-23: Refined import path for Translations module.

Pointing to ../i18n/Translations promotes better organization within the i18n directory structure.


59-59: Added import for TranslationsConfig.

Adopting TranslationsConfig for Editor’s translation-related logic fosters consistency with other refactored modules.


459-459: askZoomResource now depends on TranslationsConfig.isEnabled().

Switching to TranslationsConfig.isEnabled() cleanly toggles the translation usage for the zoom dialog’s resource key.


469-469: Centralized toggle for lastSavedResource.

This ensures the resource key is only active when translations are properly enabled. A good unified approach.


476-476: Updated currentFileResource condition.

Same pattern: returning an empty string if translations are disabled. This maintains consistency with the other resource keys.


484-484: Conditionally assigning propertiesResource.

Relying on a single global toggle for translations helps avoid confusion when enabling or disabling i18n features.


492-492: Conditional check for tasksResource.

Aligning the tasks window title resource with the rest of the i18n toggles ensures a cohesive user experience.


500-500: Conditional assignment for helpResource.

This is consistent with the new code style around i18n. Appears correct and well-scoped.


508-508: Outline window title i18n toggle.

The empty-string fallback ensures no usage of the resource key if translations are disabled. Implementation looks good.

packages/core/src/i18n/config.ts (4)

1-19: LGTM!

The copyright notice and imports are correctly implemented.


20-22: LGTM!

The function correctly handles both browser and non-browser environments, with a proper fallback to 'en'.


40-42: LGTM!

The reset function correctly restores the original configuration values.


47-92: LGTM!

The language management methods are well-implemented with proper nullish checks and clear documentation.

packages/website/docs/usage/migrate-from-mxgraph.md (1)

366-367: LGTM!

The documentation accurately reflects the relocation of language-related properties from Client to TranslationsConfig.

Also applies to: 380-381

🧰 Tools
🪛 LanguageTool

[grammar] ~366-~366: Nouns are not usually modified by plural nouns. Is it possible that you meant to use the singular or possessive form here?
Context: ...y keep the property in Translations - languages properties are replaced by getters in `...

(PLURAL_MODIFIER)

CHANGELOG.md (1)

27-33: LGTM!

The changelog clearly documents the breaking changes related to the relocation of language-related properties from Client to TranslationsConfig.

🧰 Tools
🪛 LanguageTool

[grammar] ~28-~28: This phrase is duplicated. You should probably use “to to” only once.
Context: ...nsConfig: - Client.defaultLanguagetoTranslationsConfig.getDefaultLanguage -Client.setDefaultLanguagetoTranslationsConfig.setDefaultLanguage -Client.languagetoTranslationsConfig.getLanguage -Client.setLanguagetoTranslationsConfig.setLanguage -C...

(PHRASE_REPETITION)

packages/core/src/i18n/Translations.ts (4)

20-23: LGTM!

The imports are correctly updated to use the new module structure.


112-118: LGTM!

The language support check is correctly updated to use TranslationsConfig.


148-165: LGTM!

The special bundle logic is correctly updated to use TranslationsConfig for language settings.


190-190: LGTM!

The language assignment is correctly updated to use TranslationsConfig with nullish coalescing.

@sonarqubecloud
Copy link

@tbouffard tbouffard merged commit 1f415db into main Feb 25, 2025
2 checks passed
@tbouffard tbouffard deleted the refactor/introduce_TranslationsConfig branch February 25, 2025 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant