A powerful Laravel package that automatically detects untranslated strings in your application and provides automatic translation capabilities using free translation services.
- π Smart Detection: Scans your entire Laravel application for untranslated strings
- π Free Auto Translation: Uses LibreTranslate and MyMemory (completely free, no API keys needed!)
- π Translation Statistics: Get detailed insights about your translation coverage
- π§ Flexible Configuration: Customizable scan paths, file formats, and translation patterns
- π― Multiple File Formats: Support for both JSON and PHP translation files
- π± Rich Console Interface: Beautiful command-line interface with progress indicators
- π‘οΈ Safe Operation: Dry-run mode to preview changes before applying them
composer require nabila/laravel-translation-sync
php artisan vendor:publish --tag=translation-sync-config
# Scan and auto-translate using FREE services
php artisan translations:sync --translate --auto
# Check your translation coverage
php artisan translations:sync --stats
That's it! π Your Laravel app now has automatic translation capabilities using completely free services.
- PHP: >= 8.0
- Laravel: 9.x, 10.x, 11.x, 12.x
- Composer: Latest stable version
Install the package via Composer:
composer require nabila/laravel-translation-sync
Publish the configuration file:
php artisan vendor:publish --tag=translation-sync-config
The package comes pre-configured to work with free translation services. No API keys, no billing setup required!
# Your .env file (these are the defaults)
TRANSLATION_SERVICE=libretranslate # Free service!
TRANSLATION_AUTO_TRANSLATE=true
Start using it immediately:
php artisan translations:sync --translate --auto
After installation, test that everything works:
# Run the test script
php test-package.php
# Or test with Laravel commands
php artisan translations:sync --stats
php artisan translations:sync --translate --dry-run
The package comes with a comprehensive configuration file. Here are the key settings:
// config/translation-sync.php
return [
// Translation service: 'libretranslate', 'mymemory', 'google', 'dummy'
'service' => env('TRANSLATION_SERVICE', 'libretranslate'),
// Source language (language of your translation keys)
'source_language' => env('TRANSLATION_SOURCE_LANG', 'en'),
// Target languages (defaults to Arabic - change as needed!)
'target_languages' => env('TRANSLATION_TARGET_LANGS', ['ar']),
// File format: 'php' (Laravel default) or 'json'
'file_format' => env('TRANSLATION_FILE_FORMAT', 'php'),
// Enable automatic translation
'auto_translate' => env('TRANSLATION_AUTO_TRANSLATE', false),
// Translation service configurations
'services' => [
'libretranslate' => [], // No config needed - FREE!
'mymemory' => [], // No config needed - FREE!
'google' => [
'api_key' => env('GOOGLE_TRANSLATE_API_KEY', null),
],
// ... other services
],
];
Add these to your .env
file:
TRANSLATION_SERVICE=libretranslate # FREE - No API key needed!
TRANSLATION_SOURCE_LANG=en
TRANSLATION_AUTO_TRANSLATE=true
# No API key needed for free services!
π No billing, no API keys, no setup required! The package uses free translation services by default.
π Need help getting a Google Translate API key? Check out our Quick Setup Guide for step-by-step instructions!
Scan for missing translations:
php artisan translations:sync
Enable automatic translation with the --translate
flag:
php artisan translations:sync --translate
# Auto-approve all translations without confirmation
php artisan translations:sync --auto --translate
# Use a specific translation service
php artisan translations:sync --translate --service=google
# Set source and target languages
php artisan translations:sync --translate --source=en --targets=ar,en
# Or use the --locales alias
php artisan translations:sync --translate --locales=ar,en
# Dry run (preview changes without making them)
php artisan translations:sync --dry-run
# Show translation statistics
php artisan translations:sync --stats
# Use PHP files instead of JSON
php artisan translations:sync --format=php
Completely free, open-source translation service
TRANSLATION_SERVICE=libretranslate
- β No API key required
- β No billing needed
- β Open source and self-hosted
- β Supports 50+ languages
- β Fast and reliable
Community-driven translation service
TRANSLATION_SERVICE=mymemory
- β No API key required
- β No billing needed
- β Community translations
- β Supports 80+ languages
Professional translation service
TRANSLATION_SERVICE=google
GOOGLE_TRANSLATE_API_KEY=your-api-key
- β High quality translations
- β Supports all major languages
β οΈ Requires billing setupβ οΈ API key required
π Need help with Google Translate API? Check out our Quick Setup Guide
Follow these steps to obtain your Google Translate API key:
-
Go to Google Cloud Console
- Visit Google Cloud Console
- Sign in with your Google account
-
Create or Select a Project
- Click on the project dropdown at the top
- Either select an existing project or click "New Project"
- If creating new: Enter project name and click "Create"
-
Enable the Cloud Translation API
- In the left sidebar, go to "APIs & Services" > "Library"
- Search for "Cloud Translation API"
- Click on "Cloud Translation API" from the results
- Click the "Enable" button
-
Create API Credentials
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "API key"
- Your API key will be generated and displayed
- Important: Copy and save this key securely
-
Secure Your API Key (Recommended)
- Click on the API key name to edit it
- Under "API restrictions", select "Restrict key"
- Choose "Cloud Translation API" from the list
- Under "Application restrictions", you can:
- Set HTTP referrers for web apps
- Set IP addresses for server apps
- Or leave unrestricted for development
-
Set Up Billing (Required)
- Google Translate API requires a billing account
- Go to "Billing" in the left sidebar
- Link a billing account or create a new one
- Note: Google provides free credits for new accounts
-
Add to Your Environment
GOOGLE_TRANSLATE_API_KEY=AIzaSyD...your-actual-key-here
- Free Tier: $300 in free credits for new Google Cloud accounts
- Cost: $20 per 1 million characters translated
- Monthly Free Usage: First 500,000 characters per month are free
- Check current pricing at Google Cloud Pricing
- Default Quota: 1,000,000 characters per 100 seconds
- Character Limit: 30,000 characters per request
- Rate Limit: Can be increased by requesting quota increases
For development and testing
TRANSLATION_SERVICE=dummy
The dummy service is useful for testing and development. It simply appends the target language code to the original text.
The package automatically detects these translation patterns in your code:
__('key')
- Laravel's__()
helper@lang('key')
- Blade directivetrans('key')
- Laravel'strans()
helperLang::get('key')
- Facade usage
{
"Welcome": "Bienvenido",
"Hello World": "Hola Mundo"
}
<?php
return [
'Welcome' => 'Bienvenido',
'Hello World' => 'Hola Mundo',
];
Get detailed statistics about your translation coverage:
php artisan translations:sync --stats
This shows:
- Total translation keys found
- Missing translations
- Coverage percentage
- Translation service status
The package provides context for each missing translation, showing:
- Files where the translation key was found
- Code context around the usage
- Multiple occurrences across your application
The package intelligently handles Laravel translation placeholders:
// Original
__('Welcome :name to our site')
// After translation (Spanish)
'Welcome :name to our site' => 'Bienvenido :name a nuestro sitio'
// In your Blade template
<h1>{{ __('Welcome to our site') }}</h1>
<p>{{ __('This is a new feature') }}</p>
// In your controller
return response()->json([
'message' => __('Data saved successfully')
]);
Run the sync command:
php artisan translations:sync --translate --auto
This will:
- Detect the missing translations
- Automatically translate them to your configured target languages
- Save them to your translation files
// config/translation-sync.php
return [
'service' => 'google',
'source_language' => 'en',
'target_languages' => ['es', 'fr', 'de', 'it'],
'auto_translate' => true,
'scan_paths' => [
'app',
'resources',
'routes',
'custom-modules', // Add custom paths
],
'patterns' => [
"/__\(['\"](.+?)['\"]\)/",
"/@lang\(['\"](.+?)['\"]\)/",
"/trans\(['\"](.+?)['\"]\)/",
"/Lang::get\(['\"](.+?)['\"]\)/",
"/CustomHelper::translate\(['\"](.+?)['\"]\)/", // Custom pattern
],
];
Problem: "Google Translate service is not properly configured"
Solutions:
- Verify your API key is correct in
.env
file - Ensure the Cloud Translation API is enabled in Google Cloud Console
- Check that billing is set up for your Google Cloud project
- Verify your API key has the correct restrictions (if any)
Test your API key:
# Test if your API key works
curl -X POST \
"https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY" \
-d "q=Hello World&target=es&source=en"
Problem: "Failed to create translation file" or permission errors
Solutions:
# Make sure Laravel can write to the lang directory
chmod -R 755 resources/lang/
chown -R www-data:www-data resources/lang/ # On Linux servers
Problem: Known untranslated strings are not being found
Solutions:
- Check if your translation patterns are in the scan paths
- Verify file extensions are included in configuration
- Use custom patterns for non-standard translation methods:
// config/translation-sync.php
'patterns' => [
"/__\(['\"](.+?)['\"]\)/",
"/@lang\(['\"](.+?)['\"]\)/",
"/YourCustomHelper::trans\(['\"](.+?)['\"]\)/", // Add custom patterns
],
Problem: "Quota exceeded" or rate limit errors
Solutions:
- Check your Google Cloud Console quotas
- Request quota increases if needed
- Use smaller batches with the
--dry-run
flag first - Consider using the dummy service for development:
TRANSLATION_SERVICE=dummy # For development/testing
Problem: Package installation fails
Solutions:
# Install required dependencies
composer require guzzlehttp/guzzle
composer require stichoza/google-translate-php
# If you get version conflicts, try:
composer update --with-dependencies
Problem: Configuration changes not taking effect
Solutions:
# Clear config cache
php artisan config:clear
php artisan config:cache
# Re-publish config if needed
php artisan vendor:publish --tag=translation-sync-config --force
# Check translation statistics
php artisan translations:sync --stats
# See what would be translated without making changes
php artisan translations:sync --dry-run
# Test with dummy service
php artisan translations:sync --service=dummy --translate
# Check service status
php artisan tinker
# Then run: app(Nabila\TranslationSync\Services\TranslationWriter::class)->isTranslationServiceAvailable()
Create a simple test to validate your setup:
// In tinker or a test route
use Nabila\TranslationSync\Services\Translation\TranslationServiceFactory;
$service = TranslationServiceFactory::create('google');
if ($service->isConfigured()) {
echo "β
Google Translate is configured correctly\n";
$result = $service->translate('Hello', 'es');
echo "Test translation: {$result}\n";
} else {
echo "β Google Translate is not configured\n";
}
The package includes comprehensive error handling:
- Translation Service Errors: Falls back to original text or configured fallback strategy
- File Permission Issues: Clear error messages with suggested solutions
- API Quota Limits: Graceful handling with retry suggestions
- Network Issues: Timeout handling and fallback options
Contributions are welcome! Please feel free to submit a Pull Request.
This package is open-sourced software licensed under the MIT license.
If you discover any security vulnerabilities or bugs, please send an e-mail to the package maintainer.
- π§ Broader Laravel Support: Now compatible with Laravel 9.x, 10.x, 11.x, and 12.x
- π¦ Updated Dependencies: More flexible version constraints for better compatibility
- π§ͺ Enhanced Testing: Support for multiple PHPUnit and Testbench versions
- β¨ FREE Translation Services: Added LibreTranslate and MyMemory (no API keys needed!)
- π Zero-Configuration: Works out-of-the-box with free services
- π¦ Packagist Ready: Proper versioning and composer configuration
- π§ Enhanced Configuration: LibreTranslate as default free service
- π― Improved Documentation: Clear installation and usage guides
- π§ͺ Better Testing: Support for free service testing
- π Basic translation key detection
- π Simple file writing capabilities
- π― Basic console command