edulazaro/laratext
Laratext manages and auto-translates Laravel text strings by using both key and text for readable, stable translations. Includes @text directive and text() helper, scans/updates language files, and supports OpenAI, Google Translate, and more.
composer require edulazaro/laratext
php artisan vendor:publish --tag="texts"
.env (e.g., OPENAI_API_KEY) and set supported languages in config/texts.php.@text('welcome.message', 'Welcome to our platform!')
or in PHP:
text('welcome.message', 'Welcome to our platform!');
@text('key') for views.text('key', 'default') in PHP.text('user.profile') → "Profile" (no default needed).php artisan laratext:scan --write to auto-translate missing keys.Translation Key Management:
auth.login.failed).@text('user.name') → "Name").text()/@text for fallback.Placeholder Handling:
:placeholder syntax (e.g., @text('greeting', 'Hello, :name!', ['name' => $user->name])).Scanning and Syncing:
php artisan laratext:scan --write # Auto-translates new/drifted keys
php artisan laratext:scan --write --prune # Cleans orphaned keys
php artisan laratext:scan --write --resync # Full retranslation
Multi-Language Support:
config/texts.php (e.g., ['es' => 'Spanish']).--lang=es flag to target specific languages during scans.@text in views for readability.text() in controllers/services for dynamic logic.text('key.that.might.miss', 'Default fallback text');
TranslatorInterface in unit tests to avoid API calls:
$this->app->instance(
\EduLazaro\Laratext\Contracts\TranslatorInterface::class,
MockTranslator::class
);
API Rate Limits:
config/texts.php (e.g., retries, timeout).--only-missing in CI to avoid unnecessary API calls.Drift Detection:
laratext:scan --write retranslates drifted keys (source text changed in code).--only-missing to skip drift or manually edit lang/{locale}.json.Placeholder Mismatches:
:name) are missing in translations, ensure the default value includes them:
// Bad: Missing placeholder in default → translation may omit it.
text('greeting', 'Hello!', ['name' => $user->name]);
// Good: Include placeholder in default.
text('greeting', 'Hello, :name!', ['name' => $user->name]);
Orphaned Keys:
lang/ files but unused in code can bloat translations.--prune periodically:
php artisan laratext:scan --write --prune
Auto-Generated Text Quirks:
text('user_profile') → "User Profile") may not match your intent.--diff to preview changes:
php artisan laratext:scan --dry --diff
\Log::debug('Translation response:', $translator->translate(...));
php artisan laratext:scan without --write to list missing keys.Custom Translators:
TranslatorInterface for proprietary services:
php artisan make:translator DeepLTranslator
translateMany() for batch efficiency.Pre-Translation Hooks:
scan command to add validation:
// app/Console/Commands/ScanTranslations.php
protected function handle()
{
$keys = $this->collectKeys();
$this->validateKeys($keys); // Custom logic
parent::handle();
}
Post-Translation Processing:
event(new TranslatedText($key, $translation));
public function boot()
{
TranslatedText::listen(function ($event) {
$event->translation = strip_tags($event->translation);
});
}
translateMany() over single calls in custom translators.$translation = Cache::remember("text:{$key}:{$locale}", now()->addHours(1), function() use ($key, $locale) {
return text($key);
});
--lang=es,fr.How can I help you explore Laravel packages today?