PHP code example of marksitko / laravel-deep-translatable
1. Go to this page and download the library: Download marksitko/laravel-deep-translatable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
marksitko / laravel-deep-translatable example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use MarkSitko\DeepTranslatable\UseDeepTranslations;
use MarkSitko\DeepTranslatable\Contracts\Translatable;
class Post extends Model implements Translatable
{
use UseDeepTranslations;
protected $fillable = [
'title',
'body',
];
protected $translatable = [
'title',
'body',
];
}
use App\Models\Post;
use MarkSitko\DeepTranslatable\Lang;
$post = Post::create([
'title' => 'My awesome title!',
'body' => 'This is the main content',
]);
$post->translateViaDictionary(Lang::DE);
// to swap the post language,
// simply call your url with an additional query 'lang' parameter.
// https://yousite.test/posts/1?lang=de
// It would return the post translatable keys:
// titel => 'Mein genialer Titel!'
// body => 'Dies ist der Hauptinhalt'
use App\Models\Post;
use MarkSitko\DeepTranslatable\Lang;
Post::first()->translate(Lang::DE);
class Fields extends Model implements Translatable
{
use UseDeepTranslations;
/**
* Imagine you have stored the following json object in the option column
*
* {
* option: {
* nested: {
* key: {
* value: 'I would like to be translated',
* },
* other_key: {
* value: 'I should not be translated',
* },
* },
* }
* }
*
* */
protected $translatable = [
'option.nested.key.value',
];
}