Home >PHP Framework >ThinkPHP >Installation and use of ThinkPHP 6.0 multi-language optimization extension package
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-langFeatures
.
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',Since multiple languages are accessed through the secondary directory,
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\当前语言\*\*.phpPlease 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 settingsUsing 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
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);}Compared with the official helper function,
$lang and
$replace parameters are added.
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.
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!