- How do I install this package in a Laravel project?
- Run `composer require patrickschur/language-detection` in your project root. Ensure the `mbstring` PHP extension is enabled, as it’s required for multibyte string handling. The package integrates cleanly with Laravel’s service container and facades.
- Which Laravel versions does this package support?
- The package works with Laravel 8+ and PHP 7.4+. For Laravel 10+ (PHP 8.4), it includes nullable type support for better IDE integration and type safety, but existing code remains fully backward-compatible.
- Can I detect languages in real-time for user-submitted content?
- Yes, the package is optimized for real-time detection. It processes text quickly and supports high-volume use cases like user comments or forum posts. For edge cases, combine with fallback logic (e.g., default locale or user preferences).
- How accurate is the language detection for short texts or slang?
- Accuracy depends on text length and language complexity. The package uses N-gram analysis and includes training data for 110 languages. For short or informal texts, pair it with rule-based fallbacks (e.g., user locale or domain-specific rules).
- Does this package work with Laravel’s service container and facades?
- Absolutely. Bind the detector to Laravel’s container via a service provider or use the facade (`LanguageDetector::detect($text)`). The package supports nullable types, so you can safely handle optional inputs in typed Laravel applications.
- How do I handle null or empty inputs in Laravel middleware?
- The package’s nullable type support (`detect(?string $text): ?string`) makes it trivial to handle null inputs in middleware. For example, `LanguageDetector::detect($request->input('content'))` will return `null` gracefully if the input is missing.
- Can I customize the language model or add new languages?
- Yes, the package allows training custom language models. You can extend the default 110-language dataset by providing additional text samples and regenerating the N-gram database. Check the README for training instructions.
- What’s the best way to cache detection results in Laravel?
- Cache results using Laravel’s cache system (e.g., `Cache::remember()`) with a key like `language_{md5($text)}`. This avoids redundant processing for repeated texts. For null inputs, cache the result to prevent redundant checks.
- Are there alternatives for high-precision language detection?
- For specialized use cases (e.g., code, medical text), consider dedicated libraries like `google/cloud-language` or `symfony/translation`. However, this package is ideal for general-purpose text detection with a lightweight footprint.
- How do I test this package in a Laravel CI pipeline?
- Test with PHPUnit by mocking the detector or using real text samples. Validate nullable inputs (e.g., `detect(null)`) and edge cases like mixed-language texts. Ensure your CI includes PHP 8.4 tests if targeting Laravel 10+.