Convert HTML Strings to Translation Functions

Published on by

Convert HTML Strings to Translation Functions image

HTML String Affixer is a package for Laravel to make projects multilingual. This package finds text strings in HTML and replaces them with a localization function:

{{-- Before --}}
<p>Some nice string</p>
{{-- After --}}
<p>{{ __('Some nice string') }}</p>

This tool can be used with any .jsx, .vue, or .twig file as well, with customizable file prefixes and suffixes. The package's main features include:

  • Finds your HTML visible strings and replaces them with affixed one
  • Prefix and Suffixes are customizable (default {{ __(' and ') }})
  • If a string contains one of the warning characters, it will not replace but prints out the location:
  • If a string contains one of the ignoring characters, it just ignores the string
  • Ignore characters and warning characters are customizable from the JSON config file ("ignore" and "warnings")

This package supports configuration, which might look like the following JSON file:

"folder": "resources/views",
"prefix": "{{ __('",
"suffix": "') }}",
"ignore": ["#", "_", ">", "^", "*", "="],
"warnings": ["%", "{", "(", "}", ")", "$", "'"]

You can then replace strings with translations using the following command:

./vendor/bin/hsa replace
# Or on Windows
./vendor/bin/hsawin replace

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.


Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
Kirschbaum logo


Providing innovation and stability to ensure your web application succeeds.

Shift logo


Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Bacancy logo


Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

LoadForge logo


Easy, affordable load testing and stress tests for websites, APIs and databases.

Paragraph logo


Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. Partner with Lucky Media, your favorite Laravel Development Agency!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Oh Dear logo

Oh Dear

Oh Dear is the best all-in-one monitoring tool for all your Laravel apps.

Oh Dear
Tinkerwell logo


The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.


The latest

View all →
Get insights into all your Laravel notifications with Paragraphs new package image

Get insights into all your Laravel notifications with Paragraphs new package

Read article
FrankenPHP v1.0 is Here image

FrankenPHP v1.0 is Here

Read article
Self-healing URLs in Laravel image

Self-healing URLs in Laravel

Read article
Laravel 10.35 Released image

Laravel 10.35 Released

Read article
Solve n+1 queries in PHP with Scout APM image

Solve n+1 queries in PHP with Scout APM

Read article
Show Outdated Composer Dependencies in Laravel Pulse image

Show Outdated Composer Dependencies in Laravel Pulse

Read article