All Projects → chinleung → Laravel Multilingual Routes

chinleung / Laravel Multilingual Routes

Licence: mit
A package to handle multilingual routes in your Laravel application.

Projects that are alternatives of or similar to Laravel Multilingual Routes

Laravel Aws Sns
Laravel package for the AWS SNS Events
Stars: ✭ 24 (-90.04%)
Mutual labels:  laravel, laravel-package, package
Package Skeleton
📦 My base for PHP packages.
Stars: ✭ 6 (-97.51%)
Mutual labels:  laravel, laravel-package, package
Laravel Gitscrum
GitScrum is a Project Management Tool, developed to help entrepreneurs, freelancers, managers, and teams Skyrocket their Productivity with the Agile methodology and Gamification.
Stars: ✭ 2,686 (+1014.52%)
Mutual labels:  laravel, laravel-package, package
Laravel Short Url
A Laravel package to shorten urls
Stars: ✭ 127 (-47.3%)
Mutual labels:  laravel, laravel-package, package
Laravel Google Translate
This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Stars: ✭ 97 (-59.75%)
Mutual labels:  laravel, laravel-package, package
Laravel Multilang
Package to integrate multi language (multi locale) functionality in Laravel 5.x.
Stars: ✭ 47 (-80.5%)
Mutual labels:  laravel, laravel-package, localization
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (+87.14%)
Mutual labels:  laravel, laravel-package, localization
Laravel Pdf
A Simple package for easily generating PDF documents from HTML. This package is specially for laravel but you can use this without laravel.
Stars: ✭ 79 (-67.22%)
Mutual labels:  laravel, laravel-package, package
Laravel Natural Language
This package makes using the Google Natural API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Stars: ✭ 119 (-50.62%)
Mutual labels:  laravel, laravel-package, package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (-17.84%)
Mutual labels:  laravel, laravel-package, package
Laravelpackage.com
Documentation for LaravelPackage.com: Learn to create Laravel specific PHP packages from scratch, following this open documentation.
Stars: ✭ 214 (-11.2%)
Mutual labels:  laravel, package
Laravel Multisite
Multiple sites on one codebase
Stars: ✭ 214 (-11.2%)
Mutual labels:  laravel, package
Laravel State Machine
Winzou State Machine service provider for Laravel
Stars: ✭ 213 (-11.62%)
Mutual labels:  laravel, laravel-package
Laravel Custom Casts
Make your own custom cast type for Laravel model attributes
Stars: ✭ 213 (-11.62%)
Mutual labels:  laravel, package
Laravel Terminator
A package to help you clean up your controllers in laravel
Stars: ✭ 217 (-9.96%)
Mutual labels:  laravel, laravel-package
Laravel Comment
Just another comment system for your awesome Laravel project.
Stars: ✭ 217 (-9.96%)
Mutual labels:  laravel, laravel-package
Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (-3.73%)
Mutual labels:  laravel, localization
Sneaker
An easy way to send emails whenever an exception occurs on server.
Stars: ✭ 223 (-7.47%)
Mutual labels:  laravel, laravel-package
Web Socket
Laravel library for asynchronously serving WebSockets.
Stars: ✭ 225 (-6.64%)
Mutual labels:  laravel, laravel-package
Meter
Laravel package to find performance bottlenecks in your laravel application.
Stars: ✭ 204 (-15.35%)
Mutual labels:  laravel, laravel-package

Laravel Multilingual Routes

Latest Version on Packagist Build Status Quality Score Total Downloads

A package to register multilingual routes for your application.

Installation

You can install the package via composer:

composer require chinleung/laravel-multilingual-routes

To detect and change the locale of the application based on the request automatically, you can add the middleware to your app/Http/Kernel:

protected $middlewareGroups = [
    'web' => [
        \ChinLeung\MultilingualRoutes\DetectRequestLocale::class,
        // ...
    ]
];

Configuration

