- How do I install laravel-lang/json-fallback in my Laravel project?
- Run `composer require laravel-lang/json-fallback` in your project directory. The package integrates automatically via Laravel’s service provider system, replacing the default `JsonLoader` without manual configuration for standard setups.
- Does this package work with Laravel 10, 11, or 12?
- Yes, it’s fully compatible with Laravel 10 through 13. The package is actively maintained and updated for each major release, ensuring seamless integration with the latest Laravel versions.
- Can I use fallback translations for nested JSON structures like `{'key': {'subkey': 'value'}}`?
- No, this package assumes flat JSON structures (e.g., `{'key': 'value'}`). Nested JSON requires pre-processing (e.g., flattening keys to `key.subkey`) or a custom solution to avoid runtime errors.
- What happens if my JSON translation file is malformed or missing?
- The package gracefully falls back to the next locale in the hierarchy or returns the original key if no fallback is configured. However, malformed JSON will break the entire translation load, so validate files in CI/CD using tools like JSON Schema.
- Will this conflict with third-party translation providers like spatie/laravel-translation-loader?
- Potential conflicts may arise if using custom providers. The package replaces Laravel’s default `JsonLoader`, so test thoroughly with your setup. If conflicts occur, manually bind the fallback loader in your service provider.
- How do I configure multiple fallback locales (e.g., fr-CA → fr-FR → en)?
- The package defaults to linear fallbacks (e.g., `fr` → `en`). For multi-level hierarchies, extend the `JsonFallbackLoader` class or configure custom logic in the service provider to define complex fallback chains.
- Does this package support dynamic locales (e.g., user-selected languages)?
- Yes, it works with dynamic locales via Laravel’s `App::setLocale()` or middleware. Fallback logic respects the current locale and follows the configured hierarchy, making it ideal for user-specific language preferences.
- What’s the performance impact of using fallback translations?
- The overhead is minimal (~1ms per fallback lookup). For high-traffic APIs, enable Laravel’s translation cache (`php artisan cache:clear` after updates) to optimize performance further.
- Can I use this package alongside PHP array-based translations?
- No, this package only works with JSON-based translations in the `lang/json/` directory. Mixed setups require manual migration or a hybrid solution like customizing the `JsonFallbackLoader` to handle both formats.
- Are there alternatives to this package for JSON fallback support?
- For Laravel, alternatives are limited. You could build a custom `JsonLoader` extension or use a hybrid approach with middleware to handle fallbacks, but this package provides the most seamless, framework-native solution for JSON-specific needs.