Weave 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

Locale Laravel Laravel Package

uselocale/locale-laravel

A Laravel localization package for multilingual apps, with locale detection, language switching, localized routes, and a simple configurable API that integrates cleanly with Laravel middleware and views.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Require the package with Composer and confirm your app meets the release constraints:

    composer require uselocale/locale-laravel:^2.0
    

    2.0.1 adds Laravel 10.x compatibility, so verify laravel/framework and PHP versions before upgrading. If you are on Laravel 9 or 11, check the package’s composer.json constraints rather than assuming compatibility.

  2. Publish any package config and language assets:

    php artisan vendor:publish --provider="UseLocale\Locale\LocaleServiceProvider"
    

    Check the README for the exact provider/tag names. Start by reviewing config/locale.php and the package README’s middleware/route examples.

  3. Register the locale middleware if the package does not auto-register it. Typical placement is in the web middleware group, after session startup and before routes that depend on the current locale.

  4. First use case: add a localized route group, resolve the current locale from the URL/session, and expose a simple language switcher. Keep Laravel’s normal __() / trans() calls as the translation API; the package should manage locale resolution and switching, not replace Laravel’s translator.

Implementation Patterns

  • URL-based locale routing: Put localized routes behind the package middleware and use a route prefix or route parameter for the locale. Keep one canonical URL strategy to avoid duplicate content.
  • Session/cookie language switching: Create a small controller that validates the requested locale against an allowlist, stores it in session or cookie, then redirects back to the same page. Never redirect to an unvalidated external URL.
  • Middleware-driven locale resolution: Let the package middleware set app()->setLocale() early in the request lifecycle. Keep custom resolution logic in a dedicated resolver or middleware rather than scattering App::setLocale() calls across controllers.
  • Blade integration: Use the package’s helpers/directives for current locale and switcher links if provided. Otherwise, wrap Laravel’s app()->getLocale() in a small Blade component so views do not depend directly on package internals.
  • Translation workflow: Keep translation files in version control and run any package sync/import commands in CI or a deploy step. Treat generated files as build artifacts if the package supports syncing from an external translation source.
  • Testing: Add feature tests for each supported locale, asserting redirects, session/cookie persistence, route generation, and rendered translations. Clear config and route caches in test setup if the package reads cached locale configuration.
  • SEO integration: Generate hreflang links and canonicals from the same locale allowlist used by the middleware. This keeps routing, switcher, and SEO metadata consistent.
  • Upgrade path: Treat 2.0.1 as a compatibility release, not a feature release. If coming from 1.x, review the 2.0.0 changelog/upgrade notes first; 2.0.1 only adds Laravel 10 support.

Gotchas and Tips

  • Version constraints matter: 2.0.1 explicitly targets Laravel 10 compatibility. Confirm PHP and Laravel constraints before upgrading, especially if you are on Laravel 11+ or still on Laravel 9.
  • Middleware order: Locale detection must run after session initialization but before anything that depends on translated content, route model binding with localized slugs, or locale-aware URL generation.
  • Cache clearing: After changing locale config, route prefixes, or middleware registration, run php artisan config:clear and php artisan route:clear. Cached routes/config can make locale changes appear ignored.
  • Validate locales: Always compare the requested locale against a configured allowlist. Do not pass raw route parameters into setLocale() or file paths.
  • Choose one persistence strategy: Mixing URL locale, session locale, and cookie locale can produce surprising redirects. Define precedence clearly, e.g. URL parameter → session → browser preference → default.
  • Debugging resolution: Log the detected locale source (route, session, cookie, Accept-Language, default) in non-production environments. Most locale bugs are precedence bugs, not translation bugs.
  • Avoid vendor edits: Use published config, custom middleware, or a small resolver class as extension points. Keep package overrides in app code so Composer updates remain safe.
  • Watch for deprecations: Laravel 10 compatibility may surface deprecated helpers or config patterns. Run the full test suite and check deprecation logs after upgrading.
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.
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels