This PowerShell script automates the translation of Laravel's en.json
language file into multiple languages using the Google Translate API. It reads the English language file, translates each string into the specified languages, and generates corresponding JSON files for each language in your lang
directory.
- Google Translate API Key: You need a valid Google Translate API key. You can get one from the Google Cloud Console.
- PowerShell: Ensure PowerShell is available on your system.
-
Clone the Repository or download or copy the script.
-
If you don't already have an
en.json
file containing all your translatable strings, you can use this Blade Translatable Extractor PowerShell script to extract them from your Laravel views and generate theen.json
file. -
Set your Google Translate API key in the script:
$apiKey = "YOUR_API_KEY"
-
Update the
lang
folder path in the script to point to your Laravellang
directory:$langDir = "path/to/lang"
-
Ensure the
lang
directory contains youren.json
file.
- Open PowerShell and navigate to the directory where the script is located.
- Run the script:
.\translate.ps1
The script will:
- Validate the
lang
folder anden.json
file paths. - Automatically translate the content of
en.json
into the supported languages. - Save the translated files as JSON files (e.g.,
es.json
,fr.json
) in the samelang
directory.
- If a translation already exists in the target language file, the script will skip that key unless the key is missing.
- If a translation fails, the script will log an error and move on to the next key.
For example, if you are translating to Spanish, the script will generate a es.json
file in the lang
folder containing the translated strings.
{
"welcome": "bienvenida",
"login": "acceso",
"logout": "cerrar sesión"
}
- If the script fails to find the
en.json
file orlang
folder, ensure that the paths are correctly set in the script. - Ensure that your Google Translate API Key is active and has sufficient quota.
- If the script fails to find the
en.json
file orlang
folder, ensure that the paths are correctly set in the script. - Ensure that your Google Translate API Key is active and has sufficient quota.
This script is open-source and available for personal and commercial use.