Home >PHP Framework >ThinkPHP >Installation and use of ThinkPHP 6.0 multi-language optimization extension package

Installation and use of ThinkPHP 6.0 multi-language optimization extension package

藏色散人
藏色散人forward
2020-06-23 13:50:364189browse

The following is the thinkphp framework tutorial column to introduce you to the ThinkPHP 6.0 multi-language optimization expansion package. I hope it will be helpful to friends in need!

Installation and use of ThinkPHP 6.0 multi-language optimization extension package

##think-lang

ThinkPHP 6.0 Multi-language Optimization Extension Pack

https://github.com/TLingC/think-lang

Features

    Supports accessing corresponding language pages through secondary directories such as
  1. mywebsite.com/zh-hans/ .
  2. The language packs supported for each language are separated into separate directories, and there can be a secondary directory under the directory.
Installation

composer require tlingc/think-lang

Use

Basic configuration

Please refer to the official documentation (https://www.kancloud.cn/manual/thinkphp6_0 /1037637) Perform related configurations.

Also note the following differences from official documents.

Open and load language pack

The middleware name is:

'tlingc\lang\middleware\LoadLangPack',
Since multiple languages ​​are accessed through the secondary directory,

Use Cookie to save the language Function is invalid.

Language file definition

Auto-loaded application language file:

// 单应用模式app\lang\当前语言.php
app\lang\当前语言\*.php
app\lang\当前语言\*\*.php// 多应用模式app\应用\lang\当前语言.php
app\应用\lang\当前语言\*.php
app\应用\lang\当前语言\*\*.php
Please note that this expansion pack does not modify the language pack parsing behavior, and the file name of the language file will not Affects language grouping, causing overwriting when the same definition exists in multiple files.

Routing settings

Using the secondary directory to access the corresponding language page must use routing definitions, and it is recommended to turn on the

forced routing mode.

use think\facade\Config;Route::view('/', 'index/index');$langs = Config::get('lang.allow_lang_list');foreach($langs as $lang){
	Route::rule($lang . '/', 'index/index');
	Route::rule($lang . '/welcome', 'index/welcome');}
Rewrite

url The helper function

is added to the application public file

common.php.

use think\facade\Request;use think\facade\Lang;use think\facade\Route;use think\route\Url as UrlBuild;function url(string $url = '', array $vars = [], $suffix = true, $domain = false, $lang = true, $replace = false): UrlBuild{
    if (!$lang) {
		if($replace) {
			$explode = explode('/', Request::url(), 3);
			$url = $url . $explode[2];
		}
        return Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
	}
	$lang = Lang::getLangSet();
	return Route::buildUrl('/' . $lang . $url, $vars)->suffix($suffix)->domain($domain);}
Compared with the official helper function,

$lang and $replace parameters are added.

Normal jump, the preceding language name will be automatically included when generating the URL.

url('/welcome')
If you need to replace only the language name in the URL (such as when used in a language selector), set the

$replace parameter to true.

TODO

  • Integrate route definition method.
  • Integrate and rewrite url parameters.

The above is the detailed content of Installation and use of ThinkPHP 6.0 multi-language optimization extension package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete