I have below code in my controller.
public function AllCountries() {
$Countries = (new \App\DataAccess\CountryData())->GetAllCountries();
app()->setLocale('fr');
return view('Country.List')->with('Countries', $Countries->getData()->CountryList);
}
Definition of method to fetch data is below.
public function GetAllCountries() {
return response()->json(['CountryList' => \App\Models\CountryModel::all()]);
}
Below is the screenshot which shows Translation array
Below is the screenshot that shows the View called by Controller has just one word to show translation only.
When I run the application, I get following error.
Use of undefined constant CountriesList - assumed 'CountriesList' (View: C:\xampp\htdocs\MyAccountesources\views\Country\List.blade.php)
Normally when we see websites which is not a default language set in our browser. We see a notification to translate the page on Page load. See the screenshot below.
But this does not come on my side. Although I can see French language but Translation notification is not coming. Why?
Read here for more about how localization works in Laravel:
https://laravel.com/docs/5.2/localization#introduction
If you really want to override the entire template, you can do this yourself:
$app->getLocale()
App::getLocale()
(but, I'm not suggesting this is usually a good idea - usually translating strings is a better idea.)
Those functions return the locale, so you can use this to fetch the appropriate view (e.g. placing all jp views inside a jp/ directory.)
You can use a ViewFactory's exists() method to check if it exists, and implement fallback logic as you like. It might make a lot of sense to implement this on your own controller base class (which would inherit from the framework one), and have all your controllers subclass this new base class. This way the logic can be shared.
Something roughly like this, for example:
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
class Controller extends BaseController
{
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
protected function localeViewResponse($view, $data = [], $mergeData = [])
{
$factory = view();
$locale = app()->getLocale();
if ($view->exists($locale . '.' . $view)) {
return $view->make($locale . '.' . $view, $data, $mergeData);
}
return $view->make($view, $data, $mergeData);
}
}
That being said, again, you should really consider if you have to do this. Angad's method will make sense to other people familiar with Laravel, and is usually the better approach. If you're having problems with 'undefined constant', make sure you're quoting things correctly and try updating your question with the code you've attempted.
You are going about it incorrectly.
Laravel, like most other mvc frameworks, provides localization support out of the box: https://laravel.com/docs/5.2/localization
So the following would be a better approach:
Create language files for the languages your application is offering ( for eg: english and french ).
resources
| lang
| en
| countries.php
| fr
| countries.php
In each of the language files you would put the translations for variables appropriately:
// resources/lang/en/countries.php
return [
'india' => "India"
];
// resources/lang/fr/countries.php
return [
'india' => "Inde"
];
Note: I dont know french I'm using google translate for this example
Then in your view:
{{ trans('countries.india') }}
That will display the value from the appropriate language file depending on the application setting.
You can change the application language like so:
Route::get('countries/{locale}', function ($locale) {
App::setLocale($locale);
return view('countries.list');
});