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!
https://github.com/TLingC/think-lang
.
composer require tlingc/think-lang
Also note the following differences from official documents.
Open and load language packThe middleware name is:'tlingc\lang\middleware\LoadLangPack',
Use Cookie to save the language Function is invalid.
// 单应用模式app\lang\当前语言.php app\lang\当前语言\*.php app\lang\当前语言\*\*.php// 多应用模式app\应用\lang\当前语言.php app\应用\lang\当前语言\*.php app\应用\lang\当前语言\*\*.php
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');}
The helper function
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);}
$lang and
$replace parameters are added.
url('/welcome')
$replace parameter to
true.
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!