jvdlaar/laravel-content-translation 问题修复 & 功能扩展

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

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

jvdlaar/laravel-content-translation

Composer 安装命令:

composer require jvdlaar/laravel-content-translation

包简介

A Laravel 5 package to translate properties of a model.

README 文档

README

Latest Version Software License Total Downloads

Laravel content translation

This package makes properties of your models translatable. For example when you have a Country model then you can make the country name translatable.

Todo

  • Add user interface to add translations.
  • Add tests
  • Method to disable the "static translations".

Installation

This package can be installed through Composer.

composer require jvdlaar/laravel-content-translation

You must install this service provider.

// config/app.php
'providers' => [
    ...
    JvdLaar\ContentTranslation\ContentTranslationServiceProvider::class,
    ...
];

This package also comes with a facade, which provides an easy way to call the the class.

// config/app.php
'aliases' => [
    ...
    'ContentTranslation' => JvdLaar\ContentTranslation\ContentTranslationFacade::class,
    ...
];

You can publish the config file of this package with this command:

php artisan vendor:publish --provider="JvdLaar\ContentTranslation\ContentTranslationServiceProvider"

The following config file will be published in config/content-translation.php

return [
  'fallback_language' => 'en',

  'country' => [
    'class' => \App\Models\Country::class,
    'label_property' => 'name',
    'properties' => [
      'name' => ['required' => TRUE],
      'nationality' => ['required' => TRUE],
    ],
  ],

  'page' => [
    'class' => \App\Models\Test::class,
    'label_property' => 'title',
    'properties' => [
      'title' => ['required' => TRUE],
      'body' => ['nl2br' => TRUE],
    ],
  ],
];

The array key is they key with which the translations are stored in the database, "class" refers to the model class. "label_property" is used to determine the translatable label of this model. E.g. the country name in the country model. "properties" is an array with the translatable properties and whether they are required and their output needs to be nl2br.

Usage

After installation and configuration you need to make models translatable by implementing the TranslatableContract. The HasTranslatables trait helps with this.

Example model

namespace App\Models;

use App\Base\Model;
use App\Contracts\TranslatableContract;
use App\Models\Traits\HasTranslatables;

class Country extends Model implements TranslatableContract {

  use HasTranslatables;

  protected $table = 'countries';
  protected $fillable = ['code', 'admin_name'];
  public $timestamps = FALSE;
  public $users = FALSE;



  /**
   * ATTRIBUTES
   */

  /**
   * Getter for 'name'.
   */
  public function getNameAttribute() {
    return $this->displayTranslation('name', TRUE);
  }

  /**
   * Getter for 'nationality'.
   */
  public function getNationalityAttribute() {
    return $this->displayTranslation('nationality', TRUE);
  }

  /**
   * Return an default for a property in this content.
   */
  protected function getTranslationDefault($property) {
    return $this->admin_name;
  }

}

In above example $country->name and $country->nationality are translated. When there is no translation in the database the admin_name property is used as fallback. The 2nd parameter of the function displayTranslation says that there is a default that should be used. The 3rd parameter is the locale of the translation, by default the current app locale will be used. 4th parameter determines that a fallback language should be used, this fallback language is set in the config.

Eager load translations for multiple models.

Country::eagerLoadTranslations([1, 2, 3], 'nl');
dump(Country::find(1)->name);

In above example 3 countries are eager loaded. So loading the model later and requesting a translation doesn't need an additional query.

Saving translations

You can add a translation to the database by using the facade:

\ContentTranslation::saveTranslation('country', $country->id, 'name', 'nl', 'Nederland');

Or by using a method on the model:

$country->saveTranslation('nl', ['name' => 'Nederland', 'nationality' => 'Nederlander']);

Security

If you discover any security related issues, please email johnny@ezcompany.nl instead of using the issue tracker.

About ezCompany

ezCompany is a webdevelopment agency in the Netherlands located in Tilburg, Breda and Utrecht. For more information see our website.

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固