parfumix/eloquent-translatable

v1.1 2015-08-25 09:00 UTC

This package is auto-updated.

Last update: 2024-09-19 21:24:11 UTC


README

##介绍

Eloquent translatable 允许您轻松地在不同语言中翻译 eloquent 模型。此包与 https://github.com/parfumix/laravel-localization 强烈配合,并需要安装该包。

因此,在开始使用 eloquent translatable 之前,请先学习如何在您的网站上实现本地化。

安装

您可以使用 composer 包管理器进行安装。从控制台运行

  $ php composer.phar require parfumix/eloquent-translatable "v1.0"

或将以下内容添加到您的 composer.json 文件中

"parfumix/eloquent-translatable": "v1.0"

##基本用法

在开始使用可翻译包之前,您需要在您的 eloquent 模型上实现 Translatable 合约,并使用 TranslatableTrait

<?php

namespace App;

use Eloquent\Translatable\Translatable;
use Eloquent\Translatable\TranslatableTrait;
use Illuminate\Database\Eloquent\Model;

// Create page model .
class Page extends Model implements Translatable {

    use TranslatableTrait;
    
    // That attribute is not required, will try to detect class name. If you have one custom you can do it.
    protected $translationClass = PageTranslations::class;

}

// Create translation model for page .
class PageTranslations extends Model {

    public $timestamps = false;
    
    protected $fillable = ['page_id', 'language_id', 'title', 'description'];

}

并为您的 eloquent 模型创建翻译模型。

##如何使用

// Wil create and page and their translations
Page::create([
    'slug'    => 'title-slug',
    'en'       => [ 'title' => 'English title', 'description' => 'English description' ],
    'ru'       => [ 'title' => 'Russian title', 'description' => 'Russian description' ] 
]);

// Will access Page translation instance
Page::first()->translate('en') 

// will access translate title in english 
Page::first()->translate('en')->title

// Will check if page have translation in english
Page::first()->hasTranslation('en')

// Will delete english translation for current page 
Page::first()->removeTranslation('en')

// Will return all translation for current page instance 
Page::first()->translations