ThinkPHP6ÖÐÔõÑùʵÏÖ¶àÓïÑÔÍøÕ¾£¿
thinkphp6ÊÇÒ»¿îÓÅÒìµÄphp¿ª·¢¿ò¼Ü£¬ËüÌṩÁ˺ÜÊÇÓÅÒìµÄ´úÂëÖÎÀíºÍ¿ÉÀ©Õ¹ÐÔ¡£ÔÚÏÖʵ¿ª·¢ÖУ¬Ëæ×ÅÈ«Çò»¯µÄÇ÷ÊÆ£¬Ô½À´Ô½¶àµÄÍøÕ¾ÐèÒªÌṩ¶àÓïÑÔÖ§³Ö¡£ÄÇôÔÚthinkphp6ÖУ¬ÔõÑùʵÏÖ¶àÓïÑÔÍøÕ¾ÄØ£¿±¾ÎĽ«´ÓÒÔÏÂËĸö·½Ãæ¾ÙÐнâ˵¡£
Ò»¡¢ÉèÖÃÎļþÖнç˵¶àÓïÑÔ±äÁ¿
ÔÚThinkPHP6ÖУ¬ºÜÊÇÍƼöͨ¹ýÉèÖÃÎļþµÄ·½·¨À´½ç˵¶àÓïÑÔ±äÁ¿¡£Ê×ÏÈÎÒÃÇÐèÒªÔÚconfigĿ¼Ï½¨ÉèÒ»¸ölang.phpÎļþ£¬È»ºóÔÚÆäÖнç˵¶àÓïÑÔÊý×飬ÀýÈ磺
<?php return [ 'welcome' => '½Ó´ý', 'hello' => 'ÄãºÃ', 'bye' => 'ÔÙ¼û', ... ];
µÇ¼ºó¸´ÖÆ
½Ó×ÅÔÚ¿ØÖÆÆ÷ÖÐͨ¹ýlangÖúÊÖº¯ÊýÀ´»ñÈ¡ÕâЩ¶àÓïÑÔ±äÁ¿£¬ÀýÈ磺
echo lang('welcome');
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬µ±ÄãµÄÍøÕ¾ÐèÒªÖ§³Ö²î±ðµÄÓïÑÔʱ£¬Ö»ÐèÒªÐÞ¸Älang.phpÎļþÖжÔÓ¦µÄ¶àÓïÑÔ±äÁ¿Öµ¼´¿É¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢Ê¹ÓÃÖÐÐļþÀ´Æ¾Ö¤ÓïÑÔÉèÖÃSession
ΪÁËÄܹ»Çл»²î±ðµÄÓïÑÔ£¬ÎÒÃÇÐèÒªÔÚÍøÕ¾ÖÐÉèÖÃÒ»¸öÓïÑÔSession¡£Õâ¸öÓïÑÔSession¿ÉÒÔƾ֤ǰ̨´«À´µÄ²ÎÊý»òÕßä¯ÀÀÆ÷ÉèÖÃÀ´¾ÙÐÐ×Ô¶¯Ê¶±ð¡£
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÖÐÐļþÀ´ÊµÏÖÕâ¸ö¹¦Ð§¡£ÔÚapp/middlewareĿ¼Ï½¨ÉèÒ»¸öLanguage.phpµÄÖÐÐļþÎļþ£¬´úÂëÈçÏ£º
<?php namespace appmiddleware; use thinkacadeSession; use thinkRequest; class Language { public function handle(Request $request, Closure $next) { $lang = $request->param('lang'); if(!in_array($lang, ['zh-cn', 'en-us'])){ $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; } Session::set('lang', $lang); return $next($request); } }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬ÅжÏת´ïµÄÓïÑÔ²ÎÊýÊÇ·ñÕýµ±£¬ÈôÊDz»¶Ô¹æÔòʹÓÃä¯ÀÀÆ÷ÉèÖÃÖеÄÓïÑÔ²ÎÊý¡£
ÔÚapp/middleware.phpÖÐÒýÈë¸ÃÖÐÐļþ£¬²¢ÔÚÐèÒªÖ§³Ö¶àÓïÑԵĿØÖÆÆ÷ÖÐʹÓã¬ÀýÈ磺
<?php namespace appcontroller; use thinkacadeSession; class Index { public function index() { $lang = Session::get('lang'); return lang('welcome'); } }
µÇ¼ºó¸´ÖÆ
Èý¡¢Ê¹ÓöàÓïÑÔ·ÓÉ
Õë¶ÔһЩÐèÒªÖ§³Ö¶àÓïÑÔ·ÓɵÄÐèÇó£¬ThinkPHP6ÖÐÌṩÁ˺ÜÊÇÀû±ãµÄÒªÁì¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½ç˵ÒÔÏÂÁ½¸ö·ÓÉ£º
Route::get(':lang/index', 'index/index'); Route::get(':lang/about', 'index/about');
µÇ¼ºó¸´ÖÆ
ÕâÑùÎÒÃÇ»á¼ûhttps://example.com/zh-cn/indexºÍhttps://example.com/zh-cn/aboutʱ¾Í»á½øÈë¶ÔÓ¦µÄ¿ØÖÆÆ÷ÖУ¬²¢ÇÒǰ̨¾Í²»ÐèÒªµ¥¶Àת´ïÓïÑÔ²ÎÊýÁË¡£
ËÄ¡¢Ê¹ÓÃÄ£°å±êÇ©À´Êä³ö¶àÓïÑÔÄÚÈÝ
×îºó£¬ÎÒÃÇÐèÒªÔÚǰ̨¶Ô¶àÓïÑÔ±äÁ¿¾ÙÐÐÊä³ö¡£ÕâʱÎÒÃDZã¿ÉÒÔʹÓÃThinkPHP6ÌṩµÄ¡°Ä£°å±êÇ©¡±¹¦Ð§£¬ÀýÈ磺
{: lang('welcome') }
µÇ¼ºó¸´ÖÆ
ËäÈ»£¬¸üÍƼöʹÓÃÏÂÃæÕâÖÖ·½·¨£º
{lang name="welcome"}
µÇ¼ºó¸´ÖÆ
ÕâÑù¼ÈÄܹ»Êä³ö¶ÔÓ¦µÄ¶àÓïÑÔ±äÁ¿£¬Ò²Äܹ»Ìí¼ÓһЩĬÈÏÖµºÍ²ÎÊý¡£
×ܽá
ÒÔÉϾÍÊÇÔÚThinkPHP6ÖÐʵÏÖ¶àÓïÑÔÍøÕ¾µÄÒªÁ졣ͨ¹ý½ç˵¶àÓïÑÔ±äÁ¿¡¢Ê¹ÓÃÖÐÐļþÀ´ÉèÖÃSession¡¢Ê¹ÓöàÓïÑÔ·ÓɺÍÄ£°å±êÇ©¶Ô¶àÓïÑÔÄÚÈݾÙÐÐÊä³ö£¬ÎÒÃǾͿÉÒÔÇáËɵؽ¨ÉèÒ»¸öÍêÕûµÄ¶àÓïÑÔÍøÕ¾¡£ËäÈ»£¬ÔÚÏÖʵ¿ª·¢Öл¹ÐèҪ˼Á¿Ò»Ð©Ï¸½ÚÎÊÌ⣬µ«ÒÔÉÏÒªÁìΪThinkPHP6ÖоÙÐжàÓïÑÔ¿ª·¢ÌṩÁ˺ÜÊǺõĻù´¡¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʵÏÖ¶àÓïÑÔÍøÕ¾£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