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 Localizer Laravel Package

niels-numbers/laravel-localizer

Locale-aware routing for Laravel with static, route:cache-ready localized routes. Auto-detects language, redirects to prefixed URLs, and resolves route() to the correct locale. Successor to mcamara/laravel-localization.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Locale-Aware Routing: The package excels at Laravel’s core need—seamless multi-lingual routing—with minimal boilerplate. It replaces manual route registration per locale (e.g., /en/about, /fr/about) with a single Route::localize() wrapper, auto-generating static routes while preserving Laravel’s route() helper consistency.
  • Middleware Integration: Leverages Laravel’s middleware pipeline (RedirectLocale, SetLocale) to handle redirects, session/cookie persistence, and locale detection (e.g., Accept-Language header). This aligns with Laravel’s declarative middleware pattern, reducing custom logic.
  • Ziggy/Inertia/Wayfinder Compatibility: Explicit support for frontend frameworks (via LocalizerBladeRouteGeneratorV2, TypeScript helpers) ensures client-side route consistency without breaking SSR or hydration. This is critical for modern SPAs.
  • Locale Direction Handling: Adds RTL/LTR support via Localizer::currentLocaleDirection(), addressing UX needs for languages like Arabic or Hebrew without requiring custom middleware.

Integration Feasibility

  • Low Friction: Replaces mcamara/laravel-localization (deprecated) with a drop-in API (Route::localize()). Migration path is well-documented, reducing risk.
  • Static Route Cache Compatibility: Routes are registered as static variants (with_locale.about, without_locale.about), ensuring compatibility with route:cache and Laravel’s router optimizations.
  • Detector Chain: Supports custom locale detection (e.g., user model attributes, database fallbacks) via DetectorInterface, enabling context-aware localization (e.g., user preferences over browser headers).

Technical Risk

  • Middleware Order Sensitivity: SetLocale must run after StartSession and before SubstituteBindings to avoid breaking translated route bindings (e.g., {post:slug}). Misconfiguration risks 404s or incorrect locale resolution.
    • Mitigation: Package enforces this via docs and CI tests; TPM must validate middleware stack during integration.
  • Ziggy/Wayfinder Versioning: Frontend route helpers require specific bindings (e.g., LocalizerBladeRouteGeneratorV2 for Ziggy v2). Mismatches may break client-side routing.
    • Mitigation: TPM should audit frontend stack versions pre-integration and test with @routes Blade directive.
  • Locale Direction Logic: Relies on ext-intl for BCP 47 script detection. Fallback to built-in maps may miss edge cases (e.g., rare scripts).
    • Mitigation: Extend localizer.rtl_scripts config if needed; test with target locales.

Key Questions

  1. Locale Strategy:
    • Should locale detection prioritize user session, browser headers, or URL prefix? (Configurable via localizer.detectors.)
    • How should fallback locales (e.g., en for unsupported languages) be handled?
  2. URL Canonicalization:
    • Should default locale URLs (e.g., /about) hide the prefix (hide_default_locale: true) or always show it (e.g., /en/about)?
  3. Frontend Integration:
    • Is the app using Ziggy, Wayfinder, or Inertia? If custom, will spatie/laravel-typescript-transformer helpers suffice?
  4. Performance:
    • Will route:cache be used? If yes, verify static route generation works with Route::localize().
  5. Analytics/Tracking:
    • How will locale-aware routes impact Google Analytics or heatmaps? (Use Route::baseName() to normalize route names.)

Integration Approach

Stack Fit

  • Laravel Core: Perfect fit for Laravel 9–13 apps using routes, middleware, and Blade/Ziggy.
  • Frontend Frameworks:
    • Ziggy/Inertia: Requires LocalizerBladeRouteGeneratorV2 binding (Laravel 11+).
    • Wayfinder: Uses TypeScript helpers; no package changes needed.
    • Livewire: Works out-of-the-box with route() helper.
  • Monolithic vs. Micro-Frontends:
    • Monolithic: Ideal for server-rendered or SPA hybrid apps.
    • Micro-Frontends: May require custom detector logic (e.g., API-driven locale resolution).

