Here is my problem i've read already a lot of thing, I am actually following this tutorial
- https://5balloons.info/localization-laravel-multi-language-language-switcher/
- https://www.youtube.com/watch?v=po4Xt1G3gcc
But I can't get it working, I always get error and I've been stuck on this for the past 4 hours. Everyone who is using this technic seems to be able to make it works but not me.
I've already looked all these posts
- https://laravel.com/docs/9.x/blade
- Laravel - htmlspecialchars() expects parameter 1 to be string, object given
- Laravel Get Config Variable
- https://laracasts.com/discuss/channels/laravel/problem-in-retrieving-the-data-in-viewblade-laravel
- https://www.educative.io/answers/how-to-access-config-file-values-in-laravel-using-config
- Converting an array of arrays from Laravel's collection into an object with an array in json
- Laravel Blade loop through config array variable
- https://www.w3schools.com/php/php_arrays_multidimensional.asp
- https://laravel.com/docs/8.x/configuration
- Laravel: how to get direct config value in blade template?
And more links are missing.
The thing is whatever I do I either get the error it needs an array not a string (The third line is creating this error):
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ config('languages')[App::getLocale()] }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
@foreach (config('languages') as $lang => $language)
@if ($lang != App::getLocale())
<a class="dropdown-item" href="{{ route('lang.switch', $lang) }}"> {{$language}}</a>
@endif
@endforeach
</div>
</li>
Or I get the error telling me that my array is empty (The third line is creating the error):
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ config('languages.*')[App::getLocale()] }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
@foreach (config('languages') as $lang => $language)
@if ($lang != App::getLocale())
<a class="dropdown-item" href="{{ route('lang.switch', $lang) }}"> {{$language}}</a>
@endif
@endforeach
</div>
</li>
Or I get the error telling me it needs a string not an array:
Here is how my config is made: config/languages.php
<?php
return [
'en' => [
'display' => 'English',
'flag-icon' => 'us',
],
'fr' => [
'display' => 'Français',
'flag-icon' => 'fr',
],
'es' => [
'display' => 'Spanish',
'flag-icon' => 'es',
],
];
I also tried this and tried to access it with config(languages.languages):
<?php
return [
'languages' => [
'en' => [
'display' => 'English',
'flag-icon' => 'us',
],
'fr' => [
'display' => 'Français',
'flag-icon' => 'fr',
],
'es' => [
'display' => 'Spanish',
'flag-icon' => 'es',
],
],
];