lasserafn/php-string-script-language
Detect which writing system a string uses with a simple PHP API. Check if text contains Arabic, Latin, Cyrillic, Thai, Han/Chinese, Japanese (Hiragana/Katakana), and many more scripts via boolean helpers like StringScript::isThai($text).
## Technical Evaluation
### **Architecture Fit**
- **Use Case Alignment**: The package’s focus on **script/language detection** (not just encoding) remains niche but critical for applications requiring granular text classification (e.g., multilingual CMS, NLP pipelines, or compliance tools like GDPR text analysis). New features in **v0.4** (e.g., improved script detection for CJK, RTL, and mixed-language texts) strengthen its fit for:
- **Content Moderation**: Auto-tagging user-generated content by script (e.g., Arabic vs. Hebrew for moderation workflows).
- **Localization Workflows**: Triggering script-specific validation (e.g., right-to-left text alignment for RTL scripts).
- **Analytics**: Segmenting text data by script for trend analysis (e.g., "What percentage of comments are in CJK scripts?").
- **Architectural Patterns**:
- **Event-Driven Extensions**: New `ScriptDetected` event (if added) could integrate with Laravel’s event system for reactive workflows (e.g., dispatching translation jobs).
- **Middleware**: Enhanced script detection could enable **script-aware routing** (e.g., `Route::middleware([DetectScript::class])`).
- **Service Layer**: The package’s stateless design still aligns with Laravel’s service container for dependency injection.
- **Alternatives**: Compare against **v0.4’s improvements** (e.g., CJK/RTL accuracy) vs. `symfony/intl` or `google/cloud-translate` for enterprise-grade needs. The package’s **MIT license** remains a plus for commercial use.
### **Integration Feasibility**
- **Laravel Ecosystem Compatibility**:
- **Service Providers**: v0.4’s updated API (e.g., `detectScript()`) can be wrapped in a Laravel facade for consistency:
```php
use App\Facades\ScriptDetector;
$script = ScriptDetector::detect($text); // Returns 'Arabic', 'Han', etc.
```
- **Artisan Commands**: Extend with v0.4’s new features (e.g., `php artisan detect:scripts resources/locales/* --cjk`).
- **Database**: Add `script` column (e.g., `enum('Latin', 'Arabic', 'Han', 'Hebrew')`) alongside existing `language`/`encoding` columns.
- **Caching Layer**:
- Cache **script detection results** separately from language/encoding to avoid redundant calls (e.g., `Cache::remember("script_{$md5}", 3600, fn() => ScriptDetector::detect($text))`).
- **Testing**:
- Leverage v0.4’s **improved edge-case handling** (e.g., mixed scripts) in Laravel’s PHPUnit/Pest tests with assertions like:
```php
$this->assertEquals('Arabic', ScriptDetector::detect('مرحبا بالعالم'));
```
### **Technical Risk**
- **Accuracy Improvements**:
- **v0.4’s Focus**: Better handling of **CJK (Chinese/Japanese/Korean), RTL (Arabic/Hebrew), and mixed scripts** reduces false positives/negatives for these use cases.
- **Validation Needed**: Test against **real-world datasets** (e.g., Wikipedia dumps, social media comments) to quantify improvements over v0.3.
- **Dependency Risks**:
- **PHP 8.1+**: v0.4 may require PHP 8.1+ (check `composer.json`). Ensure compatibility with Laravel’s supported versions (e.g., 9.x/10.x).
- **Ext-Intl**: Confirm if v0.4 drops reliance on `ext-intl` or adds new extensions (e.g., `ext-mbstring`).
- **Breaking Changes**:
- **API Changes**: Review the changelog for deprecated methods (e.g., `detectLanguage()` → `detectScript()`). Update Laravel bindings accordingly.
- **Configuration**: Check for new required config options (e.g., `script_threshold` for confidence scores).
### **Key Questions**
1. **Script Detection Granularity**:
- Does our use case require **script-level** (e.g., "Arabic") vs. **language-level** (e.g., "Arabic (Modern Standard)") granularity?
- How will script detection interact with existing `language` metadata (e.g., "Arabic script" vs. "Arabic language")?
2. **Performance**:
- What’s the **latency impact** of v0.4’s improved algorithms? Benchmark against v0.3.
- Can script detection be **parallelized** (e.g., via Laravel Horizon) for bulk processing?
3. **Maintenance**:
- Is the package’s **GitHub activity** (e.g., issues/PRs) sufficient for long-term support, or should we fork?
- Does v0.4 introduce **new dependencies** that require vendor lock-in?
4. **Alternatives**:
- Compare v0.4’s **CJK/RTL accuracy** against paid APIs (e.g., AWS Textract) or open-source tools like `fasttext` for language identification.
---
## Integration Approach
### **Stack Fit**
- **PHP/Laravel Synergy**:
- **Native Integration**: v0.4’s PHP-centric design integrates seamlessly with Laravel’s:
- **Service Container**: Bind the package as a singleton:
```php
$this->app->singleton(ScriptDetector::class, fn() => new \LasseRafn\ScriptDetector());
```
- **Facades**: Create a fluent interface (e.g., `Script::detect($text)->getConfidence()`).
- **Testing**: Use Laravel’s `Mockery` to stub `ScriptDetector` in unit tests.
- **Tooling**:
- **Laravel Scout**: Index script metadata for search filtering (e.g., `->whereScript('Arabic')`).
- **Nova/Vue**: Add script detection to admin panels for content moderation (e.g., flag RTL text for review).
### **Migration Path**
1. **Proof of Concept (PoC)**:
- Test v0.4’s **script detection** against a dataset with known CJK/RTL/mixed-script texts.
- Compare results with v0.3 and `mb_detect_encoding()`.
2. **Incremental Rollout**:
- **Phase 1**: Add script detection to **new text inputs** (e.g., form submissions) using middleware:
```php
public function handle($request, Closure $next) {
$request->merge(['script' => ScriptDetector::detect($request->input('content'))]);
return $next($request);
}
```
- **Phase 2**: Backfill existing data via a Laravel queue job:
```php
Post::chunk(100, fn($posts) => foreach($posts as $post) {
$post->update(['script' => ScriptDetector::detect($post->content)]);
});
```
- **Phase 3**: Integrate into **business logic** (e.g., route RTL scripts to a dedicated moderator).
3. **Fallback Strategy**:
- Chain fallbacks: `v0.4 → mb_detect_encoding → manual override`.
- Log fallback usage to monitor accuracy.
### **Compatibility**
- **Laravel Versions**:
- Test v0.4 against **Laravel 9/10** for compatibility with Symfony components (e.g., `HttpFoundation`).
- Verify no conflicts with Laravel’s `Illuminate\Support\Str` or `Illuminate/Translation`.
- **Database**:
- Add a `script` column (e.g., `enum('Latin', 'Arabic', 'Han', 'Hebrew')`) to text-heavy tables.
- Index the column if used for querying (e.g., `->whereScript('Arabic')`).
- **Third-Party Services**:
- Ensure detected scripts match API expectations (e.g., AWS Translate’s `languageCode` vs. this package’s `script`).
### **Sequencing**
1. **Pre-requisites**:
- Update `composer.json` to require `lasserafn/php-string-script-language:^0.4`.
- Install PHP extensions (e.g., `ext-intl`, `ext-mbstring`) if v0.4 requires them.
- Set up caching (Redis/Memcached) for script detection results.
2. **Core Integration**:
- Publish the package as a Laravel service provider with bindings for:
- `ScriptDetector` (core class).
- `ScriptDetectorFacade` (for fluent syntax).
- Create an Artisan command for bulk script detection:
```bash
php artisan detect:scripts --path=storage/app/texts --output=storage/app/scripts.csv
```
3. **Extensibility**:
- Add a `ScriptDetected` event to trigger downstream actions (e.g., translation jobs):
```php
event(new ScriptDetected($text, $script));
```
- Build a `ScriptDetectorInterface` for test mocking:
```php
interface ScriptDetectorInterface { public function detect(string $text): string; }
```
4. **Monitoring**:
- Log detection results to track accuracy over time (e.g., `ScriptDetectionLog` table).
- Alert on high failure rates (e
How can I help you explore Laravel packages today?