By default, the application locales are only going to be en and the default locale is not prefixed. If you want to prefix the default locale, please run the following command to publish the configuration file:

php artisan vendor:publish --provider="ChinLeung\MultilingualRoutes\MultilingualRoutesServiceProvider" --tag="config"

If your application supports different locales, you can either set a app.locales configuration or follow the configuration instructions from chinleung/laravel-locales.

Example

Instead of doing this:

Route::get('/', 'ShowHomeController')->name('en.home');
Route::get('/fr', 'ShowHomeController')->name('fr.home');

You can accomplish the same result with:

Route::multilingual('/', 'ShowHomeController')->name('home');

A demo repository has been setup to showcase the basic usage of the package.

Usage

Quick Usage

Once you have configured the locales, you can start adding routes like the following example in your routes/web.php:

Route::multilingual('test', 'TestController');

This will generate the following:

Method URI Name Action
GET|HEAD test en.test App\Http\Controllers\TestController
GET|HEAD fr/teste fr.test App\Http\Controllers\TestController

Note the URI column is generated from a translation file located at resources/lang/{locale}/routes.php which contains the key of the route like the following:

<?php

// resources/lang/fr/routes.php

return [
  'test' => 'teste'
];

To retrieve a route, you can use the localized_route(string $name, array $parameters, string $locale = null, bool $absolute = true) instead of the route helper:

localized_route('test'); // Returns the url of the current application locale
localized_route('test', [], 'fr'); // Returns https://app.test/fr/teste
localized_route('test', [], 'en'); // Returns https://app.test/test

To retrieve the current route in another locale, you can use the current_route(string $locale = null) helper:

current_route(); // Returns the current request's route
current_route('fr'); // Returns the current request's route in French version
current_route('fr', route('fallback')); // Returns the fallback route if the current route is not registered in French

Renaming the routes

Route::multilingual('test', 'TestController')->name('foo');
Method URI Name Action
GET|HEAD test en.foo App\Http\Controllers\TestController
GET|HEAD fr/teste fr.foo App\Http\Controllers\TestController

Renaming a route based on the locale

Route::multilingual('test', 'TestController')->names([
  'en' => 'foo',
  'fr' => 'bar',
]);
Method URI Name Action
GET|HEAD test en.foo App\Http\Controllers\TestController
GET|HEAD fr/teste fr.bar App\Http\Controllers\TestController

Skipping a locale

Route::multilingual('test', 'TestController')->except(['fr']);
Method URI Name Action
GET|HEAD test en.test App\Http\Controllers\TestController

Restricting to a list of locales

Route::multilingual('test', 'TestController')->only(['fr']);
Method URI Name Action
GET|HEAD fr/teste fr.test App\Http\Controllers\TestController

Changing the method of the request

Route::multilingual('test', 'TestController')->method('post');
Method URI Name Action
POST test en.test App\Http\Controllers\TestController
POST fr/teste fr.test App\Http\Controllers\TestController

Registering a view route

// Loads test.blade.php
Route::multilingual('test');
Method URI Name Action
GET|HEAD test en.test Illuminate\Routing\ViewController
GET|HEAD fr/teste fr.test Illuminate\Routing\ViewController

Registering a view route with a different key for the route and view

// Loads welcome.blade.php instead of test.blade.php
Route::multilingual('test')->view('welcome');
Method URI Name Action
GET|HEAD test en.test Illuminate\Routing\ViewController
GET|HEAD fr/teste fr.test Illuminate\Routing\ViewController

Passing data to the view

Route::multilingual('test')->data(['name' => 'Taylor']);
Route::multilingual('test')->view('welcome', ['name' => 'Taylor']);

Upgrading from 1.x to 2.x

To update from 1.x to 2.x, you simply have to rename the namespace occurrences in your application from LaravelMultilingualRoutes to MultilingualRoutes. The most common use case would be the DetectRequestLocale middleware.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].