Laratext is a Laravel package by Eduardo Lázaro that makes managing and localizing text strings in Laravel apps easier. It improves Laravel's built-in __()
method by letting developers set both a translation key and default text, which helps with readability and reduces translation mistakes. Plus, it can connect with services like OpenAI and Google Translate to automatically fill in missing translations for different languages, saving a lot of manual work.
Features
- Simplifies working with language files in Laravel.
- Auto-translate missing translation keys to multiple languages.
- Supports multiple translation services (e.g., OpenAI, Google Translate).
- Easy-to-use Blade directive (
@text
) and helper functions (text()
). - Commands to scan and update translation files.
Installation
Install this package using Composer:
composer require edulazaro/laratext
Next, publish the configuration:
php artisan vendor:publish --tag="texts"
The newly created config/texts.php
file allows you to configure translation services, API keys, and supported languages.
Usage
This package provides a text()
helper function that can be used in your PHP code or @text()
Blade directive. For example:
text('messages.welcome', 'Welcome to Laravel News'); @text('messages.welcome', 'Welcome to Laravel News')
You can use the laratext:scan
Artisan command to scan your Laravel project for missing translation keys and optionally translate them into multiple languages.
php artisan laratext:scan --write --lang=es --translator=openai
Note: If you are using one of the translation services, add your API key to the .env
file.
OPENAI_API_KEY=your_openai_api_keyGOOGLE_TRANSLATOR_API_KEY=your_google_api_key
Learn more about this package and view the source code on GitHub.