Coder Social home page Coder Social logo

nova-translatable's Introduction

Nova Translatable Field

Latest Version on Packagist

Adds the ability to show and edit translated fields created with spatie/laravel-translatable package.

It will show up in the detail view like this:

And in the edit view like this:

Installation and usage

You can require this package using composer:

composer require mrmonat/nova-translatable

You can add the field follows:

use MrMonat\Translatable\Translatable;

Translatable::make('Description'),

Make sure, that you have your Eloquent model setup correct:

  • First, you need to add the Spatie\Translatable\HasTranslations-trait.
  • Next, you should create a public property $translatable which holds an array with all the names of attributes you wish to make translatable.
  • 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:

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class NewsItem extends Model
{
    use HasTranslations;
    
    public $translatable = ['name'];
}

Defining Locales

Locales can be defined via config file config/translatable.php (config file can be created via spatie/laravel-translatable package) by adding a locales array:

// config/translatable.php
return [
    ...
    'locales' => [
        'en' => 'English',
        'de' => 'German',
        'fr' => 'French',
    ],
];

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

Translatable::make('Description')->locales([
    'en' => 'English',
    'de' => 'German',
]),

Single Line Option

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:

Translatable::make('Description')->locales([...])->singleLine(),

Trix Editor

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

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:

Translatable::make('Description')->indexLocale('de'),

nova-translatable's People

Contributors

mrmonat avatar ronmelkhior avatar andypa avatar benjamincrozat avatar jimmitjoo avatar royduin avatar d8vjork avatar vmitchell85 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.