Translating Elements

#Translatable Elements

There are certain element types starting with 't-' indicates that the value of that field can be translated to different languages when using $multilingual property:

Form's `data`:

      
          
<?php

namespace App\Forms;

use Laraform;

class ArticleForm extends Laraform
{
  public $multilingual = true;

  public $schema = [
    'title' => [
      'type' => 't-text',
      'label' => 'Title'
    ],
    'lead' => [
      'type' => 't-textarea',
      'label' => 'Lead'
    ],
    'content' => [
      'type' => 't-trix',
      'label' => 'Content'
    ],
    'date' => [
      'type' => 'date',
      'label' => 'Date'
    ]
  ];
}
          
        

As you can see Title, Lead and Content are being translated to multiple languages and their values are objects instead of simple strings. Date however remained a simple string value representing a date.

We are not getting into the full functionality of translatable elements because it's already covered here and rather we move on to processing their data.

#Storing Values

By default if you attempt to insert a translatable element's value into a single column it will be stored as a JSON.

You can also implement your custom storing method or you can use Laraform's built-in translator that supports dimsav/laravel-translatable.

To create your custom translator checkout laraform/src/Contracts/Translator for the interface or laraform/src/Translator directory for existing implementations.

#Using Dimsav

There is a popular library dimsav/laravel-translatable which Laraform supports out of the box. This means that there is a translator class that can be used to make an element compatible with dimsav's translating solution. To use it, simple add the following class to your element as translator option:

<?php

namespace App\Forms;

use Laraform;
use Laraform\Translator\Dimsav;

class ArticleForm extends Laraform
{
  public $multilingual = true;

  public $schema = [
    'title' => [
      'type' => 't-text',
      'label' => 'Title',
      'translator' => Dimsav::class
    ]
  ];
}

Now when you set up your model using dimsav's solution and make title a translatable attribute you'll see that title will be placed in the translations table.