ابزاری برای ساخت سایت های چندزبانه در لاراول
ﺯﻣﺎﻥ ﻣﻄﺎﻟﻌﻪ: 3 دقیقه

ابزاری برای ساخت سایت های چندزبانه در لاراول

اگر می خواهید ترجمه های مدل هاتون رو در دیتابیس ذخیره کنید این پکیج برای شماست.

laravel-translatable یک پکیج لاراولی برای مدل های قابل ترجمه است. هدفش حذف پیچیدگی دریافت و ذخیره سازی نمونه های مدل چندزبانه است. با این پکیج شما کمتر کد می زنید و ترجمه های شما به راحتی ذخیره و بازیابی می شوند.

پیش نمایش نحوه استفاده

ابتدا یک نمونه از نحوه استفاده از پکیج ملاحظه کنید سپس سراغ نصب و راه اندازی و تنظیمات اون خواهیم رفت.

برای دریافت عناصر ترجمه شده :

$greece = Country::where('code', 'gr')->first();
echo $greece->translate('en')->name; // Greece

App::setLocale('en');
echo $greece->name;     // Greece

App::setLocale('de');
echo $greece->name;     // Griechenland

ذخیره سازی عناصر :

$greece = Country::where('code', 'gr')->first();
echo $greece->translate('en')->name; // Greece
  
$greece->translate('en')->name = 'abc';
$greece->save();
 
$greece = Country::where('code', 'gr')->first();
echo $greece->translate('en')->name; // abc

پر کردن چندین ترجمه :

  $data = [
    'code' => 'gr',
    'en'  => ['name' => 'Greece'],
    'fr'  => ['name' => 'Grèce'],
  ];

  $greece = Country::create($data);
  
  echo $greece->translate('fr')->name; // Grèce

سازگاری با لاراول

نصب در 4 قدم

قدم اول : نصب پکیج

با استفاده از فایل composer.json یا اجرای دستور زیر در کنسول میتونید اون رو نصب کنید :

composer require dimsav/laravel-translatable

سپس کد زیر رو به Provider در مسیر congif/app.php اضافه کنید:

Dimsav\Translatable\TranslatableServiceProvider::class,

قدم دوم : Migration ها

در این مثال ما میخواهیم مدل Country رو ترجمه کنیم. پس به یک جدول اضافه به اسم country_translations نیاز داریم :

Schema::create('countries', function(Blueprint $table)

{

    $table->increments('id');

    $table->string('code');

    $table->timestamps();

});



Schema::create('country_translations', function(Blueprint $table)

{

    $table->increments('id');

    $table->integer('country_id')->unsigned();

    $table->string('name');

    $table->string('locale')->index();



    $table->unique(['country_id','locale']);

    $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');

});

قدم سوم : مدل ها

1 - مدل ترجمه شده Country باید Dimsav\Translatable\Translatable رو use کنه.

2 - مدل ترجمه شده ی اون CountryTranslation نام بگیره.

// models/Country.php

class Country extends Eloquent {

    use \Dimsav\Translatable\Translatable;


    public $translatedAttributes = ['name'];

    protected $fillable = ['code'];

    
    /**

     * The relations to eager load on every query.

     *

     * @var array

     */

    // (optionaly)

    // protected $with = ['translations'];

}


// models/CountryTranslation.php

class CountryTranslation extends Eloquent {

    public $timestamps = false;

    protected $fillable = ['name'];

}

آرایه ی translatedAttributes$ شامل اسامی فیلدهاست که در مدل Translation ترجمه شده.

قدم چهارم : Configuration

ما فایل پیکربندی رو به پروژه اضافه می کنیم.

لاراول *.5

php artisan vendor:publish --tag=translatable

لاراول *.4

php artisan config:publish dimsav/laravel-translatable

نکته : محدودیتی برای فرمت محل ها وجود نداره. برای نامگذاری از اونهایی که راحت ترید استفاده کنید مثلا "eng" به جای "en". نکته مهم تعریف محل ها و اختصاص نام به اونهاست.

پیکربندی

فایل پیکربندی

شما میتونید گزینه های سفارشی سازی بیشتری رو در فایل پیکربندی مشاهده کنید.

مدل ترجمه شده

