gpt-translate
is a package for Laravel that allows you to generate a translation file based on translation strings found throughout your application. You can call these strings using __()
, @lang()
, $t()
, or trans()
, and they can be located in php, js, ts, tsx, or vue files.
Furthermore, gpt-translate
enables you to translate your base language file, whether you previously had it or generated it using the package, to other languages using the ChatGPT API. Supported languages for translation include English, Swedish, Danish, Norwegian, Finish, Dutch, Polish, Spanish, French, German, Italian, and Portuguese.
This package supports multiple Laravel versions:
- Laravel 10.x (PHP ^8.2)
- Laravel 11.x (PHP ^8.2)
- Laravel 12.x (PHP ^8.2)
The package automatically adapts to your Laravel version and provides full compatibility across all supported versions.
Installation
Add the following to your composer.json
file:
{
"repositories": [
"gpt-translate": {
"type": "git",
"url": "https://github.com/techlove/gpt-translate.git"
}
]
}
Then install the package using composer:
composer require techlove/gpt-translate
Service Provider Auto-Discovery Laravel 5.5+ supports package auto-discovery. The service provider will be automatically registered.
For older versions of Laravel, manually add the service provider to your config/app.php
file:
'providers' => [
...
Techlove\GptTranslate\TranslateProvider::class,
...
],
Publish the Configuration Files
You need to publish the configuration files:
# First, publish the OpenAI configuration (if not already done)
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
# Then, publish the GPT-Translate specific configuration
php artisan vendor:publish --provider="Techlove\GptTranslate\TranslateProvider"
This will publish:
config/openai.php
- OpenAI API configuration (from the OpenAI Laravel package)config/gpt-translate.php
- GPT-Translate specific settings
Environment Configuration
Add the following variables to your .env
file:
# OpenAI Configuration (Required)
OPENAI_API_KEY=your-openai-api-key
OPENAI_ORGANIZATION=your-organization-id
# GPT-Translate Configuration (Optional)
GPT_TRANSLATE_DEFAULT_CONTEXT="Your application context here"
GPT_TRANSLATE_EXCLUDE_WORDS="AppName,BrandName,TechTerms"
Fetch All Translatable and Translate
This command extracts all translatable strings into a lang/en.json
, then translates them using ChatGPT into a new file lang/sv.json
.
Default values can be omitted from the command:
php artisan translate:extract --origin=en --lang=sv --model=gpt-4o --path=resources/lang
Generate Base Translation File
If you don’t already have a base translation file, generate it using:
php artisan translate:make --lang=en --path=resources/lang
As shown, this command creates a lang/en.json
in your root folder. If your application is in another language, pass the appropriate lang
parameter, e.g., for Swedish:
php artisan translate:make --lang=sv --path=resources/lang
Translate with ChatGPT
After having your base translation file, whether generated by the package or otherwise, run the translation command for ChatGPT to do the translation, e.g.:
php artisan translate:lang --origin=en --lang=sv --path=resources/lang
This translates the original en.json
to Swedish and creates a new sv.json
file.
Provide Context (Optional)
For more accurate translations, briefly describe the context of your application’s purpose. This helps ChatGPT better understand and translate the text. Use the context
parameter:
php artisan translate:lang --origin=en --lang=sv --context="a pet product sales application" --path=resources/lang
Exclude Words from Translation
Prevent specific words or phrases from being translated by using the --exclude
option:
php artisan translate:lang --origin=en --lang=sv --exclude="AppName,BrandName,API" --path=resources/lang
You can also set excluded words in your environment or config file for all translations.
Specify the Model (Optional)
By default, the package uses GPT-4o. If desired, specify any other OpenAI model compatible with the Chat API:
php artisan translate:lang --origin=en --lang=sv --model=gpt-4-turbo --path=resources/lang
This package includes development tools for maintaining code quality:
composer run format # Format code with Laravel Pint
composer run format-test # Check formatting without making changes
composer run test # Run tests with Pest
composer run test-coverage # Run tests with coverage
- ✅ Added support for Laravel 12.x
- ✅ Maintained backward compatibility with Laravel 10.x and 11.x
- ✅ Updated to latest OpenAI PHP client (v0.16.0)
- ✅ NEW: Changed default model to GPT-4o for better translations
- ✅ NEW: Added excluded words functionality
- ✅ NEW: Added
gpt-translate.php
configuration file - ✅ NEW: Support for default context via environment variables
- ✅ Improved JSON encoding with proper Unicode support
- ✅ Added Laravel Pint for code formatting
- ✅ Added Pest testing framework
- ✅ Improved service provider auto-discovery
- ✅ Enhanced code quality and type safety
Leverage the power of ChatGPT and the flexibility of gpt-translate
to localize your Laravel application effectively and efficiently across Laravel 10, 11, and 12.