×ðÁú¿­Ê±¹ÙÍøµÇ¼

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel LocalizationʵÏÖ¶àÓïÑÔÖ§³Ö£¿

Ëæ×ÅÈ«Çò»¯µÄÇ÷ÊÆ £¬Ô½À´Ô½¶àµÄÍøÕ¾ºÍÓ¦ÓóÌÐòÐèÒªÖ§³Ö¶àÖÖÓïÑÔ ¡£ÔڹŰåµÄÍøÕ¾¿ª·¢ÖÐ £¬Í¨³£ÐèÒªÊÖ¶¯¹¹½¨¶à¸öÓïÑÔ°æ±¾µÄÒ³Ãæ £¬Õâ²»µ«ºÄʱºÄÁ¦ £¬²¢ÇÒÈÝÒ×ÍÉ»¯ ¡£Ëæ×ÅÏÖ´ú¿ª·¢ÊÖÒÕµÄÉú³¤ £¬Ê¹Óÿò¼ÜºÍ¹¤¾ßÀ´¼ò»¯¶àÓïÑÔÖ§³ÖÒѾ­³ÉΪһÖÖÇ÷ÊÆ ¡£

ÔÚLaravel¿ª·¢ÖÐ £¬Laravel LocalizationÊÇÒ»ÖÖÊ¢Ðеķ½·¨À´ÊµÏÖ¶àÓïÑÔÖ§³Ö ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃLaravel LocalizationÀ´ÔÚLaravelÓ¦ÓóÌÐòÖÐʵÏÖ¶àÓïÑÔÖ§³Ö ¡£

×°ÖúÍÉèÖÃ

Ê×ÏÈ £¬ÐèҪװÖÃlaravelµÄÍâµØ»¯×é¼þ ¡£¿ÉÒÔʹÓÃcomposerÀ´×°Öãº

composer require "laravelcollective/html":"^5.8.0"
php artisan vendor:publish --tag=laravel-errors --force

µÇ¼ºó¸´ÖÆ

È»ºó £¬ÐèÒªÔÚapp/configÏÂн¨Îļþlocale.php £¬ÓÃÓÚÉèÖÃÖ§³ÖµÄÓïÑÔÑ¡Ï

return [
    'locales' => ['en' => 'English', 'zh' => 'ÖÐÎÄ'],
    'default_locale' => 'en'
];

µÇ¼ºó¸´ÖÆ

ÔÚÕâÀï £¬localesÊý×é°üÀ¨ÁËËùÖ§³ÖµÄÓïÑÔÑ¡Ïî £¬ÆäÖÐÿ¸öÔªËصļüÊÇÓïÑԵĴúÂë £¬ÖµÊÇÓïÑÔµÄÃû³Æ ¡£default_localeÖ¸¶¨Ä¬ÈϵÄÓïÑÔÑ¡Ïî ¡£

½¨Éè¶àÓïÑÔ×ÊÔ´

½ÓÏÂÀ´ £¬ÐèҪΪËùÖ§³ÖµÄÿÖÖÓïÑÔ½¨ÉèÏìÓ¦µÄÓïÑÔ×ÊÔ´Îļþ ¡£ÓïÑÔ×ÊÔ´Îļþͨ³£°üÀ¨Ò»¸öÊý×é £¬ÆäÖаüÀ¨ÔÚ¸ÃÓïÑÔÇéÐÎÏÂʹÓõÄËùÓÐÍâµØ»¯×Ö·û´® ¡£¿ÉÒÔʹÓÃartisanÏÂÁîÀ´¿ìËÙ½¨ÉèÕâЩÎļþ £¬ÈçÏÂËùʾ£º

php artisan make:lang en
php artisan make:lang zh

µÇ¼ºó¸´ÖÆ

Õ⽫½¨ÉèÁ½¸ölangĿ¼ £¬Ò»¸öÊÇÓ¢ÎÄ £¬Ò»¸öÊÇÖÐÎÄ ¡£

±à¼­¶àÓïÑÔ×ÊÔ´

±à¼­Ã¿¸öÓïÑÔÇéÐεÄÓïÑÔ×ÊÔ´Îļþ £¬½«Ìض¨ÓÚ¸ÃÓïÑÔÇéÐεÄÍâµØ»¯×Ö·û´®Ìí¼Óµ½Êý×éÖÐ ¡£ÀýÈç £¬ÔÚen.phpºÍzh.phpÎļþÖÐ £¬¿ÉÒÔÌí¼ÓÒÔÏÂÄÚÈÝ£º

