PHPackages                             edulazaro/laratext - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. edulazaro/laratext

ActiveLibrary

edulazaro/laratext
==================

Laravel localization package

1.2.11(2mo ago)752.7k↓23.5%7MITPHPPHP ^8.2

Since Apr 5Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/edulazaro/laratext)[ Packagist](https://packagist.org/packages/edulazaro/laratext)[ RSS](/packages/edulazaro-laratext/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (17)Used By (0)

Laratext for Laravel
====================

[](#laratext-for-laravel)

 [![Total Downloads](https://camo.githubusercontent.com/f23f31a717b30f8129e18f30f0da6c31316fd062c3dce0c25cf964364d60d8f3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6564756c617a61726f2f6c61726174657874)](https://packagist.org/packages/edulazaro/laratext) [![Latest Stable Version](https://camo.githubusercontent.com/7920948f2151209e3316082319dddb064070f7f2df8020753ffd15cb8c8f3f0f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6564756c617a61726f2f6c61726174657874)](https://packagist.org/packages/edulazaro/laratext)

Introduction
------------

[](#introduction)

Laratext is a Laravel package designed to manage and auto-translate your application's text strings. In laravel, when using the `__` gettext helper method you specify the translation or the key. Both options have issues. If you specify the key, the file becomes difficult to read, as you don't know what's there. If you specify the text, your translations will break if you change a single character. With Laratext you specify both the key and the text, making it useful and readable.

It also allows you to seamlessly integrate translation services (like OpenAI or Google Translate) into your Laravel application to automatically translate missing translation keys across multiple languages.

It includes these 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
------------

[](#installation)

Execute the following command in your Laravel root project directory:

```
composer require edulazaro/laratext
```

To publish the configuration run:

```
php artisan vendor:publish --tag="texts"
```

Or if for some reason it does not work:

```
php artisan vendor:publish --provider="EduLazaro\Laratext\LaratextServiceProvider" --tag="texts"
```

This will generate the `texts.php` configuration file in the `config` folder.

Configuration
-------------

[](#configuration)

The `texts.php` configuration file contains all the settings for the package, including API keys for translation services, supported languages, and more.

Example of the configuration (`config/texts.php`):

```
return [
    // Default Translator
    'default_translator' => EduLazaro\Laratext\Translators\OpenAITranslator::class,

    // Translator Services
    'translators' => [
        'openai' => EduLazaro\Laratext\Translators\OpenAITranslator::class,
        'google' => EduLazaro\Laratext\Translators\GoogleTranslator::class,
    ],

    // OpenAI Configuration
    'openai' => [
        'api_key' => env('OPENAI_API_KEY'),
        'model' => env('OPENAI_MODEL', 'gpt-3.5-turbo'),
        'timeout' => 60,
        'retries' => 3,
    ],

    // Google Translator Configuration
    'google' => [
        'api_key' => env('GOOGLE_TRANSLATOR_API_KEY'),
        'timeout' => 20,
        'retries' => 3,
    ],

    // List the supported languages for translations.
    'languages' => [
        'en' => 'English',
        'es' => 'Spanish',
        'fr' => 'French',
    ],
];
```

This configuration allows you to define your translation services, API keys, and the supported languages in your Laravel application.

This is an example of the `.env`:

```
OPENAI_API_KEY=your_openai_api_key
GOOGLE_TRANSLATOR_API_KEY=your_google_api_key

```

Usage
-----

[](#usage)

Here is how you can use the blade directive and the `text` function:

Use the `text()` helper function to fetch translations within your PHP code.

```
text('key_name', 'default_value');
```

Use the `@text` Blade directive to fetch translations within your views.

```
@text('key_name', 'default_value')
```

### Auto-Generated Text from Keys

[](#auto-generated-text-from-keys)

You can also use just the key without providing a default value. The system will automatically generate readable text from the key name:

```
// PHP usage with auto-generated text
text('hello_mate');        // Auto-generates: "Hello Mate"
text('welcome_back');      // Auto-generates: "Welcome Back"
text('user.first_name');   // Auto-generates: "First Name" (uses last part after dot)
```

```
{{-- Blade usage with auto-generated text --}}
@text('hello_mate')        {{-- Auto-generates: "Hello Mate" --}}
@text('welcome_back')      {{-- Auto-generates: "Welcome Back" --}}
@text('pages.contact_us')  {{-- Auto-generates: "Contact Us" --}}
```

The auto-generation works by:

- Taking the last part after dots (e.g., `pages.contact_us` → `contact_us`)
- Replacing underscores with spaces (e.g., `contact_us` → `contact us`)
- Capitalizing each word (e.g., `contact us` → `Contact Us`)

### Replacement Texts (Placeholders)

[](#replacement-texts-placeholders)

You can include placeholders in your text strings using the `:placeholder` syntax. These placeholders will be preserved during translation and can be replaced with actual values when displaying the text.

**Basic usage without replacements:**

```
// PHP - displays text as-is with placeholders
echo text('welcome.user', 'Welcome, :name!');
// Output: "Welcome, :name!"
```

```
{{-- Blade - displays text as-is with placeholders --}}
@text('welcome.user', 'Welcome, :name!')
{{-- Output: "Welcome, :name!" --}}
```

**Usage with replacement values:**

```
// PHP - replaces placeholders with actual values
echo text('welcome.user', 'Welcome, :name!', ['name' => 'John']);
// Output: "Welcome, John!" (or "¡Bienvenido, John!" in Spanish)

echo text('items.count', 'You have :count items.', ['count' => 5]);
// Output: "You have 5 items." (or "Tienes 5 artículos." in Spanish)
```

```
{{-- Blade - both syntaxes work identically --}}
{{ text('welcome.user', 'Welcome, :name!', ['name' => $user->name]) }}
@text('items.count', 'You have :count items in your cart.', ['count' => $cartItems])
@text('file.uploaded', ':count file uploaded.', ['count' => $fileCount])
```

When these texts are scanned and translated, the placeholders (`:name`, `:count`, etc.) will be preserved in all target languages.

Scanning Translations
---------------------

[](#scanning-translations)

You can use the `laratext:scan` command to scan your project files for missing translation keys and translate them into multiple languages:

```
php artisan laratext:scan --write
```

You can also specify the target language or the translator to use:

```
php artisan laratext:scan --write --lang=es --translator=openai
```

These are the command Options:

- `--write`: Write the missing keys to the language files.
- `--lang`: Target a specific language for translation (e.g., es for Spanish).
- `--dry` Perform a dry run (do not write).
- `--diff`: Show the diff of the changes made.
- `--resync`: Retranslate texts when source language values have changed.
- `--translator`: Specify the translator service to use (e.g., openai or google).

### Resyncing Changed Translations

[](#resyncing-changed-translations)

By default, the scan command only processes missing translation keys. If you've updated the source text for an existing key, use the `--resync` option to retranslate all languages when source values have changed:

```
php artisan laratext:scan --write --resync
```

This is useful when you've modified existing text in your code and want to update all translations to reflect the changes. Without `--resync`, only missing keys are translated, but existing keys with changed source values are left unchanged.

Creating translators
--------------------

[](#creating-translators)

To create a custom translator, you need to implement the `TranslatorInterface`. This will define the structure and method that will handle the translation.

To facilitate the creation of custom translators, you can create a `make:translator` command that will generate the required files for a new translator class.

To create a translator run:

```
php artisan make:translator BeautifulTranslator
```

This will create the `BeautifulTranslator.php` file in the `app/Translators` directory:

```
namespace App\Translators;

use EduLazaro\Laratext\Contracts\TranslatorInterface;

class BeautifulTranslator implements TranslatorInterface
{
    public function translate(string $text, string $from, array $to): array
    {
        // TODO: Implement your translation logic here.

        $results = [];

        foreach ($to as $language) {
            $results[$language] = $text; // Dummy return same text
        }

        return $results;
    }
}
```

The `translate` method, which translates a single string into one or more target languages, is required:

```
translate(string $text, string $from, array $to): array

```

Optionally, you can implement the `translateMany` method to translate multiple texts in batch, which can improve performance when supported by the translation API:

```
translateMany(array $texts, string $from, array $to): array

```

If `translateMany` is not implemented, only single-string translations (translate) will be available for batch processing. For full support, both methods are recommended, so there are less requests and create a cost effective solution.

License
-------

[](#license)

Larakeep is open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance84

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~23 days

Recently: every ~38 days

Total

15

Last Release

83d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a3c47449dfb2ec121aa410da024f47586b87cc2799a825f0418e6c5e5904955?d=identicon)[edulazaro](/maintainers/edulazaro)

---

Top Contributors

[![edulazaro](https://avatars.githubusercontent.com/u/7797530?v=4)](https://github.com/edulazaro "edulazaro (23 commits)")[![ylynfatt](https://avatars.githubusercontent.com/u/19831?v=4)](https://github.com/ylynfatt "ylynfatt (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/edulazaro-laratext/health.svg)

```
[![Health](https://phpackages.com/badges/edulazaro-laratext/health.svg)](https://phpackages.com/packages/edulazaro-laratext)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11120.2M21](/packages/anourvalar-eloquent-serialize)[namu/wirechat

A Laravel Livewire messaging app for teams with private chats and group conversations.

54324.5k](/packages/namu-wirechat)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
