Skip to content

Conversation

@Meldiron
Copy link
Contributor

@Meldiron Meldiron commented Aug 12, 2025

Summary by CodeRabbit

  • New Features

    • Accept a custom default for missing translations, applied with placeholders for dynamic fallback messages.
    • Option to return no text (null) for missing translations to intentionally omit UI content.
    • When no default is provided, a clear placeholder using the missing key is shown to highlight gaps.
  • Tests

    • Added tests verifying default behaviors, null-return, and placeholder interpolation in defaults.

@coderabbitai
Copy link

coderabbitai bot commented Aug 12, 2025

Walkthrough

Introduces a runtime sentinel DEFAULT_DYNAMIC_KEY and extends Locale::getText to accept an optional string|null $default parameter. Changes initialize translation differently when the sentinel is used, add a null-return when resolved translation is null, update docblocks, and add tests covering dynamic default, custom default, null default, and placeholder composition.

Changes

Cohort / File(s) Summary
Locale API and behavior update
src/Locale/Locale.php
Added DEFAULT_DYNAMIC_KEY constant; updated getText signature to `string
Tests for new default handling
tests/Locale/LocaleTest.php
Converted several calls to use named placeholders: arguments; added testGetTextDefault() verifying existing translation, dynamic placeholder default ('{{missing}}'), custom default, null default, and placeholder substitution with a provided default.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Locale

  Caller->>Locale: getText(key, default=?, placeholders?)
  alt default === DEFAULT_DYNAMIC_KEY
    Locale->>Locale: translation = "{{" + key + "}}"
  else default provided (string|null)
    Locale->>Locale: translation = default
  end
  Locale->>Locale: resolve translation (fallbacks / lookups)
  alt resolved translation === null
    Locale-->>Caller: null
  else
    Locale->>Locale: apply placeholders if any
    Locale-->>Caller: final string
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • Feat fallback locale #17: Modifies Locale::getText fallback/default behavior; closely related to signature and default-resolution changes here.

Suggested reviewers

  • loks0n

Poem

I hop through keys and stitch a text,
If missing, I craft "{{key}}" next.
Sometimes you give me words to show,
Or hand me none — and then I go.
I fill with names, then thump hello! 🥕🐇


📜 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 26347e4 and 3bd4495.

📒 Files selected for processing (1)
  • src/Locale/Locale.php (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Locale/Locale.php
✨ 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-get-text-default

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)

126-126: Fix parameter order in docblock.

The docblock parameter order doesn't match the actual function signature. The $default parameter comes before $placeholders in the function signature but appears after it in the docblock.

-     * @param  string  $key
-     * @param  array<string, string|int>  $placeholders
-     * @param  string|null  $default
+     * @param  string  $key
+     * @param  string|null  $default
+     * @param  array<string, string|int>  $placeholders

131-131: Consider using a more robust sentinel value.

The current sentinel value '[[defaultDynamciKey]]' (aside from the typo) could theoretically conflict with actual user input. Consider using a more unique approach like a private static object or a more distinctive string pattern.

-    const string DEFAULT_DYNAMIC_KEY = '[[defaultDynamicKey]]'; // Replaced at runtime by $key wrapped in {{ and }}
+    private static ?object $DEFAULT_DYNAMIC_KEY = null;
+    
+    private static function getDefaultDynamicKey(): object
+    {
+        if (self::$DEFAULT_DYNAMIC_KEY === null) {
+            self::$DEFAULT_DYNAMIC_KEY = new \stdClass();
+        }
+        return self::$DEFAULT_DYNAMIC_KEY;
+    }

And update the function signature and comparison:

-    public function getText(string $key, string|null $default = self::DEFAULT_DYNAMIC_KEY, array $placeholders = [])
+    public function getText(string $key, string|null|object $default = null, array $placeholders = [])
     {
+        if ($default === null) {
+            $default = self::getDefaultDynamicKey();
+        }
         // ...
-        $translation = $default === self::DEFAULT_DYNAMIC_KEY ? '{{'.$key.'}}' : $default;
+        $translation = $default === self::getDefaultDynamicKey() ? '{{'.$key.'}}' : $default;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 301984f and 26347e4.

📒 Files selected for processing (2)
  • src/Locale/Locale.php (3 hunks)
  • tests/Locale/LocaleTest.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/Locale/LocaleTest.php (1)
src/Locale/Locale.php (2)
  • getText (131-159)
  • Locale (7-170)
🔇 Additional comments (4)
src/Locale/Locale.php (2)

136-136: LGTM - Clean default value initialization.

The logic correctly initializes the translation based on whether the sentinel value is used, providing a dynamic placeholder when no explicit default is given.


150-152: LGTM - Proper null handling.

The early return for null translations prevents unnecessary placeholder processing and maintains expected behavior when an explicit null default is provided.

tests/Locale/LocaleTest.php (2)

68-71: LGTM - Improved test readability with named arguments.

Using named arguments for the placeholders parameter significantly improves test readability and makes the intent clearer. This is a good practice for optional parameters.

Also applies to: 72-74, 77-79, 81-83


124-133: LGTM - Comprehensive test coverage for default parameter.

The test thoroughly covers all the new default parameter behaviors:

  • Existing translations return correctly
  • Missing translations with dynamic default return placeholder
  • Custom default values are respected
  • Null default returns null
  • Placeholder substitution works with custom defaults

This provides excellent coverage for the new functionality.

@Meldiron Meldiron merged commit 10ffc86 into master Aug 12, 2025
4 checks passed
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