laravelÔõô½«Ó¢ÎÄ·ûºÅתÖÐÎÄ·ûºÅ
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬ÈËÃǵĽ»Á÷·½·¨Ò²Ô½À´Ô½¶àÔª»¯¡£ÔÚʹÓõç×ÓÓʼþ¡¢Î¢ÐÅ¡¢¶ÌÐŵȹ¤¾ß¾ÙÐÐÏàͬʱ£¬³£³£»á·ºÆðÓ¢ÎÄ·ûºÅ£¬Èç ¡°@¡±¡¢¡°#¡±¡¢¡°&¡±µÈ¡£È»¶ø£¬ÔÚʹÓÃÖÐÎľÙÐн»Á÷ʱ£¬Ó¢ÎÄ·ûºÅÍùÍù»á¸øÈË´øÀ´À§ÈÅ¡£ÌØÊâÊÇÔÚʹÓÃlaravelÕâÑùµÄ¿ª·¢¿ò¼Ü¾ÙÐбà³Ìʱ£¬Ó¢ÎÄ·ûºÅµÄ·ºÆð»áµ¼Ö´úÂëÄÑÒÔÔĶÁºÍÃ÷È·¡£Îª´Ë£¬laravelÌṩÁËÒ»ÖֺܺõĽâ¾ö¼Æ»®¡ª¡ªÓ¢ÎÄ·ûºÅתÖÐÎÄ·ûºÅ¡£
ÔÚLaravelÖУ¬¿ÉÒÔʹÓÃÒ»¸ö½Ð×ö¡°Laravel Localization¡±µÄ×é¼þÀ´ÊµÏÖÓ¢ÎÄ·ûºÅתÖÐÎÄ·ûºÅ¡£Localization£¨ÍâµØ»¯£©ÊÇÖ¸½«³ÌÐò˳Ӧ²î±ðÓïÑÔºÍÎÄ»¯ÇéÐεÄÀú³Ì¡£Í¨¹ýʹÓÃLaravel Localization£¬ÎÒÃÇ¿ÉÒÔ¼òÆӵؽ«Ó¢ÎÄ·ûºÅת»»ÎªÖÐÎÄ·ûºÅ£¬Ê¹µÃ³ÌÐòÔ½·¢Ò×ÓÚÔĶÁºÍÃ÷È·¡£
ÏÂÃ棬ÎÒÃÇÀ´¿´Ò»ÏÂÔõÑùʹÓÃLaravel Localization¾ÙÐÐÓ¢ÎÄ·ûºÅµ½ÖÐÎÄ·ûºÅµÄת»»¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃLaravel Localization¡£¿ÉÒÔͨ¹ýComposerÀ´×°Öãº
composer require mcamara/laravel-localization
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇÐèÒªÔÚ config/app.php ÖÐ×¢²áServiceProvider£º
'providers' => [ // ... Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider::class, ];
µÇ¼ºó¸´ÖÆ
½Ó×Å£¬ÔÚ config/app.php ÖÐ×¢²áFacades£º
'aliases' => [ // ... 'LaravelLocalization' => Mcamara\LaravelLocalization\Facades\LaravelLocalization::class, ];
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔÔÚ routes.php ÎļþÖнç˵ËùÓÐÖ§³ÖµÄÓïÑÔ£º
Route::group([ 'prefix' => LaravelLocalization::setLocale()], function () { // ... });
µÇ¼ºó¸´ÖÆ
ÔÚ½ç˵ÓïÑÔºó£¬ÎÒÃÇÐèÒª½ç˵ÓïÑÔÎļþ£¬¸ÃÎļþÉúÑÄÁËËùÓеÄÓ¢ÎÄ·ûºÅÒÔ¼°¶ÔÓ¦µÄÖÐÎÄ·ûºÅ¡£ÔÚLaravel LocalizationÖУ¬ÓïÑÔÎļþͨ³£´æ´¢ÔÚ resources/lang Ŀ¼Ï£¬ÃüÃûΪ en.php£¨Ó¢ÎÄ£©ºÍ cn.php£¨ÖÐÎÄ£©¡£
ÒÔʹÓõÄÓïÑÔΪÖÐÎÄΪÀý£¬ÎÒÃÇÔÚ cn.php ÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
return [ '@' => '£À', '#' => '££', '&' => '£¦', // ... ];
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉϽç˵£¬ÎÒÃǽ« @ ·ûºÅת»»Îª £À ·ûºÅ£¬# ·ûºÅת»»Îª ££ ·ûºÅ£¬& ·ûºÅת»»Îª £¦ ·ûºÅ£¬ÒÀ´ËÀàÍÆ¡£
×îºó£¬ÎÒÃÇÐèÒªÔÚ´úÂëÖÐʹÓà trans º¯Êý¾ÙÐзûºÅµÄת»»¡£ÀýÈ磬ÎÒÃÇʹÓà trans º¯Êý½« @ ·ûºÅת»»Îª £À ·ûºÅ£º
$email = 'example@mail.com'; $converted_email = trans($email, ['@' => '£À']); echo $converted_email; // ½«Êä³ö example£Àmail.com
µÇ¼ºó¸´ÖÆ
ͨ¹ýÕâ¸öÀý×Ó£¬ÎÒÃÇ¿ÉÒÔ¿´µ½£¬Laravel LocalizationÌṩÁËÒ»ÖÖ¼òÆÓÇÒÒ×ÓÚ²Ù×÷µÄÒªÁ죬Äܹ»ºÜºÃµØ½â¾öÓ¢ÎÄ·ûºÅÔÚʹÓÃÖÐÎľÙÐн»Á÷ʱµÄÎÊÌâ¡£
×ܽáÀ´Ëµ£¬Laravel LocalizationÄܹ»×ÊÖúÎÒÃÇÔÚʹÓÃLaravel¿ò¼Ü¾ÙÐбà³Ìʱ¾ÙÐÐÓ¢ÎÄ·ûºÅµ½ÖÐÎÄ·ûºÅµÄת»»¡£ÕâÑù£¬ÎÒÃǾÍÄܹ»×èÖ¹Ó¢ÎÄ·ûºÅ´øÀ´µÄÀ§ÈÅ£¬ÈóÌÐòÔ½·¢Ò×ÓÚÔĶÁºÍÃ÷È·¡£
ÒÔÉϾÍÊÇlaravelÔõô½«Ó¢ÎÄ·ûºÅתÖÐÎÄ·ûºÅµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