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

[11.x] Add replaceable tags to translations #51190

Merged
merged 3 commits into from Apr 29, 2024

Conversation

LegendEffects
Copy link
Contributor

@LegendEffects LegendEffects commented Apr 23, 2024

Turned #51188 into a PR.

When writing translations there are times when you may want to inline some HTML, such as for bolding some text or placing a link in the middle of a sentence, currently, you can achieve this by doing something along the lines of:

{!! __('Something <b>bold</b>, and a <a href=":href">link</a>', ['href' => 'https://laravel.com']) !!}

But this isn't very nice or readable to a translator, FormatJS already has a solution to this with Tag Replacements, these look like normal HTML tags but they don't support attributes, they can be added with callable:

{!! __(
    'Something <b>bold</b> and a <link>link</link>',
    [
        'link' => fn ($children) => "<a href=\"https://laravel.com\">$children</a>"
    ]
) !!}

This takes the complexity away from the translator (as in the person, not the class), and makes the code more maintainable if you want to modify that anchor tag with a class then you don't have to go through each translation to add it.

This still requires the usage of unescaped echos within blade, I'm happy to accept any ideas for making this into a HTMLString to avoid this but I didn't want to pile too much into this merge.

This change should be fully backwards compatible.

@LegendEffects LegendEffects changed the title Add replaceable tags to translations [11.x] Add replaceable tags to translations Apr 23, 2024
@LegendEffects
Copy link
Contributor Author

@taylorotwell Is there anything I can do to help get this merged?

@taylorotwell taylorotwell merged commit 83e1f56 into laravel:11.x Apr 29, 2024
27 of 28 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.

None yet

2 participants