Migration Path

  1. Replace mcamara/laravel-localization:
    • Update composer.json and run composer update.
    • Replace Route::group(['prefix' => '{locale}'], ...) with Route::localize(...).
    • Update middleware to use RedirectLocale and SetLocale (order-critical).
  2. Frontend Adaptation:
    • For Ziggy: Bind LocalizerBladeRouteGeneratorV2 in AppServiceProvider.
    • For TypeScript: Add the provided wrapper to resources/js/route.ts.
  3. Testing:
    • Validate redirects (/about/de/about).
    • Test locale persistence across page reloads.
    • Verify route() helper returns correct URLs in Blade/JS.

Compatibility

  • Laravel Versions: Supports 9–13 (PHP 8.2–8.4). No breaking changes for major versions.
  • Route Caching: Static routes are cache-compatible; test with php artisan route:cache.
  • Custom Detectors: Extend DetectorInterface for non-standard sources (e.g., database, API).
  • Non-English Scripts: RTL/LTR support covers 90% of cases; extend localizer.rtl_scripts if needed.

Sequencing

  1. Phase 1: Core Routing
    • Replace Route::group with Route::localize().
    • Configure localizer.php (detectors, default locale, hidden prefix).
  2. Phase 2: Middleware
    • Add RedirectLocale and SetLocale to web middleware group (order: StartSessionSetLocaleSubstituteBindings).
  3. Phase 3: Frontend
    • Bind Ziggy/Blade generators or add TypeScript helpers.
  4. Phase 4: Validation
    • Test locale redirects, session persistence, and client-side routing.

Operational Impact

Maintenance

  • Configuration-Driven: Most logic is controlled via config/localizer.php (detectors, locales, RTL rules), reducing code changes.
  • Dependency Updates: Monitor for Ziggy/Laravel version compatibility (e.g., Ziggy v2+ requires LocalizerBladeRouteGeneratorV2).
  • Detector Extensions: Custom detectors may need updates if user model or session structures change.

Support

  • Debugging Redirects:
    • Use php artisan route:list to verify static routes (with_locale.*, without_locale.*).
    • Check middleware order with php artisan route:middleware.
  • Locale Detection Issues:
    • Log Accept-Language headers and detector chain output for edge cases.
  • Frontend Routing:
    • Test @routes Blade directive and TypeScript helpers in all locales.

Scaling

  • Performance:
    • Static routes reduce runtime overhead; route:cache further optimizes.
    • Locale detection is lightweight (header/session lookup).
  • High Traffic:
    • Middleware (RedirectLocale) adds minimal latency (~1–2ms for redirects).
    • Caching Accept-Language in session reduces repeated header parsing.
  • Multi-Region:
    • Deploy locale-specific assets (e.g., lang/de.json) via CDN or edge caching.

Failure Modes

Scenario Impact Mitigation
Middleware order wrong 404s on translated routes Validate with php artisan route:middleware.
Accept-Language missing Default locale used Extend detector chain (e.g., user session).
Ziggy binding misconfigured Client-side routes broken Test @routes in all locales.
RTL/LTR misclassification Incorrect dir attribute Extend localizer.rtl_scripts config.
Route cache stale Redirects to wrong locale Clear cache after locale additions.

Ramp-Up

  • Developer Onboarding:
    • Document Route::localize() usage and middleware order.
    • Provide examples for custom detectors and frontend helpers.
  • Testing Checklist:
    1. Redirects (/about/de/about).
    2. Session/cookie persistence.
    3. Frontend route helpers (Blade/JS).
    4. RTL/LTR HTML attributes.
    5. Edge cases (wrong-case URLs, unsupported locales).
  • Rollback Plan:
    • Fallback to manual route groups if issues arise.
    • Use hide_default_locale: false to expose
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor