Skip to content

Notify depending widgets when translations are loaded #756

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

Merged
merged 2 commits into from
May 31, 2025

Conversation

rekire
Copy link
Contributor

@rekire rekire commented May 25, 2025

Fixes #755

Summary by CodeRabbit

  • Bug Fixes

    • Ensured widgets are properly updated when translations are loaded, improving localization update reliability.
  • Tests

    • Added new tests to verify translation loading and widget updates during initialization.
    • Updated existing tests to match new expected translation values and corrected minor typos in test descriptions.
  • Chores

    • Updated English localization files with revised translation values for consistency.

Copy link

coderabbitai bot commented May 25, 2025

Walkthrough

The changes update English translation files, modify notification logic in localization provider and controller classes to better signal when translations are loaded, and add or update tests to verify correct widget updates and translation loading behavior. Minor test description typos are also corrected.

Changes

File(s) Change Summary
i18n/en-US.json, i18n/en.json Updated "test" key values to "test_en-US" and "test_en" respectively.
lib/src/easy_localization_app.dart Added _translationsLoaded field; modified notification logic to include translation loading.
lib/src/easy_localization_controller.dart Added notifyListeners() after loading translations.
test/easy_localization_init_test.dart Added new test to verify translation loading and widget update behavior.
test/easy_localization_widget_test.dart Updated expected translation strings; fixed typos in test descriptions.

Sequence Diagram(s)

sequenceDiagram
    participant Widget
    participant EasyLocalizationProvider
    participant EasyLocalizationController

    Widget->>EasyLocalizationProvider: dependOnInheritedWidgetOfExactType
    EasyLocalizationProvider->>EasyLocalizationController: Check if translations loaded
    EasyLocalizationController->>EasyLocalizationProvider: Load translations (async)
    EasyLocalizationController->>EasyLocalizationProvider: notifyListeners()
    EasyLocalizationProvider-->>Widget: updateShouldNotify triggers rebuild if translations loaded
    Widget->>EasyLocalizationProvider: didChangeDependencies called
Loading

Assessment against linked issues

Objective Addressed Explanation
Notify dependent widgets when translations are loaded, not just on locale change (#755)
Ensure didChangeDependencies is triggered when translation data is available (#755)
Provide test coverage for translation loading and widget update behavior (#755)

Poem

In the garden of code where translations bloom,
Now widgets awake when new words resume!
With a hop and a skip, the locale's in sync—
No more stale carrots, fresh text in a blink.
🐇✨

"Translations loaded!" the rabbit proclaims—
Every widget rejoices, calling out names!


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 docstrings to generate docstrings for this 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

@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 (2)
test/easy_localization_init_test.dart (2)

10-19: Remove duplicate logger configuration.

The logger configuration is set twice with identical values. This is redundant and should be consolidated.

 EasyLocalization.logger.enableLevels = <LevelMessages>[
   LevelMessages.error,
   LevelMessages.warning,
 ];

 SharedPreferences.setMockInitialValues({});
-EasyLocalization.logger.enableLevels = <LevelMessages>[
-  LevelMessages.error,
-  LevelMessages.warning,
-];

29-29: Consider using a more robust path for translations.

The path '../../i18n' is relative and could be fragile depending on where tests are executed from. Consider using a more explicit path or ensuring the test environment is consistent.

-path: '../../i18n',
+path: 'test/resources/i18n', // or another more explicit path
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 11e665c and c336ee1.

📒 Files selected for processing (6)
  • i18n/en-US.json (1 hunks)
  • i18n/en.json (1 hunks)
  • lib/src/easy_localization_app.dart (3 hunks)
  • lib/src/easy_localization_controller.dart (1 hunks)
  • test/easy_localization_init_test.dart (1 hunks)
  • test/easy_localization_widget_test.dart (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (10)
i18n/en-US.json (1)

2-2: Good test data differentiation.

Changing the test key value to "test_en-US" helps distinguish between different locale files in tests, making it easier to verify that the correct locale-specific translations are being loaded.

i18n/en.json (1)

2-2: Consistent locale differentiation.

The change to "test_en" maintains consistency with the en-US locale file and helps distinguish the base English locale in tests.

lib/src/easy_localization_controller.dart (1)

127-127: Excellent fix for the core issue.

Adding notifyListeners() after translation loading ensures that dependent widgets are properly notified when translations become available. The placement is correct - after both main and fallback translations are loaded, and only on successful loading.

This directly addresses the PR objective of notifying widgets when translations are loaded.

test/easy_localization_widget_test.dart (2)

132-132: Correct test updates for new translation values.

The test expectations have been properly updated from 'test' to 'test_en-US' to align with the changes in the translation files. This ensures tests will pass with the new locale-specific translation values.

Also applies to: 136-136, 158-158, 163-163, 208-208, 213-213, 244-244, 249-249, 792-792


227-227: Good typo fixes.

Fixed the typo "loacle" to "locale" in test descriptions, improving code quality and readability.

Also applies to: 282-282

lib/src/easy_localization_app.dart (3)

232-232: Good addition to track translation loading state.

Adding the _translationsLoaded field provides a clean way to track whether translations have been loaded, which is essential for the notification mechanism.


259-259: Correct initialization of translation loading state.

The initialization logic correctly determines if translations are loaded by checking if _localeState.translations is not null. This provides the initial state for the notification mechanism.


295-296: Excellent enhancement to notification logic.

The updated updateShouldNotify method now correctly triggers notifications when either the locale changes OR when the translation loading state changes. This ensures that dependent widgets are notified immediately when translations become available, which perfectly addresses the PR objective.

test/easy_localization_init_test.dart (2)

39-77: Well-designed test observer for translation loading verification.

The I18nObserver is an excellent approach to test the notification mechanism. It correctly:

  • Uses didChangeDependencies to detect inherited widget changes
  • Calls EasyLocalization.of(context) to establish dependency
  • Tests both the initial untranslated state and the subsequent translated state

The test logic effectively verifies that dependent widgets are notified when translations are loaded.


7-7:

✅ Verification successful

Verify the dependency on easy_localization_context_test.dart.

The test imports MyApp from easy_localization_context_test.dart. Ensure this dependency is intentional and that MyApp is the appropriate widget for this test scenario.


🏁 Script executed:

#!/bin/bash
# Check if MyApp is defined in the imported file and verify its usage
fd "easy_localization_context_test.dart" --exec cat {} | grep -A 10 -B 5 "class MyApp"

Length of output: 565


🏁 Script executed:

#!/bin/bash
# Display the contents of the test to see how MyApp is used
fd "easy_localization_init_test.dart" --exec cat {} | sed -n '1,200p'

Length of output: 2110


Dependency Verified: MyApp is defined and used intentionally
The import of easy_localization_context_test.dart is correct—MyApp is declared there as the top-level widget for this test and is used appropriately within EasyLocalization. No changes needed.

@rekire
Copy link
Contributor Author

rekire commented May 25, 2025

Wow that was an interesting review.

@aissat aissat merged commit bed8c01 into aissat:develop May 31, 2025
4 checks passed
@rekire rekire deleted the notify_loading_finished branch May 31, 2025 06:33
@rekire
Copy link
Contributor Author

rekire commented May 31, 2025

Thank you @aissat for merging my fix so quickly. Do you have an ETA for the next update?

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.

Is there a callback when the localization files are loaded?
2 participants