Code Monkey home page Code Monkey logo

laravel-translatable's Introduction

Laravel Translatable

Make Eloquent model attributes translatables using Translations table

Scrutinizer Code Quality Code Coverage Build Status StyleCI License: MIT Laravel 6.x

This package contains a trait to make Eloquent attributes translatable. Translations are stored in Translations database table.

Once the trait is installed on the model you can do these things:

$customer = new Customer; // An Eloquent model
$customer
   ->setTranslation('greeting', 'en', 'Hello')
   ->setTranslation('greeting', 'es', 'Hola')
   ->save();
   
$customer->greeting; // Returns 'Hello' given that the current app locale is 'en'
$customer->getTranslation('greeting', 'es'); // returns 'Hola'

app()->setLocale('es');

$customer->greeting; // Returns 'Hola'

Installation

You can install the package via composer:

composer require latevaweb/laravel-translatable

If you want to change the default model or the default tables names, you could publish the config file:

php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=config --force

You must publish the migration file to create polymorphic and main translations tables:

php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=migrations --force

Making a model translatable

The required steps to make a model translatable are:

  • First, you need to add the LaTevaWeb\Translatable\Traits\Translatable-trait.
  • Next, you should create a public static property $translatable which holds an array with all the names of attributes you wish to make translatable.
  • You have to create a field in the migration of your model type string and nullable.

Here's an example of a prepared model:

use Illuminate\Database\Eloquent\Model;
use LaTevaWeb\Translatable\Traits\Translatable;

class NewsItem extends Model
{
    use Translatable;
       
    protected $fillable = ['greeting'];
    public static $translatable = ['greeting'];
}

laravel-translatable's People

Contributors

latevaweb avatar rafaltw avatar rafatga avatar

Watchers

 avatar

Forkers

rafatga futskito

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.