// en.php
return [
    "welcome" => "Welcome to our site",
    "about" => "About Us",
    "contact" => "Contact Us"
];

// zh.php
return [
    "welcome" => "½Ó´ý»á¼ûÎÒÃǵÄÍøÕ¾",
    "about" => "¹ØÓÚ×ðÁú¿­Ê±¹ÙÍøµÇ¼",
    "contact" => "ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼"
];

µÇ¼ºó¸´ÖÆ

ÔÚÊÓͼÖÐʹÓÃÍâµØ»¯×Ö·û´®

µ±ÊÓͼÐèÒª°üÀ¨ÍâµØ»¯×Ö·û´®Ê± £¬¿ÉÒÔʹÓÃLaravel LocalizationÖÐÌṩµÄtransº¯Êý ¡£transº¯Êý½ÓÊÜÒ»¸öÍâµØ»¯×Ö·û´®µÄ¼ü £¬²¢·µ»ØÏìÓ¦µÄÍâµØ»¯×Ö·û´® ¡£ÀýÈç £¬ÔÚbladeÄ£°åÖÐ £¬¿ÉÒÔÕâÑùʹÓÃtransº¯Êý£º

<h1>{{ trans('messages.welcome') }}</h1>

µÇ¼ºó¸´ÖÆ

ÔÚÕâÀï £¬transº¯Êý½«Æ¾Ö¤Ä¿½ñÓïÑÔÇéÐηµ»ØÏìÓ¦µÄÍâµØ»¯×Ö·û´® ¡£

Çл»ÓïÑÔÑ¡Ïî

×îºó £¬»¹¿ÉÒÔÔÚÓ¦ÓóÌÐòÖÐÌṩÇл»ÓïÑÔÑ¡ÏîµÄ¹¦Ð§ ¡£¿ÉÒÔÔÚÊÓͼÖн¨ÉèÒ»¸öÏÂÀ­¿òÀÍÆÈ´»ÓïÑÔÑ¡Ïî £¬²¢Ê¹ÓÃJavaScriptÀ´Ìá½»±íµ¥ £¬ÒԱ㽫ѡÔñµÄÓïÑÔÑ¡ÏîÉúÑĵ½LaravelµÄSessionÖÐ ¡£

<form id="locale-form" method="POST" action="{{ url('locale') }}">
    {{ csrf_field() }}
    <select name="locale" onchange="document.querySelector('#locale-form').submit();">
        @foreach (Config::get('locale.locales') as $key => $value)
            <option value="{{ $key }}" {{ ($key === App::getLocale()) ? ' selected' : '' }}>{{ $value }}</option>
        @endforeach
    </select>
</form>

µÇ¼ºó¸´ÖÆ

ÔÚ·ÓÉÖÐÌí¼ÓÒÔÏ´úÂë £¬ÒԱ㴦ÀíÀ´×ÔÓïÑÔÑ¡Ïî±íµ¥µÄÇëÇó£º

Route::post('locale', function(Request $request) {
    App::setLocale($request->locale);
    return redirect()->back();
});

µÇ¼ºó¸´ÖÆ

Õâ»á¸üÐÂÄ¿½ñ»á»°ÖеÄÓïÑÔÑ¡Ïî £¬²¢Öض¨Ïòµ½ÉÏÒ»Ò³ ¡£

½áÂÛ

ʹÓÃLaravel LocalizationÖ¼ÔÚ¼ò»¯¶àÓïÑÔÖ§³Ö £¬ÒÔ±ãÔÚLaravelÓ¦ÓóÌÐòÖÐÇáËɵØÍâµØ»¯ËùÓÐÒ³ÃæÔªËØ ¡£±¾ÎÄÖÐÏÈÈݵİ취¿ÉÓÃ×÷ÔÚLaravelÓ¦ÓóÌÐòÖÐʵÏÖ¶àÓïÑÔÖ§³ÖµÄÆðµã ¡£ÏÖʵÀú³ÌÖÐ £¬»¹¿ÉÒÔƾ֤ӦÓóÌÐòµÄÐèÒª¾ÙÐÐÊʵ±µÄµ÷½âºÍ¶¨ÖÆ ¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel LocalizationʵÏÖ¶àÓïÑÔÖ§³Ö£¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