Download the PHP package rawnoq/laravel-locale-detector without Composer
On this page you can find all versions of the php package rawnoq/laravel-locale-detector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rawnoq/laravel-locale-detector
More information about rawnoq/laravel-locale-detector
Files in rawnoq/laravel-locale-detector
Package laravel-locale-detector
Short Description A Laravel package for automatic locale detection and translation key extraction
License MIT
Informations about the package laravel-locale-detector
Laravel Locale Detector
A professional Laravel package for automatic locale detection from HTTP headers and translation key extraction from your codebase.
๐ Table of Contents
- Features
- Requirements
- Installation
- Configuration
- Usage
- Translation Key Extraction
- Translatable Models
- Advanced Usage
- Testing
- License
โจ Features
- โ
Automatic Locale Detection - Automatically detects user's preferred language from
Accept-Languageheader - โ Translation Key Extraction - Extract all translation keys from your codebase
- โ
Multiple Detection Methods - Supports Laravel's
getPreferredLanguage()and direct header parsing - โ Configurable Supported Locales - Define which locales your application supports
- โ Global Middleware - Automatically registered middleware for seamless integration
- โ Multiple Output Formats - Export translation keys as JSON, PHP, or array format
- โ Namespace Filtering - Filter translation keys by namespace
- โ Exclude Paths - Exclude specific directories from key extraction
- โ Translation Key Synchronization - Sync translation keys to language files automatically
- โ 40+ Language Files - Includes translation files for auth, pagination, passwords, and validation in 40+ languages
- โ Translatable Models - Wrapper for Astrotomic Translatable with additional helper methods
- โ Database Migrations - Helper traits and classes for creating translation tables
- โ Translation Format Converter - Convert between attribute-based and locale-based translation formats
๐ฆ Requirements
- PHP 8.2 or higher
- Laravel 11.0+ or Laravel 12.0+
๐ Installation
Step 1: Install via Composer
The package will be automatically discovered and registered by Laravel.
Step 2: Publish Configuration (Optional)
This will publish the configuration file to config/locale-detector.php.
Step 3: Publish Language Files (Optional)
This will publish language files to lang/ directory. The package includes translation files for 40+ languages (auth, pagination, passwords, validation).
โ๏ธ Configuration
The configuration file is located at config/locale-detector.php:
Environment Variables
You can configure the package using environment variables in your .env file:
๐ฏ Usage
Automatic Locale Detection
The middleware automatically detects the user's preferred language from the Accept-Language header and sets the application locale accordingly.
No additional code required! The middleware is automatically registered and works out of the box.
Manual Locale Setting
You can still manually set the locale in your code:
Accessing Current Locale
๐ Translation Key Extraction
The package provides a powerful command to extract all translation keys from your codebase.
Basic Usage
Options
| Option | Description | Default |
|---|---|---|
--path |
The path to search for translation keys | base_path() |
--output |
The output file path for extracted keys | Display in console |
--format |
Output format (json, php, array) | json |
--exclude |
Comma-separated paths to exclude | None |
--namespace |
Filter keys by namespace | None |
--with-location |
Include file path and line number for each key | false |
Examples
Extract Keys from App Directory
Extract Keys and Save to File
Extract Keys in PHP Format
Exclude Vendor and Storage Directories
Filter Keys by Namespace
This will only extract keys that start with blog::.
Extract Keys with Location Information
This will extract keys with file path and line number information. The output will include:
key: The translation keyfile: The relative file pathline: The line number where the key was foundlocation: A formatted string likefile:linelocations: An array of all locations where the key appears (if the same key appears multiple times)
Example output:
Supported Translation Patterns
The command extracts keys from the following patterns:
__('key')- Laravel's translation helpertrans('key')- Translation function@lang('key')- Blade directiveLang::get('key')- Lang facade methodLang::trans('key')- Lang facade trans methodtrans_choice('key', ...)- Translation choice function__('namespace::key')- Namespaced translationstrans('namespace::key')- Namespaced translations
Output Formats
JSON Format
PHP Format
Array Format
๐ Translatable Models
The package provides a wrapper for Astrotomic Translatable with additional helper methods and utilities.
Using the Translatable Trait
Creating Models with Translations
Converting Translation Formats
The package provides helper functions to convert between different translation formats:
Using Helper Functions
Using Model Methods
Database Migrations with Translations
The package provides helper traits and classes for creating translation tables:
Using MigrationWithTranslations
Using the Trait Directly
Using UUID for Foreign Keys
๐ Advanced Usage
Custom Locale Detection
You can customize the locale detection logic by modifying the middleware or creating your own:
Integration with HMVC Modules
If you're using the rawnoq/laravel-hmvc package, you can extract translation keys from modules:
Automated Translation Key Management
You can create a script to automatically extract and update translation files:
๐ง Troubleshooting
Middleware Not Working
If locale detection is not working:
- Check if middleware is registered:
php artisan route:list - Verify configuration:
php artisan config:show locale-detector - Check
Accept-Languageheader in requests - Ensure supported locales are configured correctly
Translation Keys Not Found
If the extraction command finds no keys:
- Verify the path contains PHP files
- Check if translation functions are used correctly
- Ensure files are readable
- Check excluded paths
Command Not Found
If locale:extract-keys command is not found:
- Run
php artisan package:discover - Clear cache:
php artisan optimize:clear - Verify Service Provider is registered
๐ Examples
Example 1: Extract All Keys
Example 2: Extract from Specific Path
Example 3: Extract and Save to File
Example 4: Extract with Exclusions
Example 5: Extract Namespaced Keys Only
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐งช Testing
The package comes with a comprehensive test suite:
Test Coverage
The test suite includes:
- โ Middleware locale detection tests
- โ ServiceProvider registration tests
- โ Translation key extraction command tests
- โ Configuration tests
- โ Translation format converter tests
- โ Helper functions tests
- โ Edge cases and error handling
๐ License
This package is open-sourced software licensed under the MIT license.
๐ Credits
- Author: Rawnoq
- Package: rawnoq/laravel-locale-detector
- Version: 1.0.0
Made with โค๏ธ for the Laravel community
All versions of laravel-locale-detector with dependencies
astrotomic/laravel-translatable Version ^11.16
illuminate/http Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0
symfony/http-foundation Version ^7.0