Skip to content

Conversation

@Meldiron
Copy link
Contributor

@Meldiron Meldiron commented Aug 12, 2025

Summary by CodeRabbit

  • New Features

    • Added configurable fallback locale for translations.
    • Ability to set a fallback locale with validation and method chaining.
    • Default placeholder text can derive from the fallback translation when available.
  • Refactor

    • Error/exception behavior now respects the global exceptions setting across locale initialization and default locale changes.
  • Tests

    • Added tests verifying fallback behavior and exception handling.
    • Updated test setup and language fixtures.

@coderabbitai
Copy link

coderabbitai bot commented Aug 12, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Introduces a fallback locale mechanism in Locale, adds setFallback(), updates constructor and validation to honor exceptions flag, and modifies getText() to consult a fallback locale for default text. Tests are updated and expanded to cover fallback behavior and exception handling.

Changes

Cohort / File(s) Summary of Changes
Locale core
src/Locale/Locale.php
Added public property fallback; added setFallback(string): self with validation; constructor and setters now respect self::$exceptions; getText() now derives default text from fallback locale when available; adjusted error handling for missing locales/keys.
Tests
tests/Locale/LocaleTest.php
Updated setup to load en-US, he-IL, hi-IN via array/JSON; added testFallback() covering fallback resolution and exception behavior; minor formatting adjustments.

Sequence Diagram(s)

sequenceDiagram
  participant C as Caller
  participant L as Locale

  C->>L: setFallback("en-US")
  L-->>C: Locale (chained)

  C->>L: getText(key, params)
  alt key exists in default locale
    L-->>C: translated value
  else key missing in default locale
    alt key exists in fallback locale
      L-->>C: fallback translation (as default text)
    else exceptions disabled
      L-->>C: placeholder or computed default
    else exceptions enabled
      L-->>C: throw Exception
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Possibly related PRs

  • Feat fallback locale #17: Implements the same fallback locale property, setFallback method, and fallback-aware getText(), with matching tests—appears to be the directly corresponding change.

Poem

A bunny hops through strings so bright,
Finds “שלום” by day, “World” by night.
When keys go missing, don’t despair—
A fallback whisper fills the air.
If errors shout, we heed the call;
With tidy tests, we guard it all.
🐇✨


📜 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 02bed77 and 8c4801c.

📒 Files selected for processing (2)
  • src/Locale/Locale.php (5 hunks)
  • tests/Locale/LocaleTest.php (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat-fallback-translations

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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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: 1

🧹 Nitpick comments (2)
src/Locale/Locale.php (2)

28-34: Consider making the fallback property protected for better encapsulation.

The fallback property is declared as public, which allows direct modification bypassing the validation logic in setFallback(). Consider making it protected to ensure proper validation.

-    /**
-     * Fallback locale. Used when specific or default locale is missing translation.
-     * Should always be set to locale that includes all translations.
-     *
-     * @var string
-     */
-    public $fallback;
+    /**
+     * Fallback locale. Used when specific or default locale is missing translation.
+     * Should always be set to locale that includes all translations.
+     *
+     * @var string
+     */
+    protected $fallback;

73-89: Consider adding null safety check and improving parameter documentation.

The method implementation is sound, but there are minor improvements that could enhance robustness and documentation.

 /**
  * Change fallback Locale
  *
- * @param $name
+ * @param string $name The name of the locale to set as fallback
  *
  * @throws Exception
  */
 public function setFallback(string $name): self
 {
     if (! \array_key_exists($name, self::$language) && self::$exceptions) {
         throw new Exception('Locale not found');
     }

     $this->fallback = $name;

     return $this;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4e6af and 8c41d48.

📒 Files selected for processing (2)
  • src/Locale/Locale.php (5 hunks)
  • tests/Locale/LocaleTest.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/Locale/LocaleTest.php (1)
src/Locale/Locale.php (1)
  • setFallback (80-89)
🔇 Additional comments (4)
src/Locale/Locale.php (3)

55-57: LGTM: Exception handling now respects the global flag.

The change to conditionally throw exceptions based on self::$exceptions improves consistency with the class's exception handling strategy.


66-68: LGTM: Constructor now respects the exception flag.

The constructor now properly respects the global $exceptions flag, maintaining consistency with other methods in the class.


100-102: LGTM: setDefault now respects the exception flag.

The change maintains consistency with the new exception handling pattern across the class.

tests/Locale/LocaleTest.php (1)

81-92: Excellent test coverage for the fallback functionality.

The test effectively validates the fallback behavior:

  1. Confirms primary locale translations take precedence
  2. Verifies fallback locale provides missing translations
  3. Tests the transition from placeholder to fallback translation

The test structure is clear and comprehensive.

@Meldiron Meldiron merged commit 301984f into master Aug 12, 2025
3 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Aug 12, 2025
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.

3 participants