If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Laravel में Localization string को different - different language में retrieve करने का एक convenient feature है। Localization की help से हम अपने Application को multiple languages में बना सकते हैं।
सभी translated string को resources/lang directory में रखा जाता है। lang directory के अंदर ही सभी language abbreviation की directory बनती है , और इन abbreviation directory के अंदर बाकी translated files होती हैं।
/resources /lang /en messages.php /es messages.php
सभी translated language file एक Associated Array return करती हैं , इसमें define की गयी key के according ही blade file पर हम string को access करते हैं।
For Example :
<?php return [ 'welcome' = 'Welcome to our application', ];
By Default Laravel Application में default Locale "en" set होती है , लेकिन आप getLocale() or isLocale() method के through current locale पता कर सकते हैं।
$locale = App::getLocale(); if (App::isLocale('en')) { // }
setLocale() method की help से आप अपनी need के according locale language set कर सकते हैं।
Route::get('welcome/{locale}', function ($locale) { if (! in_array($locale, ['en', 'es', 'fr'])) { abort(400); } App::setLocale($locale); });
__() या trans() helper function की help से आप translated string को language file से retrieve कर सकते है।
For Example , अगर हमें en & es के लिए resources/lang/en/messages.php और resources/lang/es/messages.php file बनाई है , जिसमे एक welcome name की key define की है तो उसे कुछ इस तरह से access करेंगे।
echo __('messages.welcome'); Or echo trans('messages.welcome');
अगर आप localization को blade template पर use करना चाहते हैं तो simply {{ }} के अंदर print करा सकते हैं। इसके अलावा blade file पर @lang directive भी use कर सकते हैं।
{{ __('messages.welcome') }} Or {{trans('messages.welcome')}} Or @lang('messages.welcome')
इसके अलावा अगर आप translated string में dynamically किसी parameter (subtring) को replace करना चाहते हैं तो उसे colon : से prefix करके key के साथ define करना पड़ेगा। और retrieve करते समय same [ key => value] के साथ array pass करना पड़ेगा।
/*when define tranlated string*/ return [ 'welcome' => 'Welcome, :name', ]; /*when retrieving string*/ {{__('messages.welcome', ['name' => 'dayle'])}} Or {{trans('messages.welcome', ['name' => 'dayle'])}} Or @lang('messages.welcome', ['name' => 'dayle'])
I Hope ! अब आपको Laravel में Localization का concept clear हो गया होगा।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook