PHPackages                             jiumikeji/jiumi-translatable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jiumikeji/jiumi-translatable

ActiveLibrary[Database &amp; ORM](/categories/database)

jiumikeji/jiumi-translatable
============================

The Hyperf Multilingual Model package.

1.1.2(2y ago)072↓50%MITPHPPHP &gt;=8.0

Since Aug 22Pushed 2y ago1 watchersCompare

[ Source](https://github.com/jiumikeji/jiumi-translatable)[ Packagist](https://packagist.org/packages/jiumikeji/jiumi-translatable)[ RSS](/packages/jiumikeji-jiumi-translatable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (12)Versions (4)Used By (0)

Hyperf 多语言模型组件
==============

[](#hyperf-多语言模型组件)

该组件为数据库模型提供实现多语言的能力。

ps: 组件从 `hyperf-ext/translatable` fork而来，原组件只支持到hyperf2.2版本。作者目前长时间未更新。

安装
--

[](#安装)

```
composer require jiumikeji/jiumi-translatable
```

发布配置
----

[](#发布配置)

```
php bin/hyperf.php vendor:publish jiumikeji/jiumi-translatable
```

> 文件位于 `config/autoload/translatable.php`。

> 注意，该组件依赖 `hyperf/translation`，不要忘记也要发布其配置并按需设置。

配置
--

[](#配置)

```
[
    /*
    |--------------------------------------------------------------------------
    | 应用语言列表
    |--------------------------------------------------------------------------
    |
    | 包含应用所有可用语言的数组。
    |
    */
    'locales' => [
        'en',
        'fr',
        'zh' => [
            'CN',
            'TW',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | 语言分隔符
    |--------------------------------------------------------------------------
    |
    | 用于在定义可用语言时连接语言和国家（或地区）之间的字符串。例如，如果设置为 `_`，
    | 则“中文（中国）”的语言会以 `zh_CN` 的形式存储到数据库中。
    |
    */
    'locale_separator' => '_',

    /*
    |--------------------------------------------------------------------------
    | 默认语言
    |--------------------------------------------------------------------------
    |
    | 指定要使用的默认语言。该组件默认使用 `hyperf/translation` 组件的语言设置。
    | 如果出于某种原因想要覆盖此默认设置，则可以在此处设置要使用的默认值。
    | 如果在此处设置一个值，它将仅使用当前的值，而不会回退到 `hyperf/translation`
    | 组件的设置。
    |
    */
    'locale' => null,

    /*
    |--------------------------------------------------------------------------
    | 使用回退
    |--------------------------------------------------------------------------
    |
    | 设置是否启用回退语言。为了增加灵活性，定义多语言模型的 $useTranslationFallback
    | 属性可覆盖此处的设置。
    |
    */
    'use_fallback' => false,

    /*
    |--------------------------------------------------------------------------
    | 使用属性回退
    |--------------------------------------------------------------------------
    |
    | 如果所选语言的属性为空，则属性回退特性将返回回退语言的翻译后的值。
    | 注意，必须启用 `use_fallback`。
    |
     */
    'use_property_fallback' => true,

    /*
    |--------------------------------------------------------------------------
    | 回退语言
    |--------------------------------------------------------------------------
    |
    | 回退语言是当请求的翻译不存在时，返回设置的回退语言的翻译。
    | 如要禁用，请设置为 `false`。如果设置为 `null`，将会遍历语言列表中配置的所有语言，
    | 直到找到第一个有效的翻译，否则会遍历完整个列表。
    | 语言列表从上到下遍历，对于基于国家（或地区）的语言，会先检查简单的语言代码。
    | 因此，例如在检查 `zh_CN` 之前会先检查 `zh`。
    |
    */
    'fallback_locale' => 'en',

    /*
    |--------------------------------------------------------------------------
    | Translation 模型命名空间
    |--------------------------------------------------------------------------
    |
    | 定义默认的 'Translation` 类命名空间。
    | 例如，如果要使用 `App\Translations\CountryTranslation` 而不是 `App\CountryTranslation`，
    | 请设置为 `App\Translations`。
    |
    */
    'translation_model_namespace' => null,

    /*
    |--------------------------------------------------------------------------
    | Translation 模型后缀
    |--------------------------------------------------------------------------
    |
    | 定义默认的 `Translation` 类名后缀。
    | 例如，如果要使用 `CountryTrans` 而不是 `CountryTranslation`，请设置为 `Trans`。
    |
    */
    'translation_suffix' => 'Translation',

    /*
    |--------------------------------------------------------------------------
    | 语言字段名
    |--------------------------------------------------------------------------
    |
    | 定义 `Translation` 模型的 'locale' 字段名。
    |
    */
    'locale_key' => 'locale',

    /*
    |--------------------------------------------------------------------------
    | 转换为数组时始终加载 Translation
    |--------------------------------------------------------------------------
    |
    | 将其设置为 `false` 可以提升性能，但是在使用 `toArray()` 时不会返回翻译，
    | 除非已经加载了关联的 `Translation` 模型。
    |
     */
    'to_array_always_loads_translations' => true,

    /*
    |--------------------------------------------------------------------------
    | 配置 RuleFactory 默认行为
    |--------------------------------------------------------------------------
    |
    | 用于控制 RuleFactory 行为的默认值。
    | 此处你可以为整个应用设置自己的默认格式和定界符。
    |
     */
    'rule_factory' => [
        'format' => \Jiumi\Translatable\Validation\RuleFactory::FORMAT_ARRAY,
        'prefix' => '%',
        'suffix' => '%',
    ],
];
```

使用示例
----

[](#使用示例)

### 迁移

[](#迁移)

create\_posts\_table.php

```
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');
    }
}
```

create\_post\_translations\_table

```
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');
    }
}
```

### 模型

[](#模型)

Post.php

```
use Hyperf\Database\Model\Model;
use Jiumi\Translatable\Contracts\TranslatableInterface;
use Jiumi\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'];
}
```

PostTranslation.php

```
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\Context\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
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

994d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cba3f821e5d8ea2546ad0fa2b0bca15adba0b4781de5561140c2f53b175a638e?d=identicon)[fu0526](/maintainers/fu0526)

---

Top Contributors

[![jiumikeji](https://avatars.githubusercontent.com/u/31468309?v=4)](https://github.com/jiumikeji "jiumikeji (4 commits)")

---

Tags

phplanguagedatabasetranslationhyperf

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jiumikeji-jiumi-translatable/health.svg)

```
[![Health](https://phpackages.com/badges/jiumikeji-jiumi-translatable/health.svg)](https://phpackages.com/packages/jiumikeji-jiumi-translatable)
```

###  Alternatives

[codezero/laravel-unique-translation

Check if a translated value in a JSON column is unique in the database.

186965.1k7](/packages/codezero-laravel-unique-translation)[hyperf/database-pgsql

A pgsql handler for hyperf/database.

12282.0k13](/packages/hyperf-database-pgsql)[jonas-elias/hyperf-oracle

A oracle handler for hyperf/database.

102.0k](/packages/jonas-elias-hyperf-oracle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
