Giter Club home page Giter Club logo

laravel-translatable-bootforms's Introduction

Laravel Translatable BootForms

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

Make BootForms work flawlessly with Laravel Translatable!

By importing this package, generating translatable forms using BootForms is a breeze.

Installation

  1. In the require key of composer.json file add the following

    "propaganistas/laravel-translatable-bootforms": "~1.3"
  2. Run the Composer update command

    composer update
  3. In your app config, add the Service Provider in the $providers array after BootFormsServiceProvider and TranslatableServiceProvider

    'providers' => [
        AdamWathan\BootForms\BootFormsServiceProvider::class,
        Dimsav\Translatable\TranslatableServiceProvider::class,
        ...
        Propaganistas\LaravelTranslatableBootForms\TranslatableBootFormsServiceProvider::class,
    ],
  4. In your app config, add the Facade to the $aliases array

    'aliases' => [
        ...
        'TranslatableBootForm' => Propaganistas\LaravelTranslatableBootForms\Facades\TranslatableBootForm::class,
    ],
  5. Publish the configuration file

    php artisan vendor:publish --provider="Propaganistas\LaravelTranslatableBootForms\TranslatableBootFormsServiceProvider" --tag="config"

Usage

Simply use the TranslatableBootForm Facade as if it were BootForm! That's it. Multiple form inputs will now be generated for the locales set in Translatable's configuration file. They will have the corresponding value for each language and will save all of the translations without any code manipulation.

Please review BootForms' documentation if you're unsure how to use it.

Example:

// View
{!! BootForm::text('Name', 'name')
            ->placeholder('My placeholder') !!}

// Output
<div class="form-group">
    <label for="name">Name</label>
    <input type="text" name="name" class="form-control" placeholder="My Placeholder" />
</div>

// Controller
public function postEdit($request)
{
    $someModel->save($request->all());
}
// View
{!! TranslatableBootForm::text('Name', 'name')
                        ->placeholder('My placeholder') !!}

// Output
<div class="form-group form-group-translation">
    <label for="en[name]">Name (en)</label>
    <input type="text" name="en[name]" class="form-control" placeholder="My Placeholder" data-language="en" />
</div>
<div class="form-group form-group-translation">
    <label for="nl[name]">Name (nl)</label>
    <input type="text" name="nl[name]" class="form-control" placeholder="My Placeholder" data-language="nl" />
</div>

// Controller
public function postEdit($request)
{
    $someModel->save($request->all());
}

You can use the %name and %locale placeholders while specifying parameters. The placeholder will be replaced with the corresponding input name or locale. This can be useful for two-way data binding libraries such as Angular.js or Vue.js. E.g.

{!! TranslatableBootForm::text('Title', 'title')
                        ->attribute('some-attribute', 'Name: %name')
                        ->attribute('another-attribute', 'Locale: %locale') !!}

// Output
<div class="form-group form-group-translation">
    <label for="en[title]">Title (en)</label>
    <input type="text" name="en[title]" class="form-control" some-attribute="Name: en[title]" another-attribute="Locale: en" data-language="en" />
</div>
<div class="form-group form-group-translation">
    <label for="nl[title]">Title (nl)</label>
    <input type="text" name="nl[title]" class="form-control" some-attribute="Name: nl[title]" another-attribute="Locale: nl" data-language="nl" />
</div>

To render a form element only for some chosen locales, explicitly call renderLocale() as the final method and pass the locale or an array of locales as the first parameter:

TranslatableBootForm::text('Name','name')
                    ->renderLocale('en')

If you need to apply a method only for certain locales, suffix the method with ForLocale and pass the locale or an array of locales as the first parameter:

TranslatableBootForm::text('Name','name')
                    ->dataForLocale('en', 'attributeName', 'attributeValue')
                    ->addClassForLocale(['en', 'nl'], 'addedClass')

For customizing the locale indicator in the label (and several other settings), please take a look at the configuration file.

laravel-translatable-bootforms's People

Contributors

propaganistas avatar sdebacker avatar

Watchers

 avatar  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.