Creating a Multi-Language Database System with Laravel 10

LaravelCodeCraft
3 min readSep 3, 2023

--

In this blog post, we’ll look at how to build a multilingual database system in Laravel 10. We’ll design a database structure to store articles in many languages and show how to produce and retrieve articles in certain languages. This lesson assumes you already know the basics of Laravel.

Structure of a Database

To begin, let us create the database structure that will be used for our multi-language system:

Languages Table: This table will hold the languages that are available.

Users Table: For simplicity, we’ll use Laravel’s default users table.

Table of Articles: This table will hold information about the articles.

Table of Article Translations: This table will keep translations for articles in various languages.

Let’s use Laravel migrations to build these tables.

Language Table Migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateLanguagesTable extends Migration
{
public function up()
{
Schema::create('languages', function (Blueprint $table) {
$table->id();
$table->string('code')->unique();
$table->string('name');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('languages');
}
}

Articles Table Migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticlesTable extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->boolean('status')->default(true);
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('articles');
}
}

ArticleTranslations Table Migration

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticleTranslationsTable extends Migration
{
public function up()
{
Schema::create('article_translations', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('article_id');
$table->unsignedBigInteger('lang_id');
$table->string('title');
$table->text('description');
$table->timestamps();

$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->foreign('lang_id')->references('id')->on('languages')->onDelete('cascade');
});
}

public function down()
{
Schema::dropIfExists('article_translations');
}
}

Models and Relationships

We’ll develop Laravel Eloquent models and define relationships between them after the database structure is in place.

Language Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Language extends Model
{
protected $fillable = ['code', 'name'];

public function articleTranslations()
{
return $this->hasMany(ArticleTranslation::class);
}
}

Article Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
protected $fillable = ['status'];

public function translations()
{
return $this->hasMany(ArticleTranslation::class);
}

public function translate($langId)
{
return $this->translations->where('lang_id', $langId)->first();
}
}

ArticleTranslation Model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ArticleTranslation extends Model
{
protected $fillable = ['lang_id', 'title', 'description'];

public function language()
{
return $this->belongsTo(Language::class, 'lang_id');
}
}

Creating and Fetching Articles

Now that we’ve established our models and relationships, we can write articles in different languages and get articles in specified languages.

use App\Models\Article;
use App\Models\Language;

// Create an article with translations
$article = Article::create(['status' => true]);

$english = Language::where('code', 'en')->first();
$spanish = Language::where('code', 'es')->first();

$article->translations()->create([
'lang_id' => $english->id,
'title' => 'English Title',
'description' => 'English Description',
]);

$article->translations()->create([
'lang_id' => $spanish->id,
'title' => 'Spanish Title',
'description' => 'Spanish Description',
]);

// Fetch article in a specific language
$englishArticle = $article->translate($english->id);
$spanishArticle = $article->translate($spanish->id);

echo $englishArticle->title; // Output: English Title
echo $spanishArticle->title; // Output: Spanish Title

We looked at how to establish a multi-language database system in Laravel 10 in this article. We’ve created a database structure for storing articles in several languages and shown how to use Eloquent relationships to produce and retrieve articles in certain languages. This base can be built upon to match the unique needs of your Laravel application.

Using Laravel to build a multi-language system allows you to provide dynamic and accessible information for users who speak different languages. You can effectively handle translations and provide a smooth user experience to a varied audience by harnessing the power of Laravel’s Eloquent ORM and migrations. Feel free to modify and expand on this tutorial to meet the demands of your project and to investigate more internationalisation and localization options in Laravel.

--

--

LaravelCodeCraft

"Exploring the Laravel universe one line of code at a time 🚀 #LaravelCodeCraft"