1. Go to this page and download the library: Download administrcms/localization 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/ */
// Person.php
namespace App\Models;
use Administr\Localization\Models\Translatable;
class Person extends Translatable
{
protected $table = 'people';
protected $fillable = ['is_visible', 'name', 'position'];
protected $translatable = ['name', 'position'];
}
// PersonTranslation.php
use Illuminate\Database\Eloquent\Model;
class PersonTranslation extends Model
{
protected $table = 'people_translations';
protected $fillable = ['language_id', 'name', 'position'];
}
// Get the first model with translation in the current app locale
$person = Person::translated()->first();
// Translated in the language with id of 1
$person = Person::translated(1)->first();
// Translated in the language with code of 'bg'.
// Have in mind that when you do it like this,
// an additional query will be made to find the language id.
$person = Person::translated('bg')->first();
$person->name; // You can access the translation model properties through the main model
// Create an instance of the main model
// with a translation in the current locale.
// If you do not pass translatable fields,
// only the main model will be persisted.
Person::create([
'name' => 'Miroslav Vitanov',
'position' => 'Developer',
]);
// Updates the model and the translation that was
// created in the current locale.
Person::find(1)->update([
'name' => 'Miroslav Vitanov',
'position' => 'PHP Developer',
]);
// Translate a model in another language.
// You can pass a locale code or language id.
Person::find(1)->translate('bg')
->fill([
'name' => 'Мирослав Витанов',
'position' => 'Програмист',
])
->save();
// Check if any translations exist
$person->hasTranslations();
// Check if a translation with language id exists
$person->isTranslatedIn(1);
// Get all translations
$person->translations;
// When a model is serialized to an array,
// it will
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.