Add Translation Functions To Laravel Blade Files With One Line
  • Services
    • What We Do
    • How We Collaborate
    • Product UI/UX
    • Agile Team
    • Software Development
    • Fixed Price
    • Creative Growth
    • Staff Augmentation
  • Our Work
    • Case Studies
    • Client Stories
  • About
  • Contact
  • Blog
  • Careers
Let's Talk
Back
Development
7 min read

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

  • rdbr-blogs
  • Author
    rdbr-blogs
  • Published
    February 28, 2023

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

Get in touch

Nuki Zhgenti

Business development manager

Book a call

Shermadin Osadze

Business development manager

Book a call

Hey!

We are an official Laravel and Vue partner agency from Tbilisi, Georgia.

Need help with software development or extending your team? You’re in the right place.

Let’s make cool things happen 🚀

reach out
Installation
Configuration
Replace command
Conclusion
Meet the authors
Get in touch
Next Post
5 Tips For Finding an Experienced Laravel Development Agency

Recent Posts

Development
12 min read

What Makes Vue.Js The Best Tech Stack For Startups?

Vue.js is a robust JavaScript framework that facilitates...

Read More
Development
14 min read

How to Conduct Technical Interviews? - A Step-by-Step Guide

If you are in the tech world, most...

Read More
Development
18 min read

4 Ready-Made Design Systems That'll Make Your Life a Breeze

We have worked on many highly time-sensitive projects,...

Read More
  • Follow us
    Fb. /  Lk.  /   Be.
  • Want to work with us?
    hello@redberry.ge
  • Have a general question?
    info@redberry.ge
  • Looking for an opportunity?
    joinus@redberry.ge
© 2021, Redberry - Digital Transformation Agency
All right reserved.