zxf5115/laravel-phrase-module
Installation
composer require zxf5115/laravel-phrase-module
Publish the config and migrations:
php artisan vendor:publish --provider="Zxf5115\PhraseModule\PhraseServiceProvider" --tag="config"
php artisan vendor:publish --provider="Zxf5115\PhraseModule\PhraseServiceProvider" --tag="migrations"
php artisan migrate
First Use Case
Define a phrase in config/phrase.php:
'welcome' => [
'en' => 'Welcome to our platform',
'zh' => '欢迎来到我们的平台',
],
Retrieve it in a controller:
use Zxf5115\PhraseModule\Facades\Phrase;
$welcome = Phrase::get('welcome'); // Returns 'Welcome to our platform' (default locale)
Key Files to Review
config/phrase.php: Centralized phrase definitions.app/Models/Phrase.php: Model for dynamic phrases (if using DB storage).app/Providers/PhraseServiceProvider.php: Service provider for extensions.Static Phrases (Config-Based)
// Define in config/phrase.php
'errors' => [
'validation' => [
'required' => 'This field is required',
'email' => 'Invalid email format',
],
];
// Usage
Phrase::get('errors.validation.required');
Dynamic Phrases (Database-Driven)
// Create via Tinker or API
Phrase::create(['key' => 'dynamic.greeting', 'en' => 'Hello, {name}!']);
// Usage with interpolation
Phrase::get('dynamic.greeting', ['name' => 'John']); // 'Hello, John!'
Locale Switching
// Set locale dynamically
Phrase::setLocale('zh');
Phrase::get('welcome'); // Returns '欢迎来到我们的平台'
// Or via middleware (e.g., `app/Http/Middleware/SetLocale.php`)
Blade Integration
<h1>{{ phrase('welcome') }}</h1>
<p>{{ phrase('errors.validation.required') }}</p>
Register the directive in AppServiceProvider:
Blade::directive('phrase', function ($key) {
return "<?php echo \\Zxf5115\\PhraseModule\\Facades\\Phrase::get($key); ?>";
});
FormRequest class:
public function messages()
{
return [
'email' => Phrase::get('errors.validation.email'),
];
}
return response()->json([
'error' => Phrase::get('errors.generic'),
]);
Phrase::shouldReceive('get')->with('welcome')->andReturn('Test Welcome');
Caching Quirks
php artisan cache:clear
config/phrase.php for development:
'cache_enabled' => env('APP_ENV') !== 'local',
Locale Fallback
app/config/app.php). Ensure your config/phrase.php includes all required locales.Key Collisions
.) in phrase keys if you later need to access them as nested arrays. Use underscores (_) instead:
// Good
'user_profile_name' => 'Profile Name'
// Bad (may cause issues with nested access)
'user.profile.name' => 'Profile Name'
Database Storage Overhead
config/phrase.php or the database. Enable debug mode:
Phrase::enableDebug(); // Logs missing phrases to Laravel logs
app locale in config/app.php matches your expected default.Custom Storage
Override the storage engine by binding a new implementation to the phrase.storage interface:
$this->app->bind('phrase.storage', function () {
return new CustomPhraseStorage();
});
Example interface:
interface PhraseStorage {
public function get($key, array $replace = [], $locale = null);
public function all($locale = null);
}
Event Hooks
Listen for phrase events (e.g., PhraseRetrieved) to log or modify phrases:
event(new PhraseRetrieved($key, $phrase, $locale));
Dynamic Phrase Validation
Extend the Phrase model to add custom validation rules:
use Illuminate\Database\Eloquent\Model;
class Phrase extends Model {
protected $rules = [
'key' => 'required|unique:phrases|regex:/^[a-z_]+(\.[a-z_]+)*$/i',
];
}
How can I help you explore Laravel packages today?