Code Monkey home page Code Monkey logo

laravel-translatable's Introduction

Laravel Translatable

Packagist Build Status Code Coverage Scrutinizer Code Quality license Packagist

A fresh new way to handle Model translations, the translations are joined into the Model instead of making you query a relation or get every single attribute's translation one by one.

Installation

Require the package.

composer require koenhoeijmakers/laravel-translatable

... and optionally publish the config.

php artisan vendor:publish --provider="KoenHoeijmakers\LaravelTranslatable\TranslatableServiceProvider"

Usage

Setting up a translatable Model.

Start off by creating a migration and a Model, we'll go with the Animal Model and the corresponding AnimalTranslation Model.

Migrations

Schema::create('animals', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
});

Always have a locale and a foreign_key to the original Model, in our case animal_id.

Schema::create('animal_translations', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('animal_id');
    $table->string('locale');
    $table->string('name');
    $table->timestamps();
    
    $table->unique(['locale', 'animal_id']);
    $table->foreign('animal_id')->references('id')->on('animals');
});

Models

Register the trait on the Model, and add the columns that should be translated to the $translatable property, But also make them fillable, this is because the saving is handled through events, this way we don't have to change the save method and makes the package more interoperable.

So make sure the $translatable columns are also $fillable on both Models.

use Illuminate\Database\Eloquent\Model;
use KoenHoeijmakers\LaravelTranslatable\HasTranslations;

class Animal extends Model
{
    use HasTranslations;
    
    protected $translatable = ['name'];
    
    protected $fillable = ['name'];
}
use Illuminate\Database\Eloquent\Model;

class AnimalTranslation extends Model
{
    protected $fillable = ['name'];
}

This is pretty much all there is to it, but you can read more about the package down here.

About

What makes this package so special is the way it handles the translations, how it retrieves them, how it stores them, and how it queries them.

Querying

Due to how the package handles the translations, querying is a piece of cake, while for other packages you would have a ->whereTranslation('nl', 'column', '=', 'foo') method.

But in this package you can just do ->where('column', '=', 'foo') and it'll know what to query, just query how you used to!

Retrieving

When you retrieve a Model from the database, the package will join the translation table with the translation of the current locale config/app.php.

This makes it so that any translated column acts like it is "native" to the Model, due to this we don't have to override a lot of methods on the Model which is a big plus.

Need the Model in a different language? Call $model->translate('nl') and you are done. Now you would like to save the nl translation? just call ->update(). The Model knows in which locale it is loaded and it will handle it accordingly.

$animal = Animal::query()->find(1);

$animal->translate('nl')->update(['name' => 'Aap']);

Storing

You will be storing your translations as if they're attributes on the Model, so this will work like a charm:

Animal::query()->create(['name' => 'Monkey']);

But i hear you, you would like to store multiple translations in one request! In that so you can use the ->storeTranslation() or the ->storeTranslations() method.

$animal = Animal::query()->create(['name' => 'Monkey']);

$animal->storeTranslation('nl', [
    'name' => 'Aap',
]);

$animal->storeTranslations([
    'nl' => [
        'name' => 'Aap',
    ],
    'de' => [
        'name' => 'Affe',
    ],
]);

laravel-translatable's People

Contributors

koenhoeijmakers avatar ricardovanlaarhoven avatar svenluijten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-translatable's Issues

Typo in readme

$animal->storeTranslation([
    'nl' => [
        'name' => 'Aap',
    ],
    'de' => [
        'name' => 'Affe',
    ],
]);

Sould change to

$animal->storeTranslations([
    'nl' => [
        'name' => 'Aap',
    ],
    'de' => [
        'name' => 'Affe',
    ],
]);

that is storeTranslations with an s at the end

Unit Tests

We can't guarantee that changes won't break current applications unless we add unit tests.

Compatibility issues with updateOrCreate

$language = Language::updateOrCreate([
    'id' => $values[1],`
    'name' => $values[2],`
    'parent_id' => $values[3],
]);

cause the following error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select 'languages'.*, 'language_translations'.'name' from 'languages' left join 'language_translations' on 'languages'.'id' = 'language_translations'.'language_id' and 'locale' = nl where ('id' = 1 and 'parent_id' = 0) limit 1)

If there is no easy fix for this, is there an other way to use updateOrCreate?

Update: even simple things like Language::whereId(1)->get(); cause the same issue

Laravel 7.4 compatibility

Declaration of KoenHoeijmakers\LaravelTranslatable\HasTranslations::resolveRouteBinding($value) must be compatible with Illuminate\Database\Eloquent\Model::resolveRouteBinding($value, $field = NULL)

I'm not sure if this can be updated without breaking backwards compatibility

Select is overridden because applyScopes is called last.

At the moment you can't query like this, because applyScopes() is called only just before get() and thus overrides the select.

Model::query()->addSelect('something')->get();

I suppose making select into addSelect in the JoinTranslationScope fixes the issue.

Fallback locale

At the moment there is no fallback locale for when a translation is missing, the default is to return null.

But it might be nice to optionally be able to load the default locale.

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.