5 min read

One Command Line To Place All Your HTML Strings In The Translation Function

Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.

You can see the original blog from the author on Medium.

laravel blade file translation

If you're looking for a hassle-free way to translate Laravel blade files and localize your HTML files in Laravel projects, html-strings-affixer can be a great solution. This robust command line tool allows you to localize your HTML files easily, especially in Laravel projects.

In this article, we'll guide you through the installation process of the tool using Composer, creating an affixer.json file with the required configurations, and using it to add translation functions to Laravel's blade files.

Installation

First things first, let's go over the installation process. To install html-strings-affixer in your Laravel project, make sure you have Composer installed on your machine. Once that's taken care of, head to the command line, navigate to your project's root directory, and run the following command:

composer require maestroerror/html-strings-affixer --dev

Once you've run the installation command for html-strings-affixer in your Laravel project, the package is ready to be used. To access the tool, simply type ./vendor/bin/hsa on your command line. If you're using Windows, type ./vendor/bin/hsawin instead.

Configuration

Laravel Blade

The next step is setting up the affixer.json file. This file is used to configure the prefix and suffix strings that will be used to replace the visible HTML strings, as well as the ignore and warning characters. To create the file, navigate to the root directory of your project and create a new file called "affixer.json."

This file contains the prefix and suffix strings used to replace visible HTML strings in your project. By default, these strings are set as {{ __('and ') }}, but you can modify them as needed to fit the requirements of your project. In this guide, we'll use the default strings to demonstrate how to add a translation function to your Laravel blade files.

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

Replace command

Now that we have installed the tool and set up the configuration file, we can use it to add translation functions to our blade files. To do this, navigate to the root directory of your project in the command line and run the following command:

./vendor/bin/hsa replace
# For windows:
./vendor/bin/hsawin replace

This command tells the tool to run the replace command and look for strings in the directory resources/views/. This will find all strings in the specified directory (blade.php files) and replace them with the prefixed and suffixed strings, for example:

<p>Some nice string</p>

Will be replaced with

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

In this way, you can use html-strings-affixer to add translation functions to your Laravel blade files quickly and easily. The tool also allows you to customize the ignore and warning characters, file extensions, scanning folders, and parsing methods. So you can fine-tune it to suit your specific needs.

You'll need to run the appropriate command in the terminal or command prompt, depending on how you've installed html-strings-affixer. If you've installed it using binary, you can run the command using ./vendor/bin/hsa (or ./vendor/bin/hsawin on Windows) from the root directory of your project. If you've installed it with Composer, navigate to the root directory of your project and run the command.

Conclusion

Laravel blade file translation

In conclusion, html-strings-affixer is a tool that helps developers quickly add localization functions to their HTML files. It can be installed in a Laravel project using Composer, and its configurations can be set up in an affixer.json file. Once set up, the tool can replace HTML strings in blade files with the desired prefix and suffix strings, making adding translation functions to your application easy. 

The tool also allows users to customize the ignore and warning characters, making it possible to fine-tune it to suit specific needs. Overall, html-strings-affixer is a handy and time-saving tool for developers looking to streamline the localization process in their projects.

For more information, please refer to the Documentation and feel free to contact me (revaz.gh@gmail.com)

 

Written by Revaz Ghambarashvili

Meet the authors

We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.

Contact us