Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel I18N Laravel Package

richan-fongdasen/laravel-i18n

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require richan-fongdasen/laravel-i18n
    

    Publish the config file:

    php artisan vendor:publish --provider="RichanFongdasen\LaravelI18n\LaravelI18nServiceProvider" --tag="config"
    
  2. Configure Supported Locales Update config/laravel-i18n.php:

    'locales' => [
        'en' => 'English',
        'fr' => 'Français',
        'es' => 'Español',
    ],
    
  3. First Use Case: Localized Routes Define a localized route in routes/web.php:

    Route::i18n([
        'en' => 'home',
        'fr' => 'accueil',
        'es' => 'inicio',
    ], function () {
        return view('welcome');
    });
    

    Access via /en/home, /fr/accueil, etc.


Implementation Patterns

Route Localization

  • Dynamic Route Grouping Use Route::i18nGroup() for shared prefixes:

    Route::i18nGroup(['prefix' => 'admin'], function () {
        Route::i18n(['en' => 'dashboard', 'fr' => 'tableau-de-bord'], function () {
            // ...
        });
    });
    
  • Locale Fallback Set a default locale in config/laravel-i18n.php:

    'default_locale' => 'en',
    'fallback_locale' => 'en',
    

Eloquent Localization

  • Localized Model Attributes Use the Localizable trait:

    use RichanFongdasen\LaravelI18n\Eloquent\Localizable;
    
    class Product extends Model
    {
        use Localizable;
    
        protected $localizable = ['name', 'description'];
    }
    

    Access translations:

    $product->name('fr'); // Get French name
    $product->setName('fr', 'Nouveau nom'); // Set French name
    
  • Database Schema The package auto-creates localizable_<table_name> tables. Migrate with:

    php artisan i18n:migrate
    

Middleware & Request Handling

  • Locale Detection Use middleware to auto-detect locale from:

    • URL (/fr/...)
    • Accept-Language header
    • Session Configure in app/Http/Kernel.php:
    protected $middlewareGroups = [
        'web' => [
            // ...
            \RichanFongdasen\LaravelI18n\Http\Middleware\DetectLocale::class,
        ],
    ];
    
  • Locale-Specific Responses Dynamically load views/translations:

    $locale = app()->getLocale();
    return view("pages.{$locale}.home");
    

Gotchas and Tips

Pitfalls

  • Route Caching Conflicts Clear route cache after adding localized routes:

    php artisan route:clear
    

    Or disable caching in config/laravel-i18n.php:

    'cache_routes' => false,
    
  • Locale-Specific Validation Ensure validation rules account for localized fields:

    $validator = Validator::make($request->all(), [
        "name.{$locale}" => 'required|string',
    ]);
    
  • Database Overhead Localized fields create additional database tables. Monitor query performance with:

    php artisan tinker
    >>> \DB::enableQueryLog();
    >>> $product->name('fr');
    >>> \DB::getQueryLog();
    

Debugging

  • Locale Not Switching? Check middleware order in app/Http/Kernel.php. DetectLocale must run before ShareErrorsFromSession or StartSession.

  • Missing Translations Verify the localizable_<table> tables exist and are populated. Use:

    php artisan i18n:refresh
    

Extension Points

  • Custom Locale Providers Extend locale detection by implementing RichanFongdasen\LaravelI18n\Contracts\LocaleProvider:

    class CookieLocaleProvider implements LocaleProvider
    {
        public function getLocale(): string
        {
            return request()->cookie('locale', config('app.locale'));
        }
    }
    

    Register in config/laravel-i18n.php:

    'providers' => [
        \RichanFongdasen\LaravelI18n\Providers\UrlLocaleProvider::class,
        App\Providers\CookieLocaleProvider::class,
    ],
    
  • Locale-Specific Controllers Use route model binding with localized models:

    Route::i18n(['en' => 'product/{product}', 'fr' => 'produit/{product}'], function (Product $product) {
        // $product is automatically localized
    });
    
  • Fallback Logic Override fallback behavior in a service provider:

    public function boot()
    {
        app()->setFallbackLocale(function () {
            return request()->ip() === '127.0.0.1' ? 'en' : config('app.fallback_locale');
        });
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager