fevrok/laravel-translatable 问题修复 & 功能扩展

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

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

fevrok/laravel-translatable

Composer 安装命令:

composer require fevrok/laravel-translatable

包简介

This package allows you to translate your models fields.

README 文档

README

It's a Laravel model columns translation manager

How it works?

Laravel Translatable current working model

Installation

You can install the package via composer:

composer require fevrok/laravel-translatable

If you have Laravel 5.5 and up The package will automatically register itself.

else you have to add the service provider to config/app.php

Fevrok\Translatable\TranslatableServiceProvider::class,

publish config file and migration.

php artisan vendor:publish --provider="Fevrok\Translatable\TranslatableServiceProvider"

next migrate translations table

php artisan migrate

Setup

After finishing the installation you can open config/translatable.php:

return [
   /**
    * Set whether or not the translations is enbaled.
    */
   'enabled' => true,

   /**
    * Select default language
    */
   'locale' => 'en',

];

And update your config accordingly.

Making a model translatable

The required steps to make a model translatable are:

  • use the Fevrok\Translatable\Translatable trait.
  • define the model translatable fields in $translatable property.

Here's an example of a prepared model:

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

class Item extends Model
{
    use Translatable;

    /**
      * The attributes that are Translatable.
      *
      * @var array
      */
    protected $translatable = [
        'name',
        'description',
    ];
}

Custom translations model

To get started, publish the assets again this will create new migration update table name to your desire.

CustomTranslation.php

class CustomTranslation extends \Fevrok\Translatable\Models\Translation
{
    protected $table = 'custom_translations';
}

Add $translations_model property and give it to the model you wanna customize,

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

class Item extends Model
{
    use Translatable;

    /**
      * The attributes that are Translatable.
      *
      * @var array
      */
    protected $translatable = [
        'name'
    ];

    /**
      * The model used to get translatios.
      *
      * @var string
      */
    protected $translations_model = CustomTranslation::class;
}

Usage

Eager-load translations

// Loads all translations
$posts = Post::with('translations')->get();

// Loads all translations
$posts = Post::all();
$posts->load('translations');

// Loads all translations
$posts = Post::withTranslations()->get();

// Loads specific locales translations
$posts = Post::withTranslations(['en', 'da'])->get();

// Loads specific locale translations
$posts = Post::withTranslations('da')->get();

// Loads current locale translations
$posts = Post::withTranslations('da')->get();

Get default language value

echo $post->title;

Get translated value

echo $post->getTranslatedAttribute('title', 'locale', 'fallbackLocale');

If you do not define locale, the current application locale will be used. You can pass in your own locale as a string. If you do not define fallbackLocale, the current application fallback locale will be used. You can pass your own locale as a string. If you want to turn the fallback locale off, pass false. If no values are found for the model for a specific attribute, either for the locale or the fallback, it will set that attribute to null.

Translate the whole model

$post = $post->translate('locale', 'fallbackLocale');
echo $post->title;
echo $post->body;

// You can also run the `translate` method on the Eloquent collection
// to translate all models in the collection.
$posts = $posts->translate('locale', 'fallbackLocale');
echo $posts[0]->title;

If you do not define locale, the current application locale will be used. You can pass in your own locale as a string. If you do not define fallbackLocale, the current application fallback locale will be used. You can pass in your own locale as a string. If you want to turn the fallback locale off, pass false. If no values are found for the model for a specific attribute, either for the locale or the fallback, it will set that attribute to null.

Check if model is translatable

// with string
if (Translatable::translatable(Post::class)) {
    // it's translatable
}

// with object of Model or Collection
if (Translatable::translatable($post)) {
    // it's translatable
}

Set attribute translations

$post = $post->translate('da');
$post->title = 'foobar';
$post->save();

This will update or create the translation for title of the post with the locale da. Please note that if a modified attribute is not translatable, then it will make the changes directly to the model itself. Meaning that it will overwrite the attribute in the language set as default.

Query translatable Models

To search for a translated value, you can use the whereTranslation method. For example, to search for the slug of a post, you'd use

$page = Page::whereTranslation('slug', 'my-translated-slug');
// Is the same as
$page = Page::whereTranslation('slug', '=', 'my-translated-slug');
// Search only locale en, de and the default locale
$page = Page::whereTranslation('slug', '=', 'my-translated-slug', ['en', 'de']);
// Search only locale en and de
$page = Page::whereTranslation('slug', '=', 'my-translated-slug', ['en', 'de'], false);

whereTranslation accepts the following parameter:

  • field the field you want to search in
  • operator the operator. Defaults to =. Also can be the value (Same as where)
  • value the value you want to search for
  • locales the locales you want to search in as an array. Leave as null if you want to search all locales
  • default also search in the default value/locale. Defaults to true.

Maintainers


Abdellah Chadidi

fevrok/laravel-translatable 适用场景与选型建议

fevrok/laravel-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48 次下载、GitHub Stars 达 70, 最近一次更新时间为 2021 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translation」 「translatable」 「model」 「laravel」 「translate」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 fevrok/laravel-translatable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 70
  • Watchers: 6
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-26