blackstone/laravel-model-translations-sync 问题修复 & 功能扩展

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

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

blackstone/laravel-model-translations-sync

Composer 安装命令:

composer require blackstone/laravel-model-translations-sync

包简介

Synchronize Laravel model translations between models, language_lines, files and external translation flows.

README 文档

README

Laravel package for synchronizing translations between:

  • translatable Eloquent model JSON attributes via spatie/laravel-translatable
  • language_lines via spatie/laravel-translation-loader
  • local Laravel lang files
  • external translation services such as Crowdin, Lokalise or custom project commands

Pipeline target:

Models -> DB (language_lines) -> external service -> DB -> Models -> Files

Requirements

  • PHP 8.3, 8.4+
  • Laravel 11 or 12
  • spatie/laravel-translatable
  • spatie/laravel-translation-loader

Installation

composer require blackstone/laravel-model-translations-sync

Publish config:

php artisan vendor:publish --tag=model-translations-config

Model setup

Add HasTranslations from Spatie and the package trait:

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use BlackstonePro\ModelTranslationsSync\Traits\ModelTranslatable;

class Product extends Model
{
    use HasTranslations;
    use ModelTranslatable;

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

    public array $translatable = ['title', 'description'];
}

Default behavior:

  • getModelTranslationNamespace() returns snake_case model basename
  • getTranslatableAttributesForSync() returns $this->translatable

You can override both methods in the model if needed.

Storage format in language_lines

Model translations are stored as:

  • group = models.{namespace}
  • key = {id}.{attribute}
  • text = {"en":"...", "fr":"..."}

Example:

group = models.product
key = 12.title
text = {"en":"iPhone","fr":"iPhone"}

Configuration

Published config file: config/model-translations.php

Main options:

  • models.auto_discover: discover models from configured paths
  • models.paths: directories for model discovery
  • models.list: explicit model class list
  • models.namespace_map: manual namespace to model mapping
  • locales: supported locales for sync
  • default_locale: fallback locale
  • ignore_groups: groups ignored during file export
  • export_path: destination for generated lang files
  • export.overwrite, export.pretty_print, export.sort_keys
  • sync.stop_on_error: stop or continue when a pipeline step fails
  • sync.pipeline: ordered list of artisan commands for the sync pipeline

Commands

Export models to language_lines:

php artisan translations:export-models {model?} {--fresh} {--chunk=500} {--dry-run}

Import model translations from language_lines:

php artisan translations:import-models {--locale=} {--group=} {--chunk=500} {--dry-run}

Export DB translations to local files:

php artisan translations:export-files {--dry-run}

Import local files back into DB:

php artisan translations:import-files {--dry-run}

Run the full pipeline:

php artisan translations:sync {--dry-run}

Pipeline is config-driven. Example:

'sync' => [
    'stop_on_error' => true,
    'pipeline' => [
        ['command' => 'translations:export-models', 'enabled' => true],
        ['command' => 'crowdin:upload', 'enabled' => true],
        ['command' => 'crowdin:download', 'enabled' => true],
        ['command' => 'translations:import-models', 'enabled' => true],
        ['command' => 'translations:export-files', 'enabled' => true],
    ],
],

translations:sync executes enabled commands in order, warns about missing or failing steps, and stops only when sync.stop_on_error is true.

Generating translatable JSON migrations

The package can generate a snapshot-based Laravel migration for safely converting existing scalar/text translatable columns into JSON columns without runtime model scanning inside the migration.

php artisan translations:make-translatable-migration convert_translatable_fields_to_json --locale=en

Available options:

php artisan translations:make-translatable-migration
    {name?}
    {--paths=*}
    {--locale=en}
    {--chunk=500}
    {--force}

The command:

  • discovers models using Spatie\Translatable\HasTranslations
  • filters only real DB columns
  • writes a fixed snapshot of models, tables, primary keys and translatable attributes
  • generates a self-contained migration in database/migrations

The generated migration:

  • creates __json_tmp columns in up()
  • wraps old scalar values as {"<locale>":"value"}
  • renames temp JSON columns back to the original names
  • restores values back to text columns in down() using the chosen locale

Source of truth

  • translations:export-models: models are source of truth
  • translations:import-models: language_lines is source of truth
  • translations:export-files: language_lines is source of truth
  • translations:import-files: lang files are source of truth

File formats

Supported export/import targets:

  • resources/lang/{locale}/*.php
  • resources/lang/{locale}.json
  • resources/lang/{locale}/models.php

Model translations in models.php are structured as:

return [
    'product' => [
        12 => [
            'title' => 'iPhone',
            'description' => 'Smartphone',
        ],
    ],
];

Testing

composer test

blackstone/laravel-model-translations-sync 适用场景与选型建议

blackstone/laravel-model-translations-sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 blackstone/laravel-model-translations-sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 blackstone/laravel-model-translations-sync 我们能提供哪些服务?
定制开发 / 二次开发

基于 blackstone/laravel-model-translations-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-11