Home > PHP Framework > ThinkPHP > body text

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

藏色散人
Release: 2020-06-23 13:52:18
forward
3880 people have browsed it

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
Copy after login

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
Copy after login

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',
Copy after login

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
Copy after login

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');}
Copy after login

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);}
Copy after login

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')
Copy after login

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!

Related labels:
source:csdn.net
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 [email protected]
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!