codanux/multi-language

Multi Language Laravel

v1.0.2 2021-04-10 08:03 UTC

This package is auto-updated.

Last update: 2024-11-05 20:49:30 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

Installation

You can install the package via composer:

composer require codanux/multi-language

Configuration

php artisan vendor:publish --provider="Codanux\MultiLanguage\MultiLanguageServiceProvider"

To detect and change the locale of the application based on the request automatically:

config/multi-language.php

'middleware' => [
    'web' => [
        \Codanux\MultiLanguage\DetectRequestLocale::class
    ]
],

Model

class Post extends Model // implements HasMedia
{
    use HasLanguage;
    // use InteractsWithMedia, MediaTrait { MediaTrait::getMedia insteadof InteractsWithMedia; }
}

Migration

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->locale(); // added
});

Route

Route::localeResource('post', 'PostController')->names('post');
or
Route::locale('post.index', 'PostController@index');
Route::locale('post.show', 'PostController@show');
Route::locale('post.create', 'PostController@create');
Route::locale('post.create', 'PostController@store')->method('POST');
Route::locale('post.edit', 'PostController@edit');
Route::locale('post.edit', 'PostController@update')->method('PUT');
Route::locale('post.destroy', 'PostController@destroy')->method('DELETE');


Route::locale('dashboard', function () {
    return view('dashboard');
})
->name('home');

Route::group(['localePrefix' => 'admin.prefix'], function (){

    Route::locale('admin.dashboard', function () {
        return view('admin.dashboard');
    });

});

Route Usage

routeLocalized('post.show', $post)

| Method    | URI           | Name          | Action                              |
|-----------|---------------|---------------|-------------------------------------|
| GET\|HEAD | posts/{post}  | en.post.show  | App\Http\Controllers\PostController@show |
| GET\|HEAD | tr/postlar/{post} | tr.post.show | App\Http\Controllers\PostController@show |

Controller

public function index()
{
    // locale scope
    $posts = (new Post())->newQuery()->locale()->get();
}

public function store(Request $request)
{
    $post = Post::create([
       'name' => 'Post en',
       'locale' => 'en',
   ]);

    Post::create([
       'name' => 'Post tr',
       'locale' => 'tr',
       'locale_slug' => 'post-1',
       'translation_of' => $post->translation_of
    ]);
    
    Post::localeSlug('post-1', 'tr')->first(); // Post tr
    
    Post::localeSlug('post-1', 'en')->first(); // Post en
}

public function show(Post $post)
{
    return view('post.show', compact('post'));
}

Views

post.index
    <x-links component="jet-nav-link"></x-links>

post.show
    <x-links :translations=['post' => $post]></x-links>
    
    //category/{category}/posts/{post}

    // route model translations ['category' => $category, 'post => $post]

Jetstream Router

1. config/multi-language.php

'fortify' => [
    'routes' => true,
],

'jetstream' => [
    'routes' => true,
]

2. Default Router İgnore

class FortifyServiceProvider extends ServiceProvider
{
    public function register()
    {
        Fortify::ignoreRoutes();
    }
}

class JetstreamServiceProvider extends ServiceProvider
{
   public function register()
   {
       Jetstream::ignoreRoutes();
   }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email codanux@hotmail.com instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.