Foreword
ThinkPHP built-in multi-language capabilities, but it is by judging the URL parameter, Cookies, HTTP_ACCEPT_LANGUAGE
request parameter to return to the language pack, due to the multilingual URL fixed, when the search engines crawl the page, the page is returned depends on which language reptile brought the request header, this method has a certain impact on SEO, it is difficult to comprehensive collection of various languages. For example Google webmaster support of " managing multi-regional and multilingual sites ," one article mentioned:
Google recommends using different URLs for each language version of the page, instead of using the Cookie or browser settings to adjust language content on the page.
If you want to dynamically change the content or re-routing based on user language setting, note, Google may not be able to find and crawl all your variants. This is because, Googlebot crawlers usually come from the United States. Further, when the crawler and sends an HTTP request is not provided in the request Accept-Language header.
achieve
The sample project can be downloaded at the bottom of the article.
- Tp6 into the project directory, in the first
config\lang.php
set默认语言
and允许的语言列表
. I set up to allow simplified Chinese, traditional Chinese and English.
- Set
config\route.php
in the routing configuration, forced open the routesurl_route_must
and exact matchroute_complete_match
conflict in order to avoid the setting might cause. - Enter the
app
directory, the newlang
directory, write language packs.
//zh_cn.php
<?php
return [
'lang' => '简体中文'
];
//zh_tw.php
<?php
return [
'lang' => '繁體中文'
];
//en_us.php
<?php
return [
'lang' => 'English'
];
- Editing
controller\Index.php
, Set Index method returns lang language pack above the field, in order to identify the currently selected language test.
- Edit
route\app.php
routing configuration, replaced with the following. Route subsequently added below :: get routing method can be referred to.
There is no use to route packets function, to avoid access the root directory (such ashttp://localhost/zh-cn/
time) prompted the route is not correct, but do not add a slash at the end of the problem but also match (http://localhost/zh-cn
).
use think\facade\Route;
use think\facade\Config;
Route::get('/', 'index/index');
$langs = Config::get('lang.allow_lang_list');
foreach($langs as $lang){
Route::get($lang . '/', 'index/index');
}
- New
app\middleware
directory, increaseLoadLangPack.php
middleware.
<?php
declare (strict_types = 1);
namespace app\middleware;
use Closure;
use think\App;
use think\Lang;
use think\Request;
class LoadLangPack
{
public function handle($request, Closure $next, Lang $lang, App $app)
{
$path = explode('/', $request->pathinfo());
if(sizeof($path) > 0){
if(empty($path[0])){
$langset = $lang->detect();
return redirect('/' . $langset . '/');
}else{
$langset = $path[0];
}
$lang->setLangSet($langset);
$lang->load([
$app->getThinkPath() . 'lang' . DIRECTORY_SEPARATOR . $langset . '.php',
]);
$app->LoadLangPack($langset);
}
return $next($request);
}
}
- In
app\middleware.php
add middleware registration file.
return [
\app\middleware\LoadLangPack::class
];
- Try to access. When access to the root directory will be automatically jump to the corresponding language according to the parameters.
- Further, as required for
url()
the function related to the generated URL rewriting helper like. Withurl()
helper function, for example, openapp\common.php
, write the following code:
use think\facade\Lang;
use think\facade\Route;
use think\route\Url as UrlBuild;
function url(string $url = '', array $vars = [], $suffix = true, $domain = false): UrlBuild
{
$lang = Lang::getLangSet();
return Route::buildUrl('/' . $lang . '/' . $url, $vars)->suffix($suffix)->domain($domain);
}
So when you call the url function will automatically add the language path. In addition to the need to route the language to use to write full time.
download
https://github.com/TLingC/thinkphp6.0-multilingual-example
Original: https://tlingc.com/2019/08/implementing-secondary-directory-multilingual-function-in-thinkphp-6-0/