برای اینکه مدل ترجمه شده شناخته بشه کلیدواژه Translation کنار نام مدل قرار میگیره. پس اگر مدل شما \MyApp\Models\Country باشه مدل پیشفرض ترجمه بصورت MyApp\Models\CountryTranslation خواهد بود. برای استفاده از یک کلاس سفارشی بعنوان مدل ترجمه, کلاس ترجمه رو بعنوان پارامتر تعریف کنید. برای مثال :

namespace MyApp\Models;

use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Country extends Eloquent
{
    use Translatable;

    public $translationModel = 'MyApp\Models\CountryAwesomeTranslation';
}

لیست ویژگی ها

لطفا ابتدا قدم های نصب رو مطالعه کنید تا بفهمید چه کلاس های نیاز هست.

متدهای موجود

// Before we get started, this is how we determine the default locale.
// It is set by laravel or other packages.
App::getLocale(); // 'fr' 

// To use this package, first we need an instance of our model
$germany = Country::where('code', 'de')->first();

// This returns an instance of CountryTranslation of using the default locale.
// So in this case, french. If no french translation is found, it returns null.
$translation = $germany->translate();

// It is possible to define a default locale per model by overriding the model constructor.
public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    $this->defaultLocale = 'de';
}


// It is also possible to define a default locale for our model on the fly:
$germany->setDefaultLocale('de');

// If an german translation exists, it returns an instance of 
// CountryTranslation. Otherwise it returns null.
$translation = $germany->translate('de');

// If a german translation doesn't exist, it attempts to get a translation  
// of the fallback language (see fallback locale section below).
$translation = $germany->translate('de', true);

// Alias of the above.
$translation = $germany->translateOrDefault('de');


// Returns instance of CountryTranslation of using the default locale.
// If no translation is found, it returns a fallback translation
// if enabled in the configuration.
$translation = $germany->getTranslation();

// If an german translation exists, it returns an instance of 
// CountryTranslation. Otherwise it returns null.
// Same as $germany->translate('de');
$translation = $germany->getTranslation('de', true);

// To set the translation for a field you can either update the translation model.
// Saving the model will also save all the related translations.
$germany->translate('en')->name = 'Germany';
$germany->save();

// Alternatively we can use the shortcut
$germany->{'name:en'} = 'Germany';
$germany->save();

// There are two ways of inserting mutliple translations into the database
// First, using the locale as array key.
$greece = $country->fill([
    'en'  => ['name' => 'Greece'],
    'fr'  => ['name' => 'Grèce'],
]);

// The second way is to use the following syntax.  
$greece = $country->fill([
    'name:en' => 'Greece',
    'name:fr' => 'Grèce',
]);

// Returns true/false if the model has translation about the current locale. 
$germany->hasTranslation();

// Returns true/false if the model has translation in french. 
$germany->hasTranslation('fr');

// If a german translation doesn't exist, it returns
// a new instance of CountryTranslation.
$translation = $germany->translateOrNew('de');

// Returns a new CountryTranslation instance for the selected
// language, and binds it to $germany
$translation = $germany->getNewTranslation('it');

// The eloquent model relationship. Do what you want with it ;) 
$germany->translations();

// Remove all translations linked to an object
$germany->deleteTranslations();

// Delete one or multiple translations
$germany->deleteTranslations('de');
$germany->deleteTranslations(['de', 'en']);

// Gel all the translations as array
$germany->getTranslationsArray();

// Returns
[
 'en' => ['name' => 'Germany'],
 'de' => ['name' => 'Deutschland'],
 'fr' => ['name' => 'Allemagne'],
];


// Creates a clone and clones the translations
$replicate = $germany->replicateWithTranslations(); 

متدهای مختلفی در این پکیج وجود داره که کار شما رو راحت میکنه. برای مشاهده لیست کامل متدهای موجود و scope ها به این لینک مراجعه کنید.

چه امتیازی برای این مقاله میدهید؟

خیلی بد
بد
متوسط
خوب
عالی
در انتظار ثبت رای

دیدگاه و پرسش

برای ارسال دیدگاه لازم است وارد شده یا ثبت‌نام کنید ورود یا ثبت‌نام

در حال دریافت نظرات از سرور، لطفا منتظر بمانید

در حال دریافت نظرات از سرور، لطفا منتظر بمانید

مقالات برگزیده

مقالات برگزیده را از این قسمت میتوانید ببینید

مشاهده همه مقالات