fredckl/transable

Translater tables

Installs: 5

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:laravel-plugin

0.2.3 2019-08-12 08:43 UTC

This package is auto-updated.

Last update: 2024-05-12 18:42:09 UTC


README

very simple to translate all fields from your existing tables

Installation

via composer

composer require fredckl/transable

add ServiceProvider to your config file app.php

"providers" => [
    ...

    /*
     * Package Service Providers...
     */
    Fredckl\Transable\ServiceProvider::class,
    
    ...
]

execute migration

php artisan migrate

import configuration

php artisan vendor:publish --tag=transable

define your languages translatable in your transable.php

<?php
return [
    "default_locale" => "en", // Change if you your default locale is different
    "locales" => [
        "fr",
        "es",
        /// ...
    ]
];

How to use

add transable trait to your class

namespace App;

use Fredckl\Transable\src\Traits\Transable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Transable; // Import Trait

    protected $fillable = ['title', 'content'];
    
    /**
     * Define fields translatable
     */
    public function transable (): array
    {
        return [
            'title', 
            'content'
        ];
    }
}

work with translations

$post = App\Post::find(1);
echo $post->title; // return "Hello"
echo $post->content; // return "Content"
echo $post->fr->title; // return "Hello", default value EN because the value not exists

$post->fr->title = "Salut";
$post->fr->content = "contenu";
$post->es->title = "Hola";

$post->save(); true

echo $post->fr->title; // return "Salut"
echo $post->fr->content; // return "contenu"
echo $post->es->title; // return "Hola"  
echo $post->es->content; // return "Contents", default value EN because the value not exists

save records

$data = [
    'title' => 'Hello',
    'content' => 'Contents',
    'fr' => [
        'title' => 'Salut',
        'content' => 'contenu'
    ],
    'es' => [
        'title' => 'Hola',
        'content' => 'contenido'
    ]
];
App\Post::create($data);

$post = App\Post::where('title', 'Hello')->first();
echo $post->fr->title; // return "Salut";

automatic load translation

App::setLocale('fr');
App\Post::autoTranslate();
// OR without App::setLocale
App\Post::autoTranslate('fr');

$post = App\Post::where('title', 'Hello')->first();
echo $post->title; // return "Salut"

finder

App\Post::translated(); // retrieve all posts translated
App\Post::doesntHaveTranslations(); // retrieve all posts without translations

App\Post::whereTranslation($field, $value); // return matched posts

deleting

$post->delete(); // delete post and all translations
App\Post::deleteTranslationsWhenEmptyModel(); delete all translations without model
// OR 
Fredckl\Transable\Models\I18n::deleteEmpty(); 

This project is being tested. It is not advisable to use it in production. All suggestions are welcome as well as contributions.