PHPackages                             anacreation/translatable - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. anacreation/translatable

ActiveLib[Localization &amp; i18n](/categories/localization)

anacreation/translatable
========================

Translate Model

00PHP

Since Jun 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/anacreation/translatable)[ Packagist](https://packagist.org/packages/anacreation/translatable)[ RSS](/packages/anacreation-translatable/feed)WikiDiscussions develop Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Overview
========

[](#overview)

This add translation to `Eloquent Model`

Installation
============

[](#installation)

```
composer require anacreation/translatable
```

Eloquent model that use the

```
namespace App;

use Anacreation\Translatable\traits\TranslatableTrait;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use TranslatableTrait;
}
```

can use the following APIs

Usage
-----

[](#usage)

Create and update translations
------------------------------

[](#create-and-update-translations)

#### `createModelWithTranslations( array $attributes = [], array $content): Model`

[](#createmodelwithtranslations-array-attributes---array-content-model)

the `$content` has a predefine format

```
$content = [
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
        "attribute_3" => "value 3",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 4",
        "attribute_2" => "value 5",
        "attribute_3" => "value 6",
    ]
]

$newModel = Model::createModelWithTranslations($attributes, $content);

```

it create a model instance and save the content

#### `updateTranslations(array $content): void`

[](#updatetranslationsarray-content-void)

No matter for create new language transaltion or update existing language. Simple call this funciton. The $content structure is same as above.

Retrieve translation
--------------------

[](#retrieve-translation)

if you have a model with translation as above.

then you can

```
$mode->attribute_1;
```

this will automatically fetch the translation base on your current locale setting.

```
app()->getLocale();
```

### Fallback

[](#fallback)

Then defalut fallback is set to `false`, no fallback if content is `null`.

The fall back system is very simple. if you have set the config fallback\_locale **and** set the eloquent model fallback to `true`

```
$model->fallback = true;
```

then if the translation for particular attribute is `null`. It will try to get the translation for fallback locale.

### Retrieve all translation

[](#retrieve-all-translation)

```
$translation_array = $model->translatables;
```

this will return a array as above. The translation array has structure as below:

```
[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
        "attribute_2" => "value 4",
    ]
]

```

Delete Translation
------------------

[](#delete-translation)

#### `deleteTranslatableAttribute( string $key, string $locale = null ): void`

[](#deletetranslatableattribute-string-key-string-locale--null--void)

This will delete specfic attribute for all or speficied transaltion. If the original translation as below:

```
[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
        "attribute_2" => "value 4",
    ]
]

```

We call

```
    $model->deleteTranslatableAttribute("attribute_2");
```

the translation will become

```
[
    "language_code_1"=>[
        "attribute_1" => "value 1",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
    ]
]

```

if we call

```
    $model->deleteTranslatableAttribute("attribute_2", "language_code_2");
```

the result will as below

```
[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ],

    "language_code_2"=>[
        "attribute_1" => "value 3",
    ]
]

```

#### `deleteTranslatableWithLocale(string $locale ): void`

[](#deletetranslatablewithlocalestring-locale--void)

This will remove all translation with specified locale. if we call

```
    $model->deleteTranslatableWithLocale("language_code_2");
```

the result will as below

```
[
    "language_code_1"=>[
        "attribute_1" => "value 1",
        "attribute_2" => "value 2",
    ]
]

```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/710ed9dde4b8cbcbc613ec7227fdf6c672675c93eb1077847c47b9ee5d006dd2?d=identicon)[Xavier Au](/maintainers/Xavier%20Au)

---

Top Contributors

[![xavierauana](https://avatars.githubusercontent.com/u/11531712?v=4)](https://github.com/xavierauana "xavierauana (9 commits)")

### Embed Badge

![Health badge](/badges/anacreation-translatable/health.svg)

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

###  Alternatives

[smmoosavi/php-gettext

Wrapper for php-gettext by danilo segan. This library provides PHP functions to read MO files even when gettext is not compiled in or when appropriate locale is not present on the system.

1926.6k1](/packages/smmoosavi-php-gettext)[laradevs/spanish

labels translated to spanish

166.7k](/packages/laradevs-spanish)

PHPackages © 2026

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