translateable cms pages

This commit is contained in:
Arno Kaimbacher 2018-09-13 14:03:17 +02:00
parent ee8584a2d5
commit 0457e65565
8 changed files with 244 additions and 10 deletions

View File

@ -5,10 +5,12 @@ namespace App\Models;
use App\Models\ModelTrait; use App\Models\ModelTrait;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Dimsav\Translatable\Translatable;
class Page extends Model class Page extends Model
{ {
use ModelTrait; use ModelTrait;
use Translatable;
/** /**
* The database table used by the model. * The database table used by the model.
* *
@ -16,6 +18,8 @@ class Page extends Model
*/ */
protected $table; protected $table;
public $translatedAttributes = ['title', 'description'];
/** /**
* The guarded field which are not mass assignable. * The guarded field which are not mass assignable.
* *
@ -40,6 +44,7 @@ class Page extends Model
{ {
parent::__construct($attributes); parent::__construct($attributes);
$this->table = 'pages'; //config('module.pages.table'); $this->table = 'pages'; //config('module.pages.table');
// $this->defaultLocale = 'de';
} }
public function owner() public function owner()

View File

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
// models/CountryTranslation.php
class PageTranslation extends Model
{
public $timestamps = false;
//protected $fillable = ['description'];
protected $guarded = ['id'];
}

View File

@ -17,7 +17,8 @@
"laravelcollective/html": "^5.5.0", "laravelcollective/html": "^5.5.0",
"solarium/solarium": "^3.8", "solarium/solarium": "^3.8",
"yajra/laravel-datatables-oracle": "^8.8", "yajra/laravel-datatables-oracle": "^8.8",
"zizaco/entrust": "^1.9" "zizaco/entrust": "^1.9",
"dimsav/laravel-translatable": "8.*"
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "^1.8", "fzaninotto/faker": "^1.8",

70
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "01c12d1633f3bbbb6ddbff87d4516b4f", "content-hash": "77d562b002d6678573038e1e2101911c",
"packages": [ "packages": [
{ {
"name": "davejamesmiller/laravel-breadcrumbs", "name": "davejamesmiller/laravel-breadcrumbs",
@ -65,6 +65,64 @@
], ],
"time": "2017-09-14T08:23:50+00:00" "time": "2017-09-14T08:23:50+00:00"
}, },
{
"name": "dimsav/laravel-translatable",
"version": "v8.1",
"source": {
"type": "git",
"url": "https://github.com/dimsav/laravel-translatable.git",
"reference": "8bdcb62b580df98fc5d5540edc2d8aa9a2760db4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/dimsav/laravel-translatable/zipball/8bdcb62b580df98fc5d5540edc2d8aa9a2760db4",
"reference": "8bdcb62b580df98fc5d5540edc2d8aa9a2760db4",
"shasum": ""
},
"require": {
"illuminate/support": "5.5.*",
"php": ">=5.4.0"
},
"require-dev": {
"orchestra/testbench": "3.5.*",
"phpunit/phpunit": "~6.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Dimsav\\Translatable\\TranslatableServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Dimsav\\Translatable\\": "src/Translatable/"
},
"classmap": [
"tests"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Dimitrios Savvopoulos",
"email": "ds@dimsav.com",
"homepage": "http://dimsav.com"
}
],
"description": "A Laravel package for multilingual models",
"keywords": [
"database",
"language",
"laravel",
"translation"
],
"time": "2018-01-04T20:11:56+00:00"
},
{ {
"name": "dnoegel/php-xdg-base-dir", "name": "dnoegel/php-xdg-base-dir",
"version": "0.1", "version": "0.1",
@ -1511,16 +1569,16 @@
}, },
{ {
"name": "swiftmailer/swiftmailer", "name": "swiftmailer/swiftmailer",
"version": "v6.1.2", "version": "v6.1.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/swiftmailer/swiftmailer.git", "url": "https://github.com/swiftmailer/swiftmailer.git",
"reference": "7d760881d266d63c5e7a1155cbcf2ac656a31ca8" "reference": "8ddcb66ac10c392d3beb54829eef8ac1438595f4"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/7d760881d266d63c5e7a1155cbcf2ac656a31ca8", "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/8ddcb66ac10c392d3beb54829eef8ac1438595f4",
"reference": "7d760881d266d63c5e7a1155cbcf2ac656a31ca8", "reference": "8ddcb66ac10c392d3beb54829eef8ac1438595f4",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1566,7 +1624,7 @@
"mail", "mail",
"mailer" "mailer"
], ],
"time": "2018-07-13T07:04:35+00:00" "time": "2018-09-11T07:12:52+00:00"
}, },
{ {
"name": "symfony/console", "name": "symfony/console",

117
config/translatable.php Normal file
View File

@ -0,0 +1,117 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Application Locales
|--------------------------------------------------------------------------
|
| Contains an array with the applications available locales.
|
*/
'locales' => [
'en',
'de',
'fr',
'es' => [
'MX', // mexican spanish
'CO', // colombian spanish
],
],
/*
|--------------------------------------------------------------------------
| Locale separator
|--------------------------------------------------------------------------
|
| This is a string used to glue the language and the country when defining
| the available locales. Example: if set to '-', then the locale for
| colombian spanish will be saved as 'es-CO' into the database.
|
*/
'locale_separator' => '-',
/*
|--------------------------------------------------------------------------
| Default locale
|--------------------------------------------------------------------------
|
| As a default locale, Translatable takes the locale of Laravel's
| translator. If for some reason you want to override this,
| you can specify what default should be used here.
|
*/
'locale' => null,
/*
|--------------------------------------------------------------------------
| Use fallback
|--------------------------------------------------------------------------
|
| Determine if fallback locales are returned by default or not. To add
| more flexibility and configure this option per "translatable"
| instance, this value will be overridden by the property
| $useTranslationFallback when defined
|
*/
'use_fallback' => false,
/*
|--------------------------------------------------------------------------
| Use fallback per property
|--------------------------------------------------------------------------
|
| The property fallback feature will return the translated value of
| the fallback locale if the property is empty for the selected
| locale. Note that 'use_fallback' must be enabled.
|
*/
'use_property_fallback' => true,
/*
|--------------------------------------------------------------------------
| Fallback Locale
|--------------------------------------------------------------------------
|
| A fallback locale is the locale being used to return a translation
| when the requested translation is not existing. To disable it
| set it to false.
|
*/
'fallback_locale' => 'en',
/*
|--------------------------------------------------------------------------
| Translation Suffix
|--------------------------------------------------------------------------
|
| Defines the default 'Translation' class suffix. For example, if
| you want to use CountryTrans instead of CountryTranslation
| application, set this to 'Trans'.
|
*/
'translation_suffix' => 'Translation',
/*
|--------------------------------------------------------------------------
| Locale key
|--------------------------------------------------------------------------
|
| Defines the 'locale' field name, which is used by the
| translation model.
|
*/
'locale_key' => 'locale',
/*
|--------------------------------------------------------------------------
| Always load translations when converting to array
|--------------------------------------------------------------------------
| Setting this to false will have a performance improvement but will
| not return the translations when using toArray(), unless the
| translations relationship is already loaded.
|
*/
'to_array_always_loads_translations' => true,
];

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePageTranslationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('page_translations', function (Blueprint $table) {
$table->increments('id');
$table->integer('page_id')->unsigned();
$table->string('locale')->index();
$table->string('title');
$table->text('description');
$table->unique(['page_id','locale']);
$table->foreign('page_id')->references('id')->on('pages')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('page_translations');
}
}

View File

@ -134,7 +134,9 @@
<div id="main"> <div id="main">
<div class="header"> <div class="header">
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1-2"><h1>Dashboard</h1></div> <div class="pure-u-1-2">
<h1>Dashboard</h1>
</div>
<div class="pure-u-1-2 text-right"> <div class="pure-u-1-2 text-right">
<section class="user-info"> <section class="user-info">
@if(Auth::user()) @if(Auth::user())

View File

@ -39,12 +39,12 @@
</div><!--col-lg-3--> </div><!--col-lg-3-->
</div><!--form control--> </div><!--form control-->
<div class="pure-control-group"> {{-- <div class="pure-control-group">
{{ Form::label('language', 'Language..', ['class' => 'col-lg-2 control-label required']) }} {{ Form::label('language', 'Language..', ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10"> <div class="col-lg-10">
{{ Form::select('language', $languages, $page->language, ['placeholder' => '--no language--']) }} {{ Form::select('language', $languages, $page->language, ['placeholder' => '--no language--']) }}
</div> </div>
</div> </div> --}}
<div class="pure-control-group"> <div class="pure-control-group">
{{ Form::label('cannonical_link', trans('validation.attributes.backend.pages.cannonical_link'), ['class' => 'col-lg-2 control-label']) }} {{ Form::label('cannonical_link', trans('validation.attributes.backend.pages.cannonical_link'), ['class' => 'col-lg-2 control-label']) }}