PHPackages                             davidpersson/li3\_translate - 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. davidpersson/li3\_translate

AbandonedArchivedLithium-library[Localization &amp; i18n](/categories/localization)

davidpersson/li3\_translate
===========================

A translate behavior for the Lithium PHP framework

v2.1.0(7y ago)01.8k7BSD-3-ClausePHPPHP ^5.5 || ^7

Since Feb 20Pushed 7y ago2 watchersCompare

[ Source](https://github.com/davidpersson/li3_translate)[ Packagist](https://packagist.org/packages/davidpersson/li3_translate)[ RSS](/packages/davidpersson-li3-translate/feed)WikiDiscussions 2.0 Synced 1mo ago

READMEChangelogDependencies (3)Versions (12)Used By (7)

Translatable Behavior
=====================

[](#translatable-behavior)

What this behavior does is enable you to have content of different locales/languages to be stored in your database via your lithium based model. You can also search and retrieve locale specific data simply.

*At this moment the plugin is compatible with MongoDB and all relational databases supported by lithium.*

Installation
------------

[](#installation)

Install the plugin via composer (this will also pull in any dependencies):

```
composer require davidpersson/li3_translate
```

Usage
-----

[](#usage)

In the model you wish to have translatable please add something to the tune of:

```
// ...
class Artists extends \lithium\data\Model {

   use li3_behaviors\data\model\Behaviors;

   protected static $_actsAs = [
       'Translatable' => [
           'default' => 'ja',
           'locales' => ['en', 'it', 'ja'],
           'fields' => ['name']
       ]
   ];

   // ...
```

- The default option is required, espececially if you are saving multiple languages in one create or save command. A base language of which to gather the content and validate against is needed. This ensures that your validations will still work.
- The locales that you want to use is fairly self explanatory, it simply tells the plugin which languages you want support for.
- So as not to double up on too much data. The fields array tells the behavior which fields will need localizations. Those that are not included here will be simple fields which will not be attached a locale.

Good example usage of the plugin can be seen in the unit tests, but here is a brief description.

Saving Data
-----------

[](#saving-data)

When saving data with the default locale, you basically don't have to change anything. When saving translated data along with the original data use one of the following syntax (all are equivalent):

```
$user = Users::create([
	'profile' => 'Dreaded Rasta',
	'name' => 'Richard',
	'i18n.name.it' => 'Ricardo'
]);

$user = Users::create([
	'name' => 'Richard',
	'profile' => 'Dreaded Rasta',
	'i18n' => [
		'name' => [
			'it' => 'Ricardo'
		]
	]
]);

$user = Users::create([
	'profile' => 'Dreaded Rasta',
	'name' => 'Richard'
]);
$user->translate('name', 'it', 'Ricardo');
```

When *saving just translated data* i.e. when updating an already existing record use the following syntax. Please note that in this case original data (for the default locale must already be present).

```
$user = Users::find('first', ['conditions' => ['name' => 'Richard']]);

$user->save([
	'i18n.name.it' => 'Ricardo'
]);

// ... or ...

$user->translate('name', 'it', 'Ricardo');
$user->save();
```

Retrieving translated Entities
------------------------------

[](#retrieving-translated-entities)

```
$user = Users::find('first', [
	'conditions' => ['i18n.name.it' => 'Ricardo']
]);

$user = Users::find('all', [
	'order' => ['i18n.name.it' => 'ASC']
]);
```

If you don't want to use the `translate()` method to translate single fields, but want the record translated into a single locale use the following syntax. You can then retrieve field data as normal.

```
$user = Users::find('first', [
	'conditions' => ['id' => 23],
	'translate' => 'it'
]);

$user->name; // returns 'Ricardo'.
```

This is good for display purposes. For saving data use the syntax described above.

If you do not know the translation you are searching for, the translation can be searched by the following:

```
$users = Users::all(['conditions' => ['i18n.name' => 'Ricardo']]);

```

On-the-fly Disabling of Translations
------------------------------------

[](#on-the-fly-disabling-of-translations)

You can disable the automatic retrieval of translations for a record:

```
$user = Users::find('first', [
	'conditions' => ['name' => 'Richard'],
	'translate' => false
]);
```

And disable running the behavior on save:

```
$user->save(null, ['translate' => false]);
```

Accessing Translations
----------------------

[](#accessing-translations)

```
$user = Users::find('first', ['conditions' => ['name' => 'Richard']]);

$user->translate('name', 'it'); // returns 'Ricardo';
$user->translate('name'); // returns ['en' => 'Richard', 'it' => 'Ricardo'];
$user->name; // returns 'Richard', as the default locale is `en`.
```

Validation
----------

[](#validation)

When translations are present in the to-be-saved data, all are validated against the base rule.

```
$user = Users::create([
	'profile' => 'Dreaded Rasta',
	'name' => 'Richard'
]);
$user->validate(['translate' => false]);
```

Data Model
----------

[](#data-model)

Translation data is stored inline with the entity. For MongoDB a subdocument will used, for relational databases special field names are used.

- ``

    - `name => Richard`
    - `profile`
    - ``
        - `name`
            - `it => Ricardo`
- ``

    - `name => Richard`
    - `profile`
    - `i18n_name_it => Ricardo`

Gotchas
-------

[](#gotchas)

You should not change the locale when the model already has saved data. Otherwise manual migration will be required.

I have yet tested this plugin for white lists and other features. If you find a case that doesn't work then please log an issue.

###  Health Score

32

—

LowBetter than 71% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 76.1% 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 ~123 days

Recently: every ~105 days

Total

11

Last Release

2865d ago

Major Versions

v1.0.0 → v2.0.0-beta2015-04-22

PHP version history (3 changes)v1.0.0PHP &gt;=5.3

v2.0.0-betaPHP &gt;=5.5

v2.1.0PHP ^5.5 || ^7

### Community

Maintainers

![](https://www.gravatar.com/avatar/a358e1a5ab7f7c81ecaae07acd830aa4a36726ab4da46a11df754a6e5b3a4cda?d=identicon)[mariuswilms](/maintainers/mariuswilms)

---

Top Contributors

[![mariuswilms](https://avatars.githubusercontent.com/u/29702?v=4)](https://github.com/mariuswilms "mariuswilms (35 commits)")[![mackstar](https://avatars.githubusercontent.com/u/197328?v=4)](https://github.com/mackstar "mackstar (11 commits)")

---

Tags

phptranslatelithiumBehaviorli3g11n

### Embed Badge

![Health badge](/badges/davidpersson-li3-translate/health.svg)

```
[![Health](https://phpackages.com/badges/davidpersson-li3-translate/health.svg)](https://phpackages.com/packages/davidpersson-li3-translate)
```

###  Alternatives

[stichoza/google-translate-php

Free Google Translate API PHP Package

2.0k7.6M124](/packages/stichoza-google-translate-php)[gettext/languages

gettext languages with plural rules

7530.3M10](/packages/gettext-languages)[punic/punic

PHP-Unicode CLDR

1542.9M29](/packages/punic-punic)[mediawiki/translate

The only standard solution to translate any kind of text with an avant-garde web interface within MediaWiki, including your documentation and software

457.9k](/packages/mediawiki-translate)

PHPackages © 2026

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