laravel-blog
laravel-blog copied to clipboard
How to show english posts for all language codes & link without language code?
My website uses 3 languages. Default: www.domainname (calls the english version) English: www.domainname/en German: www.domainname/de French: www.domainname/fr
I want my blog to display only english posts, no matter what language the user uses.
I created German and French languages in the blog admin panel. Default: www.domainname/blog (404 error) English: www.domainname/en/blog German: www.domainname/de/blog French: www.domainname/fr/blog
How can I always show english posts for all language codes and also for the link without a language code (www.domainname/blog)?
You can use sessions to store active language. And English could be the default language. Then you use select input or anything like that for a user to change locale. That feature needs to use middleware to always check active language.
I think you misunderstand my question. What you describe is already in place.
But the problem is if the user selects german, then the blog only shows posts that have been written with the selected language german. I want all languages to show posts that have been written for the language english though. And not the posts of the selected language.
Hi guys @dappstatus @jmatike , thanks for reporting.
I've created a branch named "no-locale-route-support" to add this functionality. Unfortunately Laravel does not support optional prefix in route grouping, and this made it a little complex.
Anyway you @dappstatus can use the changes in this branch: https://github.com/binshops/laravel-blog/tree/no-locale-route-support now you can access your blog at: Default: www.domainname (calls the english version) but as you see, the urls for posts and categories include the {locale}. So, I've changed the DetectLanguage logic to flag requests when the locale is not required with this attribute: noLocaleRoute With the help of this attribute you can generate routes dynamically so that they do not include locale. That was the explanation of what is the logic, probably you are interested into contribute to add this functionality.
Thanks, have a great day ;)
To contribute to this feature try to make PR from this branch: https://github.com/binshops/laravel-blog/tree/no-locale-route-support
Awesome this is the solution, thank you! For displaying english in all versions, you have to remove where("lang_id" , '=' , $request->get("lang_id")) or change it to the english id in BinshopsReaderController.
So far this is working very well. I'm looking for 1 more improvement though, that I can't find the solution for.
I use 3 languages in my project, with the default being en In LaravelLocalization you can set 'hideDefaultLocaleInURL' => true. This will result in an auto redirect of /en/ to / by using 'prefix' => LaravelLocalization::setLocale() in web routes.
Now I tried to do the same thing for the laravel-blog routes. I tried to change:
Route::group(['prefix' => "/{locale}/".config('binshopsblog.blog_prefix', 'blog')], function () {
to
Route::group(['prefix' => (App::getLocale() !== 'en' ? "/{locale}/" : "/").config('binshopsblog.blog_prefix', 'blog')], function () {
Although the redirect itself works this way, it results in the following error:
Route [binshopsblog.single] not defined
which has to do with this line of code:
<a href='{{$noLocaleRoute == 1 ? $post->url('') : $post->url($locale)}}'><span>{{$post->title}}</span></a>
Any idea what the issue with this line of code is after that change in the routes?
After some more testing it seems like ->url('')
doesn't work after the change in route, it has no result when debugging.
New feature added: https://github.com/binshops/laravel-blog/releases/tag/v9.3.0