When building multilingual websites or web applications in Laravel, managing localization files can quickly become a headache.
If your Blade templates contain large amounts of text or you need translations across multiple languages, manually extracting and translating strings becomes repetitive and error-prone.
This is where a simple CLI package can make your life easier — automatically extracting strings from Blade templates and generating translated locale JSON files in one command.
Example: Blade File with Translatable Text
<p>{{ __('Laravel has an incredibly rich ecosystem. We suggest starting with the following.') }}</p>
The package scans your Blade templates for __()
calls and adds them to the appropriate JSON files in resources/lang
.
Introducing Locale Generator
Locale Generator is a Laravel CLI package that:
- Scans Blade templates and extracts translatable strings
- Generates JSON locale files for multiple languages
- Translates them automatically using Google Translate API or DeepL API
Installation
1. Install the package
composer require eii/locale-generator
2. Publish the config file
php artisan vendor:publish --tag=config
3. Create the resources/lang directory
If it doesn’t already exist:
mkdir resources/lang
Setting up Google Translate API
To use Google Translate, you’ll need a Google Cloud Service Account with the Translation API enabled.
Step-by-step:
Go to Google Cloud Console
https://console.cloud.google.com/
Sign in with your Google account.-
Create a new project
- Click the project selector at the top
- Choose New Project
- Give it a name and create it.
-
Enable the Translation API
- Go to APIs & Services → Library
- Search for
Cloud Translation API
- Click Enable.
-
Create a Service Account
- Navigate to IAM & Admin → Service Accounts
- Click Create Service Account
- Fill in the name and description
- Assign the role:
Project > Editor
orCloud Translation API User
-
Generate a key file
- Select your new service account
- Go to the Keys tab
- Click Add Key → Create New Key
- Select JSON and download the file.
-
Save the file in your Laravel project
- Move it to:
storage/app/google-credentials.json
- Move it to:
Update your
.env
file:
GOOGLE_TRANSLATE_KEY_FILE=storage/app/google-credentials.json
GOOGLE_PROJECT_ID=your-project-id
Setting up DeepL API
If you prefer DeepL:
Create or log in to a DeepL account
https://www.deepl.com/pro-api-
Get your API key
- Go to Account → API Keys & Limits
- Click Generate Key and copy it.
Update your
.env
file:
DEEPL_API_KEY=your-deepl-api-key
Usage
Once your credentials are set, run:
php artisan lang:extract welcome --locales=ja,es,de --translate=deepl
This will:
- Scan
welcome.blade.php
for translatable text - Create
ja.json
,es.json
, andde.json
in resources/lang - Automatically translate the content using DeepL
You can specify:
-
--locales
→ Comma-separated list of language codes -
--translate
→ deepl or google
Common Language Codes
-
en
→ English -
ja
→ Japanese -
es
→ Spanish -
de
→ German -
fr
→ French
Why This Helps
Without this tool, you would:
- Manually scan each Blade file
- Copy strings into multiple JSON files
- Translate each string into multiple languages
With Locale Generator, you can:
- Automate extraction and file creation
- Translate into multiple languages in seconds
- Keep localization consistent and up-to-date
Tip: Machine translations are great for speed, but always have a native speaker review important content.
Top comments (0)