PHP code example of subhashladumor1 / laravel-translate
1. Go to this page and download the library: Download subhashladumor1/laravel-translate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
subhashladumor1 / laravel-translate example snippets
// Simple translation
echo translateText('Hello World', 'es');
// Output: Hola Mundo
// With auto-detection
echo translate('Bonjour', 'en');
// Output: Hello
use Subhashladumor1\Translate\Facades\Translate;
$translation = Translate::translate('Good morning', 'fr');
// Output: Bonjour
namespace App\Http\Controllers;
use App\Models\Product;
use Subhashladumor1\Translate\Facades\Translate;
class ProductController extends Controller
{
public function translateProducts()
{
$products = Product::all();
$targetLanguages = ['es', 'fr', 'de', 'it'];
foreach ($products as $product) {
foreach ($targetLanguages as $lang) {
ProductTranslation::updateOrCreate([
'product_id' => $product->id,
'language' => $lang,
], [
'name' => translateText($product->name, $lang),
'description' => translateText($product->description, $lang),
'features' => translate_array($product->features, $lang),
]);
}
}
return response()->json(['message' => 'Products translated!']);
}
}
public function sendContactForm(Request $request)
{
$userLang = $request->user()->preferred_language ?? 'en';
// Send confirmation in user's language
$confirmationMessage = translateText(
'Thank you for contacting us! We will respond within 24 hours.',
$userLang
);
// Send notification to admin in default language
$adminNotification = translateText(
'New contact form submission from {name}',
config('app.locale')
);
return response()->json([
'message' => $confirmationMessage
]);
}
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
// Auto-sync translations daily at 2 AM
$schedule->command('translate:sync --source=en --target=es,fr,de')
->dailyAt('02:00')
->emailOutputOnFailure('[email protected]');
// Clear old cache weekly
$schedule->command('translate:clear-cache')
->weekly()
->sundays()
->at('03:00');
}
bash
# Test all translation services
php artisan translate:test
# Test specific service
php artisan translate:test --service=lingva
# Custom text and language
php artisan translate:test --text="Good morning" --target=fr
bash
# Translate Laravel language file
php artisan translate:file resources/lang/en/messages.php es
# Custom output path
php artisan translate:file resources/lang/en/auth.php fr --output=lang/fr/auth.php
# Export as JSON
php artisan translate:file resources/lang/en/validation.php de --format=json
bash
# Sync to multiple languages
php artisan translate:sync --source=en --target=es,fr,de
# Force overwrite existing translations
php artisan translate:sync --source=en --target=es --force
# Custom language directory
php artisan translate:sync --source=en --target=fr --path=lang