Maciej Swic
Maciej Swic

Reputation: 11359

Multilanguage database management with Laravel

I'm creating an app with a backend in Laravel. The backend needs to manage a collection of objects which are downloaded to the app. The objects must be localised depending on the device language.

Is there a simple way to do this in Laravel? Perhaps an eloquent or Laravel-plugin? I'd like to avoid writing the localisation support myself.

(The built in localisation in Laravel is only for the interface, it doesn't support Eloquent objects)

Upvotes: 10

Views: 19185

Answers (5)

Buraco
Buraco

Reputation: 461

There are some translation packages for Eloquent models and static language resources. You can combine them, it's up to your scenario.

These packages might be useful when you just want to translate your resources or outsource translation to 3rd parties (like customer or content creator) via extranet (kinda front-end), so these are storing your translation files in database:
https://github.com/barryvdh/laravel-translation-manager
https://github.com/joedixon/laravel-translation

In order to make your Eloquent model multilanguage, store it as JSON array. If you are creating a sort of CMS like application, you will need multilingual title or content body. Following packages might help to achive this:
https://github.com/Astrotomic/laravel-translatable
https://github.com/spatie/laravel-translatable

Upvotes: 2

Alex
Alex

Reputation: 85

I did similar but more universal

Schema::create('index', function(Blueprint $table) {
        $table->increments('id');
        $table->string('title_uk');
        $table->string('title_ru');
        $table->string('title_en');
        $table->string('heading_uk');
        $table->string('heading_ru');
        $table->string('heading_en');
        $table->string('photo');
        $table->timestamps();
    });

The model

public function TextTrans($text)
{
    $locale=App::getLocale();
    $column=$text.'_'.$locale;

    return $this->{$column};
}

Now I for each language version as well as for each field will not prescribe a specific function, and cause all this:

$text=Index::find('1'); $text->TextTrans('title'); $text->TextTrans('heading');

Upvotes: 7

Marlon Marcello
Marlon Marcello

Reputation: 23

Following the @user1067281 answer I found a great advanced tutorial for multi languages site.

You should totally check this out: https://medium.com/laravel-4/26cdc75e4810

Upvotes: 1

adrianthedev
adrianthedev

Reputation: 646

Glad To Help's answer seems perfect for multilingual site's with many languages. I've found out that it's kind of clunky to do that when your site has just two/three languages.

What I've done is having two columns for each translatable fields. for the title attribute I have in the database title_en and title_es. After that, in the controller just set this method to do an automated translation.

public function getTitleAttribute()
{
    $locale = App::getLocale();
    $column = "title_" . $locale;
    return $this->{$column};
}

Now when you call Post::find($id)->title it will automatically get the one for the current language.

Hope it helps. Cheers!

Upvotes: 8

Glad To Help
Glad To Help

Reputation: 5387

You will need to write that on your own. First you will have to model your database tables to support multilanguage content and then in your model you will be able to say something like:

class Content extends Eloquent
{

   public function scopeEnglish($query)
   {
          return $query->where('language', '=', 'en');
   }

   public function scopeSpanish($query)
   {
      return $query->where('language', '=', 'es');
   }
}


class Post extends Eloquent
{
  public function content()
  { 
     return $this->hasMany('Content'); 
  }
}

and then you can use it like:

$englishContent = Posts::find($id)->content->english()->get();
$spanishContent = Posts::find($id)->content->spanish()->get();

Upvotes: 10

Related Questions