ÔõÑùʹÓÃThinkPHP6¾ÙÐжàÓïÑÔ·Òë²Ù×÷£¿
Ëæ×ÅÈ«Çò»¯µÄÉú³¤£¬Ô½À´Ô½¶àµÄÍøÕ¾ºÍÓ¦ÓóÌÐòÐèÒªÌṩ¶àÓïÑÔÖ§³Ö¡£¶ø¹ØÓÚʹÓÃthinkphp6¿ò¼ÜµÄ¿ª·¢ÕßÀ´Ëµ£¬ÔõÑùʵÏÖ¶àÓïÑÔ·Òë²Ù×÷ÊÇÒ»¸öÖ÷ÒªµÄÐèÇó¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp6¾ÙÐжàÓïÑÔ·Òë²Ù×÷¡£
ÉèÖÃÓïÑÔ°ü
ÔÚThinkPHP6ÖУ¬ÓïÑÔ°üÊÇÒ»¸ö°üÀ¨¼üÖµ¶ÔµÄÊý×é¡£¿ÉÒÔ½«Æä´æ´¢ÔÚapp/lang/Ŀ¼ÏµÄÖÖÖÖ×ÓĿ¼ÖС£ÀýÈ磺
/app/lang/zh-cn/ /app/lang/en-us/
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬zh-cnºÍen-usÊÇÓïÑÔ°üµÄÃû³Æ£¬ËüÃǶÔÓ¦µÄĿ¼Ï¾ÍÓ¦¸Ã´æ·Å¸ÃÓïÑÔ°æ±¾µÄ·Òë¡£ÔÚÓïÑÔ°üĿ¼Ï£¬Í¨³£»áÓÐÒ»¸öapp.php»òÕßvalidation.phpÎļþ£¬ËüÃÇ»®·Ö¶ÔÓ¦Ó¦ÓóÌÐòºÍ±íµ¥ÑéÖ¤µÄ·Òë¡£
ÀýÈ磺
/app/lang/zh-cn/app.php /app/lang/en-us/app.php
µÇ¼ºó¸´ÖÆ
ÔÚÕâÁ½¸öÎļþÖУ¬»á½ç˵һЩ¼üÖµ¶Ô£¬ÓÃÀ´·ÒëÓ¦ÓóÌÐòÖеÄÎÄ×Ö¡£ÀýÈ磬һ¸ö¼òÆÓµÄapp.phpÎļþ¿ÉÄܳ¤ÕâÑù£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<?php return [ 'welcome' => '½Ó´ý', 'login' => 'µÇ¼', 'logout' => 'Í˳öµÇ¼', ];
µÇ¼ºó¸´ÖÆ
ÕâÀï½ç˵ÁËÈý¸ö¼üÖµ¶Ô£¬ËüÃÇÓÃÀ´·ÒëÓ¦ÓóÌÐòÖеġ°½Ó´ý¡±¡¢¡°µÇ¼¡±ºÍ¡°Í˳öµÇ¼¡±µÄÎÄ×Ö¡£¹ØÓÚÓ¢ÎÄ°æ±¾£¬¿ÉÒÔ½¨ÉèÒ»¸öen-us/app.phpÎļþ£¬½«ÉÏÃæµÄÖÐÎÄ·ÒëÌæ»»³ÉÓ¢ÎÄ·Ò룺
<?php return [ 'welcome' => 'Welcome', 'login' => 'Log in', 'logout' => 'Log out', ];
µÇ¼ºó¸´ÖÆ
¶ÁÈ¡ÓïÑÔ°ü
ÔÚÓ¦ÓóÌÐòÖУ¬Í¨³£ÐèҪƾ֤Óû§µÄÓïÑÔÉèÖÃÀ´¶ÁÈ¡ÏìÓ¦µÄÓïÑÔ°ü¡£¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´»ñÈ¡Ä¿½ñÓïÑÔÉèÖãº
$lang = $request->lang();
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁË$request¹¤¾ßµÄlang()ÒªÁ죬¸ÃÒªÁì¿ÉÒÔ»ñÈ¡ÇëÇ󹤾ßÖеÄlang²ÎÊý¡£Í¨³£Õâ¸ö²ÎÊý»á´æ´¢Óû§µÄÓïÑÔÉèÖã¬ÀýÈ磺
http://example.com/?lang=zh-cn http://example.com/?lang=en-us
µÇ¼ºó¸´ÖÆ
ÕâÀïÈôÊÇlang²ÎÊýûÓÐÖ¸¶¨£¬¿ÉÒÔͨ¹ý¶ÁÈ¡ä¯ÀÀÆ÷µÄAccept-LanguageÍ·À´ÍƲâÓû§µÄÓïÑÔÉèÖá£
½ÓÏÂÀ´£¬¿ÉÒÔʹÓÃlang()º¯ÊýÀ´¶ÁÈ¡Ä¿½ñÓïÑÔ°ü£¬ÀýÈ磺
$lang = $request->lang(); app()->setLocale($lang); $translations = lang('app');
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁËapp()º¯ÊýÀ´»ñÈ¡Ó¦ÓóÌÐò¹¤¾ß£¬È»ºóͨ¹ýËüµÄsetLocale()ÒªÁìÀ´ÉèÖÃÄ¿½ñµÄÓïÑÔÇéÐΡ£×îºó£¬Ê¹ÓÃlang()º¯ÊýÀ´¶ÁÈ¡ÓïÑÔ°ü£¬Ëü·µ»ØÒ»¸öÊý×飬ÆäÖаüÀ¨ÁËÄ¿½ñÓïÑÔÇéÐÎϵÄËùÓзÒë¡£
ʹÓ÷Òë
ÓÐÁËÓïÑÔ°üºÍ·ÒëÊý×飬¾Í¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐʹÓ÷ÒëÁË¡£ÀýÈ磬ÔÚÄ£°åÖпÉÒÔʹÓñêÇ©À´»ñÈ¡·Ò룺
<p>{{ __('app.welcome') }}</p>
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁË__º¯ÊýÀ´»ñÈ¡·Òë¡£__º¯Êý»á½«´«ÈëµÄ×Ö·û´®Æ¾Ö¤µã·Ö·¨ÆÊÎö³ÉÒ»¸öÊý×飬Ȼºó´Ó·ÒëÊý×éÖвéÕÒÏìÓ¦µÄ·Òë¡£
ÈôÊÇûÓÐÕÒµ½¶ÔÓ¦µÄ·Ò룬__º¯Êý»á·µ»ØÔʼ×Ö·û´®¡£Òò´Ë£¬¿ÉÒÔ½«Ó¢ÎÄ×Ö·û´®×÷ΪĬÈÏÖµ´«Èë__º¯Êý£¬ÀýÈ磺
<p>{{ __('app.welcome', ['default' => 'Welcome']) }}</p>
µÇ¼ºó¸´ÖÆ
ÕâÀïÖ¸¶¨ÁËÒ»¸ödefault²ÎÊý£¬ËüµÄÖµÊÇ’Welcome’¡£ÈôÊÇÔÚ·ÒëÊý×éÖÐûÓÐÕÒµ½’welcome’¼ü¶ÔÓ¦µÄ·Ò룬__º¯Êý¾Í»á·µ»ØÕâ¸öĬÈÏÖµ¡£
Ìí¼Ó×Ô½ç˵·Òë
ÓÐʱ¼äÐèÒªÌí¼ÓһЩ×Ô½ç˵µÄ·Ò룬ÀýÈç±íµ¥ÑéÖ¤µÄ¹ýʧÐÅÏ¢¡£¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´Ìí¼Ó×Ô½ç˵·Ò룺
use thinkacadeLang; Lang::load([ 'validation.custom' => [ 'email' => [ 'required' => 'ÇëÌîдÓÊÏäµØµã', 'email' => 'ÇëÊäÈëÓÐÓõÄÓÊÏäµØµã', ], ], ]);
µÇ¼ºó¸´ÖÆ
ÕâÀïʹÓÃÁËLangÀàµÄload()ÒªÁìÀ´Ìí¼Ó×Ô½ç˵·Òë¡£load()ÒªÁì½ÓÊÜÒ»¸öÊý×é×÷Ϊ²ÎÊý£¬¸ÃÊý×éƾ֤ÓïÑÔ°üµÄ½á¹¹×éÖ¯£¬ÓÃÀ´Ìí¼Ó×Ô½ç˵µÄ·Òë¡£
ÔÚÉÏÃæµÄÀý×ÓÖУ¬Ìí¼ÓÁËÒ»¸övalidation.customÓïÑÔ°ü£¬Ëü°üÀ¨ÁË’email’¼üµÄ×Ô½ç˵·Òë¡£ÕâЩ·Òë»áÁýÕÖϵͳĬÈϵķÒë¡£
×ܽá
ʹÓÃThinkPHP6¾ÙÐжàÓïÑÔ·Òë²Ù×÷ºÜÊÇÀû±ã¡£Ö»ÐèҪ׼±¸ºÃÓïÑÔ°ü£¬È»ºó¶ÁÈ¡ÓïÑÔ°ü²¢Ê¹Ó÷Òë¼´¿É¡£ÈôÊÇÐèÒªÌí¼Ó×Ô½ç˵·Ò룬ҲºÜÊǼòÆÓ¡£¶àÓïÑÔÖ§³Ö²»µ«ÄÜÌá¸ßÓ¦ÓóÌÐòµÄ¿ÉÓÃÐÔ£¬»¹ÄܸüºÃµØÖª×ãÓû§µÄÐèÇó£¬ÔöÌíÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐжàÓïÑÔ·Òë²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