DEV Community

amit-eiitech
amit-eiitech

Posted on

Effortless Laravel Localization: Auto-Generate JSON Locale Files and Translate with Google or DeepL

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2. Publish the config file

php artisan vendor:publish --tag=config
Enter fullscreen mode Exit fullscreen mode

3. Create the resources/lang directory

If it doesn’t already exist:

mkdir resources/lang
Enter fullscreen mode Exit fullscreen mode

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:

  1. Go to Google Cloud Console
    https://console.cloud.google.com/
    Sign in with your Google account.

  2. Create a new project

    • Click the project selector at the top
    • Choose New Project
    • Give it a name and create it.
  3. Enable the Translation API

    • Go to APIs & Services → Library
    • Search for Cloud Translation API
    • Click Enable.
  4. 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 or Cloud Translation API User
  5. 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.
  6. Save the file in your Laravel project

    • Move it to: storage/app/google-credentials.json
  7. Update your .env file:

GOOGLE_TRANSLATE_KEY_FILE=storage/app/google-credentials.json
GOOGLE_PROJECT_ID=your-project-id
Enter fullscreen mode Exit fullscreen mode

Setting up DeepL API

If you prefer DeepL:

  1. Create or log in to a DeepL account
    https://www.deepl.com/pro-api

  2. Get your API key

    • Go to Account → API Keys & Limits
    • Click Generate Key and copy it.
  3. Update your .env file:

DEEPL_API_KEY=your-deepl-api-key
Enter fullscreen mode Exit fullscreen mode

Usage

Once your credentials are set, run:

php artisan lang:extract welcome --locales=ja,es,de --translate=deepl
Enter fullscreen mode Exit fullscreen mode

This will:

  • Scan welcome.blade.php for translatable text
  • Create ja.json, es.json, and de.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)