1. Go to this page and download the library: Download hyperf-ext/translatable 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/ */
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
public function up(): void
{
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->string('author');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
}
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
class CreatePostTranslationsTable extends Migration
{
public function up(): void
{
Schema::create('post_translations', function(Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->string('locale')->index();
$table->string('title');
$table->text('content');
$table->unique(['post_id', 'locale']);
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
}
public function down(): void
{
Schema::dropIfExists('post_translations');
}
}
use Hyperf\Database\Model\Model;
use HyperfExt\Translatable\Contracts\TranslatableInterface;
use HyperfExt\Translatable\Translatable;
class Post extends Model implements TranslatableInterface
{
use Translatable;
// 转换为数组时是否始终加载 Translation。默认为 `null`。
// 值为 `null` 时使用配置文件的 `to_array_always_loads_translations` 值。
protected static $autoloadTranslations = null;
// 删除记录的同时删除关联的翻译。默认为 `false`。
protected static $deleteTranslationsCascade = false;
// 配置该属性会覆盖配置文件的 `use_fallback` 值。
//protected $useTranslationFallback = false;
// 配置对应的 Translation 模型的多语言字段列表
public $translatedAttributes = ['title', 'content'];
protected $fillable = ['author'];
}
use Hyperf\Database\Model\Model;
class PostTranslation extends Model
{
public $timestamps = false;
// 多语言字段列表
protected $fillable = ['title', 'content'];
}
use App\Post;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Utils\ApplicationContext;
$post = Post::query()->first();
echo $post->translate('en')->title; // My first post
$translator = ApplicationContext::getContainer()->get(TranslatorInterface::class);
$translator->setLocale('en');
echo $post->title; // My first post
$translator->ssetLocale('de');
echo $post->title; // Mein erster Post
use App\Post;
$post = Post::query()->first();
echo $post->translate('en')->title; // My first post
$post->translate('en')->title = 'My cool post';
$post->save();
$post = Post::query()->first();
echo $post->translate('en')->title; // My cool post
use App\Post;
$data = [
'author' => 'Gummibeer',
'en' => ['title' => 'My first post'],
'fr' => ['title' => 'Mon premier post'],
];
$post = new Post();
$post->fill($data);
$post->save();
echo $post->translate('fr')->title; // Mon premier post