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

Align trExists API with tr/plural #738

Merged
merged 2 commits into from
Jan 7, 2025
Merged

Conversation

omj-denis
Copy link
Contributor

@omj-denis omj-denis commented Jan 7, 2025

Thanks for this useful library!

This PR adds some missing parts for trExists function:

  • trExists should accept context as an argument (similar to tr and plural API)
  • trExists was available as a standalone function and as a String extension previously. This PR also extends BuildContext with it (similar to tr and plural).

Without a context, trExists function would have an issue similar to #448 and #552.

Summary by CodeRabbit

  • New Features

    • Enhanced localization functionality by adding optional context parameter to trExists method
    • Improved translation key existence checking across different contexts
  • Tests

    • Added test case to verify translation key existence functionality

The changes provide more flexible localization support, allowing developers to check translation key existence with or without a specific context.

Copy link

coderabbitai bot commented Jan 7, 2025

Walkthrough

The changes enhance the localization functionality in the Easy Localization library by introducing an optional BuildContext parameter to the trExists method. This modification allows for more flexible translation key existence checking, enabling developers to verify translations within specific widget contexts or using the global localization instance. The updates span across the public interface, extension methods, and include a corresponding test case to validate the new functionality.

Changes

File Change Summary
lib/src/public.dart Updated trExists method signature to include optional BuildContext parameter
lib/src/public_ext.dart - Modified StringTranslateExtension.trExists() to accept optional context
- Added BuildContextEasyLocalizationExtension.trExists() method
test/easy_localization_test.dart Added new test case for trExists() function to verify key existence

Sequence Diagram

sequenceDiagram
    participant Client
    participant trExists
    participant Localization
    
    alt With Context
        Client->>trExists: key, context
        trExists->>Localization: of(context)
        Localization-->>trExists: localization instance
        trExists->>trExists: Check key existence
    else Without Context
        Client->>trExists: key
        trExists->>Localization: Use global instance
        trExists->>trExists: Check key existence
    end
    trExists-->>Client: Boolean result
Loading

Poem

🐰 A Rabbit's Ode to Translation Might

With context or without, we'll seek the light
Keys dancing, existence taking flight
Localization's magic, clear and bright
Translations checked with all our might

Code hops, context sings! 🌈🔍


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.

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 (1)
test/easy_localization_test.dart (1)

719-722: Add test coverage for context parameter scenarios.

While the current tests cover the basic functionality, consider adding test cases for:

  • Testing with a valid BuildContext
  • Testing with null context
  • Testing with an invalid context (should throw LocalizationNotFoundException)

Here's a suggested implementation:

test('trExists with context', () {
  final mockContext = MockBuildContext();
  expect(trExists('test', context: mockContext), true);
  expect(trExists('xyz', context: mockContext), false);
});

test('trExists with null context', () {
  expect(trExists('test', context: null), true);
  expect(trExists('xyz', context: null), false);
});

test('trExists with invalid context', () {
  final invalidContext = MockBuildContext();
  expect(
    () => trExists('test', context: invalidContext),
    throwsA(isA<LocalizationNotFoundException>()),
  );
});
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0aa278e and 9fdabe5.

📒 Files selected for processing (3)
  • lib/src/public.dart (1 hunks)
  • lib/src/public_ext.dart (2 hunks)
  • test/easy_localization_test.dart (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (3)
lib/src/public.dart (1)

48-53: LGTM! Implementation aligns with existing patterns.

The implementation correctly follows the established pattern used in tr() and plural() functions for context handling.

lib/src/public_ext.dart (2)

93-93: LGTM! String extension implementation is correct.

The implementation properly forwards the context parameter to the core implementation.


222-230: LGTM! BuildContext extension implementation is correct.

The implementation properly handles the localization not found case and follows the established pattern.

@bw-flagship bw-flagship merged commit d3278cd into aissat:develop Jan 7, 2025
5 checks passed
@bw-flagship
Copy link
Collaborator

Thanks for the contribution @omj-denis

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.

2 participants