localize.dev

Docs / Laravel

Laravel localization for Blade, automated

Move hardcoded copy in Laravel Blade views into __() calls with lang/<locale>.json translations keyed by the text, in one command or pull request.

How it works

Text in Blade views becomes {{ __('Hi :name, welcome back', ['name' => $user->name]) }}: Laravel’s JSON translation strings, keyed by the text itself, with :name placeholders.

Directives like @if split sentences; email addresses aren’t mistaken for directives.

Before and after

The engine’s real output for this file. In a real run, the hosted model names the keys from context.

resources/views/cart.blade.php · before
@extends('layouts.app')
@section('content')
  <h1>Hi {{ $user->name }}, welcome back</h1>
  <input placeholder="Search products">
@endsection
resources/views/cart.blade.php · after
@extends('layouts.app')
@section('content')
  <h1>{{ __('Hi :name, welcome back', ['name' => $user->name]) }}</h1>
  <input placeholder="{{ __('Search products') }}">
@endsection
lang/en.json
{
    "Hi :name, welcome back": "Hi :name, welcome back",
    "Search products": "Search products"
}

What the setup adds

The first run sets up Laravel’s translation strings if the project doesn’t have it yet:

  • lang/<locale>.json for every language
  • A SetLocale middleware in the web group: ?locale=, then a lang cookie, then Accept-Language

Run it

The CLI needs Node.js 22 or later, nothing else: npx runs it without adding a package.json to the project. No Node on your machine? The GitHub Action below runs it for you.

npx localize-dev login

# Preview, then write the changes
npx localize-dev run --dry-run -l es,fr,de
npx localize-dev run -l es,fr,de

# Or open a pull request instead
npx localize-dev pr -l es,fr,de

Or add the GitHub Action and get a pull request on every push. Try it on your own code first in the playground.