readme.md 2.79 KB
Newer Older
Luca's avatar
Luca committed
1
# Nova Translatable Field
2
[![Latest Version on Packagist](https://img.shields.io/packagist/v/yeswedev/nova-translatable.svg?style=flat-square)](https://packagist.org/packages/yeswedev/nova-translatable)
Luca's avatar
Luca committed
3

4
5
This package is based on [mrmonat/nova-translatable](https://github.com/mrmonat/nova-translatable).

6
Adds the ability to show and edit translated fields created with [astrotomic/laravel-translatable](https://github.com/Astrotomic/laravel-translatable) package.
Luca's avatar
Luca committed
7
8
9
10
11

It will show up in the detail view like this:

<img width="400" src="https://mrmonat.de/github/images/nova-spatie-translatable-details.png">

Luca's avatar
Luca committed
12
And in the edit view like this:
Luca's avatar
Luca committed
13
14
15
16
17
18
19

<img width="400" src="https://mrmonat.de/github/images/nova-spatie-translatable-edit.png">

## Installation and usage
You can require this package using composer:

```
20
composer require yeswedev/nova-translatable
Luca's avatar
Luca committed
21
22
```

Luca's avatar
Luca committed
23
You can add the field follows:
Luca's avatar
Luca committed
24

25
26
``` php
use YesWeDev\Nova\Translatable\Translatable;
Luca's avatar
Luca committed
27

Luca's avatar
Luca committed
28
29
30
Translatable::make('Description'),
```

andypa's avatar
andypa committed
31
32
Make sure, that you have your Eloquent model setup correct:

33
- First, you need to add the `Astrotomic\Translatable\Translatable`-trait.
34
- Next, you should create a public property `$translatedAttributes` which holds an array with all the names of attributes you wish to make translatable.
andypa's avatar
andypa committed
35
36
37
38
39
40
- Finally, you should make sure that all translatable attributes are set to the `text`-datatype in your database. If your database supports `json`-columns, use that.

Here's an example of a prepared model:

``` php
use Illuminate\Database\Eloquent\Model;
41
use Astrotomic\Translatable\Translatable;
andypa's avatar
andypa committed
42
43
44

class NewsItem extends Model
{
45
    use Translatable;
andypa's avatar
andypa committed
46
    
47
    public $translatedAttributes = ['name'];
andypa's avatar
andypa committed
48
49
50
51
}
```


Luca's avatar
Luca committed
52
### Defining Locales
53
Locales can be defined via config file ```config/translatable.php``` by adding a ```locales``` array:
Luca's avatar
Luca committed
54

55
``` php
Luca's avatar
Luca committed
56
57
58
59
60
61
62
63
64
65
66
67
68
// config/translatable.php
return [
    ...
    'locales' => [
        'en' => 'English',
        'de' => 'German',
        'fr' => 'French',
    ],
];
```

Alternatively you can "override" the config locales with the ```locales(...)``` method:

69
``` php
Luca's avatar
Luca committed
70
71
72
Translatable::make('Description')->locales([
    'en' => 'English',
    'de' => 'German',
Luca's avatar
Luca committed
73
]),
Luca's avatar
Luca committed
74
75
```

Luca's avatar
Luca committed
76
### Single Line Option
Luca's avatar
Luca committed
77
By default the input field on the edit view is a textarea. If you want to change it to a single line input field you can add the ```singleLine()``` option:
Luca's avatar
Luca committed
78

79
``` php
Luca's avatar
Luca committed
80
81
82
83
84
85
Translatable::make('Description')->locales([...])->singleLine(),
```

### Trix Editor
You can use the trix editor for your translated fields by using the ```trix()``` option:

86
``` php
Luca's avatar
Luca committed
87
88
89
90
91
92
Translatable::make('Description')->trix(),
```

### Index View
By default the locale used when displaying the field on the index view is determined by ```app()->getLocale()```. To override this you can use the ```indexLocale($locale)``` option:

93
``` php
Luca's avatar
Luca committed
94
Translatable::make('Description')->indexLocale('de'),
Luca's avatar
Luca committed
95
96
```