PHP code example of aewebsolutions / laravel-translator

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

    

aewebsolutions / laravel-translator example snippets


//Set a route
Route::get('apple', [
  'locales' => ['en', 'es'],
  'as' => 'apple_path',
  'uses' => 'fruitsController@apple' 
]);

//Get a URL from a route name
route('es.apple_path');

//Get a translated text
echo tt('fruits.apple'); 

//Get a title for current locale
$article = App\Article::find(1);
echo $article->title;

class Kernel extends HttpKernel
{
    use \Translator\Traits\KernelRouterExtender;
    //etc.
}

'locales_available' => [  'es', 'en' , 'fr' ],

Route::get('apple', [
  'locales' => 'en',  
  'uses' => 'fruitsController@apple'
]);

Route::get('apple', ['locales' => ['en', 'es'] ,  'uses' => 'fruitsController@apple'  ]);
Route::get('peach', ['locales' => 'all' ,  'uses' => 'fruitsController@peach'  ]);

Route::group([ 'locales' =>  'en' , 'prefix' => 'fruits' ], function(){

    Route::get('apple/{color}', ['locales' => 'es', 'as' =>  'apple_path',  'uses' => 'fruitsController@apple' ]);
    
    Route::get('peach/{color}', [ 'as' =>  'peach_path',  'uses' => 'fruitsController@peach' ]);
    
});

    route('apple_path', ['color' => 'red']);

    route('es.apple_path', ['color' => 'red'] );
    route('es.apple_path', ['color' => 'red'], true, 'en' );

$url = routes('apple_path', ['color' => 'red'] );
echo $url->es;
echo $url->en;

URL::current('es');

echo tt('fruits.apple');  // output: apple
echo tt('es.fruits.apple'); // manzana

echo tt('fr.fruits.apple'); // apple
echo tt('fr.fruits.apple', [], false); // NULL

echo tt('messages.welcome');  //output: Hi, :name.
echo tt('messages.welcome', ['name' => 'John']);  //output: Hi, John.

echo Translator::choice('en.fruits.apple', 5, ['color' => 'red'], false);  // red apples.
echo tt('en.fruits.apple');  // :color apple|apples.

$texts = Transalor::texts('fruits.apple');
echo $texts->en; // apple
echo $texts->es; // manzana
echo $texts->fr; // NULL

Translator::create('es.fruits.peach', 'durazno');

Translator::create(‘fruits.peach‘, [
  'es' => 'durazno', 
  'en' => 'peach',
  'fr' => 'pêche'
]);

Translator::create('fruits.peach', [
  'es' => 'durazno',  
  'en' => 'peach',
  'fr' => 'pêche'
 ], [
  'type' => 'infotext',
  'description' => 'Prunus persica‘s fruit'
]);

Translator::update('es.fruits.peach', 'melocotón');
Translator::update('fruits.peach', [
  'es' => 'melocotón',
  'en' => 'peach'
],[
  'type' => 'information'
]);

// Change the whole group name:
Translator::updateGroupNeedle('fruits', 'juicy_fruits');

// Change the needle, but not the group:
Translator::updateGroupNeedle('fruits.peach', 'fruits.yellow_peach');

//Change a single group.needle:
Translator::updateGroupNeedle('fruits.peach', 'juicy_fruits.peach');

//Delete the whole group
Translator::delete('fruits'); 

//Delete the group.needle for all locales
Translator::delete('fruits.apple');

//Delete a group.needle for a specific locale
Translator::delete('es.fruits.apple');

$article = App\Article::find(1);
echo $article->title; 
// output would be 'My title' if locale were 'en', 
// but 'Mi título' if locale were 'es';

Schema::create('articles', function ($table) {
    $table->increments('id');
    $table->text('body');
    $table->string('title');
    $table->timestamps();

    $table->localize([ 'title', 'body' ]);
});

class Article extends Translator\Eloquent\Model
{
    protected $translatable = ['title', 'body'];

    protected $fillable = ['title', 'body'];

}

$article = App\Article::find(1);
echo $article->title; // output 'My Title'

$title = $article->trans('title');
echo $title->es; // output 'Mi título

//Modify an article
$article = App\Article::find(1);
$article->title = [
    'es' => 'Mi título en español',
    'en' => 'My Title in English'
];
$article->save();

//Insert an article
$article = new App\Article;
$article->fill([
    'title' => [
        'es' => 'Mi título en español',
        'en' => 'My Title in English'
    ]
]);
$article->save();

php artisan vendor:publish --provider="Translator\TranslatorServiceProvider"

php artisan migrate