2017 © Pedro Peláez
 

lithium-library li3_translate

A translate behavior for the li3 PHP framework

image

davidpersson/li3_translate

A translate behavior for the li3 PHP framework

  • Wednesday, July 4, 2018
  • by davidpersson
  • Repository
  • 2 Watchers
  • 0 Stars
  • 1,374 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 5 % Grown

The README.md

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., (*1)

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

Installation

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

composer require davidpersson/li3_translate

Usage

In the model you wish to have translatable please add something to the tune of:, (*4)

// ...
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., (*5)

  • The locales that you want to use is fairly self explanatory, it simply tells the plugin which languages you want support for., (*6)

  • 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., (*7)

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

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):, (*9)

$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)., (*10)

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

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

// ... or ...

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

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., (*11)

$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., (*12)

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

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

On-the-fly Disabling of Translations

You can disable the automatic retrieval of translations for a record:, (*14)

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

And disable running the behavior on save:, (*15)

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

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

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

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

Data Model

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

  • <user>, (*18)

    • name => Richard
    • profile
    • <i18n>
      • name
        • it => Ricardo
  • <user>, (*19)

    • name => Richard
    • profile
    • i18n_name_it => Ricardo

Gotchas

You should not change the locale when the model already has saved data. Otherwise manual migration will be required., (*20)

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., (*21)

The Versions

04/07 2018

2.1.x-dev

2.1.9999999.9999999-dev

A translate behavior for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

04/07 2018

v2.1.0

2.1.0.0

A translate behavior for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

10/10 2017

v2.1.0-alpha

2.1.0.0-alpha https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

09/05 2017

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

09/05 2017

v2.0.3

2.0.3.0 https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

13/04 2016

v2.0.2

2.0.2.0 https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

08/04 2016

dev-master

9999999-dev https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

08/04 2016

v2.0.1

2.0.1.0 https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

05/09 2015

v2.0.0

2.0.0.0 https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

05/09 2015

v2.0.0-rc1

2.0.0.0-RC1 https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

22/04 2015

v2.0.0-beta

2.0.0.0-beta https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3

20/02 2015

v1.0.0

1.0.0.0 https://github.com/davidpersson/li3_translate

A translate behavior for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

php behavior translate g11n lithium li3