- How does this package compare to Laravel’s built-in trans() helper for basic translations?
- This package doesn’t replace Laravel’s trans() for key-value translations but adds Intl-powered features like locale-aware number/currency formatting, pluralization (e.g., '1 item' vs. '2 items'), and gendered forms. Use trans() for simple strings and this package for complex formatting rules. For hybrid setups, you’ll need a custom facade to bridge both systems.
- Will this work with Laravel’s existing resources/lang/ JSON files without migration?
- Yes, but you’ll need to wrap calls with the package’s translator (e.g., `translator()->get('key')`). For full integration, migrate incrementally by updating translation files to include Intl-specific placeholders (e.g., `{number, plural, one {# item} other {# items}}`). Laravel’s default `trans()` calls won’t automatically use this package’s Intl features.
- What’s the performance impact of using Intl extension for pluralization vs. hardcoded rules?
- The Intl extension is CPU-intensive, especially for high-traffic APIs. Benchmark your use case: hardcoded rules (e.g., `count === 1 ? 'item' : 'items'`) are faster but less maintainable for complex locales (e.g., Arabic pluralization). For CLI or low-traffic apps, the difference is negligible. Cache compiled translations if possible.
- How do I handle missing ext-intl on shared hosting or CI environments?
- Add `ext-intl: *` to your `composer.json` constraints and document the requirement. For graceful degradation, wrap Intl calls in a try-catch block and fall back to basic formatting (e.g., `number_format($value)`). Test with `php -m | grep intl` in your CI pipeline to fail early if the extension is missing.
- Can this package work alongside spatie/laravel-translation-loader or laravel-localization?
- Potential conflicts may arise due to duplicate translation loading or locale detection middleware. Disable spatie’s loader or configure it to delegate Intl-specific formatting to this package. For laravel-localization, ensure its middleware runs *after* your Intl translator’s service provider to avoid locale override issues.
- Does this support RTL (right-to-left) languages like Arabic or Hebrew?
- Yes, the Intl extension natively handles RTL languages, including bidirectional text, date formatting, and locale-specific collation. Test with `IntlDateFormatter` and `NumberFormatter` for RTL locales (e.g., `ar_EG`, `he_IL`). However, ensure your frontend (e.g., CSS `direction: rtl`) and backend (e.g., database text storage) are also RTL-compatible.
- How do I integrate this with Laravel’s SetLocaleMiddleware for dynamic locale switching?
- Extend Laravel’s `SetLocaleMiddleware` to initialize the Intl translator after locale detection. Example: Add a middleware priority higher than `SetLocaleMiddleware` to bind the translator with the resolved locale. Use `app('translator')->setLocale($request->locale)` to sync both systems.
- Are there any breaking changes if I switch from Laravel’s default trans() to this package?
- Yes, the API differs (e.g., `translator()->get('key')` vs. `trans('key')`). Plan a phased migration: start with non-critical paths, create a custom facade to alias `trans()` to the new translator, and use deprecation warnings in `AppServiceProvider` to guide developers. Avoid mixing both systems in the same view/controller.
- Can I use this for CLI applications or is it frontend-only?
- This package works for both CLI and frontend. Use it in Artisan commands or Laravel Horizon jobs for locale-aware output (e.g., `translator()->formatNumber($value, 'en_US')`). For CLI, ensure the PHP process has access to the Intl extension (check `php -m` in your CLI environment).
- What Laravel versions and PHP versions are officially supported?
- The package targets Laravel 8+ and PHP 7.4+ (due to Intl extension features). Verify compatibility by checking the `composer.json` constraints and testing with your Laravel version. For Laravel 9/10, ensure no breaking changes exist in the Lang facade or service container binding methods.