Home > PHP Framework > Laravel > body text

Discuss how Laravel implements database multi-language

PHPz
Release: 2023-04-10 15:18:52
Original
934 people have browsed it

Laravel is a popular PHP web application framework that provides a set of powerful, elegant and simple syntax, making the development and maintenance of web applications easier and more enjoyable. Laravel provides many useful features, one of which is built-in support for the development of multi-language applications. In this article, we will explore how Laravel implements database polyglots.

  1. Internationalization (i18n) and localization (l10n) in Laravel

In Laravel, internationalization and localization are two basic concepts. Internationalization refers to designing an application to support multiple languages ​​and cultural practices, while localization refers to adapting an application to a specific place. Laravel has some built-in tools and classes that can help us implement i18n and l10n.

  1. Laravel multi-language implementation methods

There are many ways to implement Laravel multi-language applications. We will introduce one of the implementation methods, which is to use a database to implement multiple languages. language.

First, create a table in the database to store the multiple languages ​​supported by the system, for example:

CREATE TABLE `languages` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `code` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
  `is_default` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Copy after login

In this table, the name field stores the language name, and the code field stores the language. Code (such as en, zh, etc.), the is_default field specifies the default language.

Then, add multiple fields to the table that requires multi-language support, corresponding to text in different languages. For example:

CREATE TABLE `products` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name_en` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name_zh` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description_en` text COLLATE utf8mb4_unicode_ci,
  `description_zh` text COLLATE utf8mb4_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Copy after login

In this table, the name_en and name_zh fields store English and Chinese product name, description_en and description_zh fields store English and Chinese product descriptions respectively.

Next, we need to make some changes to these tables to support multiple languages:

  1. In the app/Http/Kernel.php file, register a middleware with To dynamically set the currently used language:
protected $middleware = [
    ...
    \App\Http\Middleware\SetLanguage::class,
];
Copy after login
  1. Create a SetLanguage middleware:
<?php

namespace App\Http\Middleware;

use Closure;
use App;

class SetLanguage
{
    public function handle($request, Closure $next)
    {
        $language = $request->get('lang', '');
        if ($language != '') {
            App::setLocale($language);
        }
        return $next($request);
    }
}
Copy after login

The function of this middleware is to obtain the lang parameter from the request parameter , set the currently used language.

  1. Create a Language model:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Language extends Model
{
    protected $table = &#39;languages&#39;;
    public $timestamps = false;

    public static function getDefault()
    {
        return self::where(&#39;is_default&#39;, true)->first();
    }

    public static function getCurrent()
    {
        $code = app()->getLocale();
        return self::where('code', $code)->first();
    }
}
Copy after login

This model is used to obtain the currently used language information, default language information, etc.

  1. Create a getLocalizedAttribute method in the Model:
public function getLocalizedAttribute($value)
{
    $lang = Language::getCurrent();
    $field = $this->getLocalizedFieldName($lang->code);
    return $this->$field;
}

protected function getLocalizedFieldName($langCode)
{
    $fallback = $this->getDefaultFieldName();
    return "{$this->$fallback}_{$langCode}";
}

protected function getDefaultFieldName()
{
    return 'name_en';
}
Copy after login

This method is used to obtain the field corresponding to the currently used language, for example: if the current language is Chinese, and This model corresponds to the English product name, and the Chinese field corresponding to the product name is returned.

  1. In the Blade template file, you can use multi-language strings like this:
<h1>{{ $product->localized }}</h1>
Copy after login

This statement will automatically obtain the product name in the corresponding language based on the currently used language.

So far, we have completed the multi-language implementation of Laravel database and can easily create multi-language web applications.

The above is the detailed content of Discuss how Laravel implements database multi-language. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!