PHP code example of wazza / dom-translate

1. Go to this page and download the library: Download wazza/dom-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/ */

    

wazza / dom-translate example snippets


return [
        App\Providers\AppServiceProvider::class,
        Wazza\DomTranslate\Providers\DomTranslateServiceProvider::class,
];

    // ---------------
    // Register the default Blade directive - @transl8()
    // Only the phrase argument is ','fr')
    // Register the @transl8 directive separately
    Blade::directive('transl8', function ($string) {
        return "<?= app(" . TranslateController::class . "::class)->phrase({$string}); 

use Wazza\DomTranslate\Helpers\TranslateHelper;

class YourLanguageController extends Controller
{
    public function setLanguage(Request $request)
    {
        $langCode = $request->input('language');
        return TranslateHelper::setLanguage($langCode);
    }

    public function getLanguage()
    {
        return TranslateHelper::getLanguage();
    }
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Wazza\DomTranslate\Helpers\TranslateHelper;

class LanguageController extends Controller
{
    public function setLanguage(Request $request)
    {
        $request->validate([
            'language' => '

// Apply to specific routes
Route::group(['middleware' => ['dom-translate.locale']], function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
    Route::get('/profile', [ProfileController::class, 'show']);
});

// Or apply to individual routes
Route::get('/settings', [SettingsController::class, 'index'])
    ->middleware('dom-translate.locale');

// resources/lang/fr/validation.php

return [
    'sse e-mail valide.',
    // ... more translations
];

// resources/lang/fr/messages.php

return [
    'welcome' => 'Bienvenue',
    'goodbye' => 'Au revoir',
    // ... more translations
];



namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Wazza\DomTranslate\Helpers\TranslateHelper;

class CustomLocaleMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        // Custom logic: maybe check user's profile settings first
        $language = auth()->user()?->preferred_language
                    ?? TranslateHelper::currentDefinedLanguageCode();

        // Additional locale mapping if needed
        $localeMap = [
            'zh' => 'zh_CN',
            'pt' => 'pt_BR',
            // ... more mappings
        ];

        $locale = $localeMap[$language] ?? $language;
        app()->setLocale($locale);

        return $next($request);
    }
}

// Line 14 in 'wazza\dom-translate\config\dom_translate.php'
// Third-party translation service providers
'api' => [
    'provider' => env('DOM_TRANSLATE_PROVIDER', 'google'),
    'google' => [
        'controller' => "Wazza\DomTranslate\Controllers\ApiTranslate\GoogleTranslate",
        'endpoint' => "https://www.googleapis.com/language/translate/v2",
        'action' => "POST",
        'key' => env('DOM_TRANSLATE_GOOGLE_KEY', null), // https://console.cloud.google.com/apis/credentials
    ],
    // To contribute, fork the project and add more translation providers here, implementing CloudTranslateInterface
],
bash
composer vendor:publish --tag="dom-translate-config"
php artisan vendor:publish --tag="dom-translate-migrations"
php artisan migrate
bash
php artisan config:clear
php artisan config:cache
bash
   php artisan config:clear
   php artisan config:cache