Giter Club home page Giter Club logo

laravel-db-localization's Introduction

Laravel DB Localization for version 5.1

Note: if you are looking for the version for Laravel 4.2 check out v1 branch.

Installation

Open composer.json file of your project and add the following to the require array:

"despark/laravel-db-localization": "2.0.*"

Now run composer update to install the new requirement.

Once it's installed, you need to register the service provider in config/app.php in the providers array:

'providers' => array(
  ...
  Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider::class,
);

Publish config file: php artisan vendor:publish --provider="Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider" --tag="config"

Publish migrations: php artisan vendor:publish --provider="Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider" --tag="migrations"

How to use it

Database Example

  • First you need to create your languages table
Schema::create('i18n', function (Blueprint $table) {
        $table->increments('id');
        $table->string('locale')->unique()->index();
        $table->string('name')->index();
        $table->timestamps();
});
  • Example of translatable table
Schema::create('contacts', function (Blueprint $table) {
        $table->increments('id');

        // untranslatable columns
        $table->string('fax');
        $table->string('phone');
        $table->timestamps();
});
  • Example of translations table
Schema::create('contacts_i18n', function (Blueprint $table) {

        $table->integer('contact_id')->unsigned();
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
        $table->integer('i18n_id')->unsigned();
        $table->foreign('i18n_id')->references('id')->on('i18n')->onDelete('cascade');

        // translatable columns
        $table->string('name', 100);
        $table->string('location', 100);

        $table->unique(['contact_id', 'i18n_id']);
        $table->primary(['contact_id', 'i18n_id']);
        $table->timestamps();
});

Model Example

use Despark\LaravelDbLocalization\i18nModelTrait;

class Contacts extends Eloquent
{
    use i18nModelTrait; // You must use i18nModelTrait

    protected $fillable = [
        'fax',
        'phone',
    ];

    protected $translator = 'Despark\LaravelDbLocalization\ContactsI18n'; // Here you need to add your translations table model name

    protected $translatorField = 'contact_id'; // your translator field name

    protected $localeField = 'i18n_id'; // here is your locale field name

    protected $translatedAttributes = ['contact_id', 'i18n_id', 'name', 'location']; // translatable fillables
}

class ContactsI18n extends Eloquent
{
    protected $table = 'contacts_i18n';
}

View example

Create

{!! Form::text("fax", null) !!}
{!! Form::text("phone", null) !!}

@foreach($languages as $language)
    {!! Form::text("name[name_$language->id]", null) !!}  // Follow this convention array( fieldname_languageId );
    {!! Form::text("location[location_$language->id]", null) !!}
@endforeach

Retrieve

    // locale string
    $contacts->translate('en'); // all fields
    $contacts->translate('en')->location; // specific field

    // locale id
    $i18nId = 2;
    $contacts->translate($i18nId); // all fields
    $contacts->translate($i18nId)->location; // specific field

Config Example

config/laravel-db-localization.php
    'locale_class' => 'Despark\LaravelDbLocalization\I18n', // Eloquent model that handles your languages.

laravel-db-localization's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-db-localization's Issues

[Convenience Issue 2.0.1] Trying to access attribute of object null

Hi !
First of all, thank you for this awesome package. Such an obvious need and no official solution from Laravel :D .

Second of all, I added some convenience code so the Trait doesn't generate unneeded exceptions, but I didn't know if it was enough to create a pull request :D .

In File src/Despark/LaravelDbLocalization/i18nModelTrait.php line 105

This block
if (isset($this->id) && $locale) {
        $translation = $translationModel::where($this->translatorField, $this->id)
            ->where($this->localeField, $locale)->first();
}
Should look more like this
if (isset($this->id) && $locale) {
       $translation = $translationModel::where($this->translatorField, $this->id)
           ->where($this->localeField, $locale)->first();

       if(!$translation)
           $translation = $translationModel;
}

Otherwise when people do something like $contacts->translate('en')->location; and the english translation is not available, this bit $contacts->translate('en') would return null because of the line 98 ($translation = null;) so the code would trigger an exception for trying to access attribute location of null object. The solution is simply to return an empty translator object instead.

I also added this function :
    public function getTranslatedNoIdAttributes()
    {
        $array = [];
        foreach($this->translatedAttributes as $attr) {
            if(strpos($attr, '_id') === false) {
                array_push($array, $attr);
            }
        }
        return $array;
    }

It returns the translatedAttributes without the model_id and the 18n_id so people can do some convenient but not so fancy stuff like this :

    {!! Form::open(['model' => $contact, 'store' => 'admin.contacts.store', 
         'update' => 'admin.contacts.update']) !!}

        @foreach($contact->getTranslatedNoIdAttributes() as $trans_attr)
            @foreach($languages as $language)
                {!! Form::text($trans_attr.'['.$trans_attr.'_'.$language->id.']', 
                    ucfirst($trans_attr) . ' ('.$language->name.')', 
                    $contact->translate($language->id)->{$trans_attr}) !!}
            @endforeach
        @endforeach
...

This removes the need to update every form.blade.php everytime you add a language or a translatable field. Which was my need in the first place :p . Hope you include this to make the 2.0.2 . I would gladly update to that version !

Thank you again !

vendor publish error

Hi, I'm using Laravel 5.2 and I can't run the php artisan vendor:publish command. It keeps saying:
"Class 'Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider' not found". I'm quite a newbie with programming, so the error should be the way I installed it.
Anyway, can you help me ?

vendor

Thanks in advance.

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.