Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 828
Maciej Swic Avatar asked Sep 30 '13 07:09

Maciej Swic


4 Answers

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();
like image 199
Glad To Help Avatar answered Nov 03 '22 07:11

Glad To Help


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!

like image 43
adrianthedev Avatar answered Nov 03 '22 09:11

adrianthedev


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');

like image 40
Alex Avatar answered Nov 03 '22 07:11

Alex


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

like image 22
Buraco Avatar answered Nov 03 '22 09:11

Buraco