secretwebmaster/wncms-translatable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

secretwebmaster/wncms-translatable

最新稳定版本:v1.4.1

Composer 安装命令:

composer require secretwebmaster/wncms-translatable

包简介

Make any laravel model translatable

README 文档

README

A Laravel package to add translation capabilities to any Eloquent model by storing translations in a separate table with a polymorphic relationship. Easily manage translations for specified fields in multiple locales.

Features

  • Translates specified model fields
  • Stores translations in a separate translations table
  • Polymorphic relationship to support multiple models
  • Easy to use with a simple trait

Installation

Step 1. Install the package via Composer:

composer require secretwebmaster/wncms-translatable

Step 2. Publish the migration file:

php artisan vendor:publish --provider="Wncms\Translatable\TranslatableServiceProvider" --tag="translatable-migrations"

Step 3. Run the migration to create the translations table:

php artisan migrate

Step 4. Setup your model

Add the HasTranslations trait to your model To make any model translatable, simply add the HasTranslations trait to your Eloquent model and define the $translatable property with the list of fields that should be translatable.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Wncms\Translatable\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    // Define the fields that can be translated
    public $translatable = ['title', 'description'];

    protected $fillable = ['title', 'description'];
}

Usage

Setting Translations

Create your model and call setTranslation()

$post = Post::find(1);

// Set translations for 'title' and 'description'
$post = Post::create([
    'title' => 'Hello World',
    'description' => 'Description in English',
]);

$post->setTranslation('title', 'fr', 'Bonjour le monde');
$post->setTranslation('description', 'fr', 'Description en Français');

Getting Translations

When retrieving translatable fields, the package automatically returns the value based on the current locale of the application:

// Get the 'title' based on the app's current locale
echo $post->title; // Outputs 'Hello World' or 'Bonjour le monde' depending on the locale

You can also retrieve translations for a specific locale:

// Get the 'title' in French
echo $post->getTranslation('title', 'fr'); // Outputs 'Bonjour le monde'

// Get the 'description' in English
echo $post->getTranslation('description', 'en'); // Outputs 'Description in English'

You can get available translation field of a model:

$post->getTranslatable() // Outputs an array of translatable fields

Table Structure

The package will create a translations table with the following structure:

Field Type Description
id bigint Primary key
translatable_type string Model class name (e.g., App\Models\Post)
translatable_id bigint The ID of the related model instance
field string The name of the field being translated
locale string Locale for the translation (e.g., en, fr)
value text The translated value
created_at timestamp Timestamp for when the translation was created
updated_at timestamp Timestamp for when the translation was updated

Update log

v1.4.0 2026-04-08

  • Laravel 13 baseline (illuminate/database / illuminate/support ^13.0)
  • PHP 8.4 baseline
  • Testbench 11 / PHPUnit 12 compatibility
  • fix default-locale skip logic so model save/update is not blocked

v1.2.8 2025-08-20

  • fix typo in Feature test
  • add save_empty_translations option in config file

统计信息

  • 总下载量: 239
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固