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.
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.
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;
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;
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:
protected $middleware = [ ... \App\Http\Middleware\SetLanguage::class, ];
<?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); } }
The function of this middleware is to obtain the lang parameter from the request parameter , set the currently used language.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Language extends Model { protected $table = 'languages'; public $timestamps = false; public static function getDefault() { return self::where('is_default', true)->first(); } public static function getCurrent() { $code = app()->getLocale(); return self::where('code', $code)->first(); } }
This model is used to obtain the currently used language information, default language information, etc.
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'; }
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.
<h1>{{ $product->localized }}</h1>
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!