Pages

23 March, 2023

Laravel Convert HTML Strings to Translation Functions

 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.

No comments:

Post a Comment

Thanks