ThinkPHP 6.0 multi-language function to achieve secondary directory

Foreword

ThinkPHP built-in multi-language capabilities, but it is by judging the URL parameter, Cookies, HTTP_ACCEPT_LANGUAGErequest 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.

  1. Tp6 into the project directory, in the first config\lang.phpset 默认语言and 允许的语言列表. I set up to allow simplified Chinese, traditional Chinese and English.
  2. Set config\route.phpin the routing configuration, forced open the routes url_route_mustand exact match route_complete_matchconflict in order to avoid the setting might cause.
  3. Enter the appdirectory, the new langdirectory, write language packs.
//zh_cn.php
<?php
return [
	'lang' => '简体中文'
];
//zh_tw.php
<?php
return [
	'lang' => '繁體中文'
];
//en_us.php
<?php
return [
	'lang' => 'English'
];
  1. Editing controller\Index.php, Set Index method returns lang language pack above the field, in order to identify the currently selected language test.
  2. Edit route\app.phprouting 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 as http://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');
}
  1. New app\middlewaredirectory, increase LoadLangPack.phpmiddleware.
<?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);
    }
}
  1. In app\middleware.phpadd middleware registration file.
	return [
		\app\middleware\LoadLangPack::class
	];
  1. Try to access. When access to the root directory will be automatically jump to the corresponding language according to the parameters.


  2. Further, as required for url()the function related to the generated URL rewriting helper like. With url()helper function, for example, open app\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/

Published 16 original articles · won praise 1 · views 4690

Guess you like

Origin blog.csdn.net/TLingC/article/details/99430686