laravel-blog icon indicating copy to clipboard operation
laravel-blog copied to clipboard

How to show english posts for all language codes & link without language code?

Open dappstatus opened this issue 2 years ago • 6 comments

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)?

dappstatus avatar Apr 05 '22 12:04 dappstatus

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.

jmatike avatar Apr 05 '22 18:04 jmatike

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.

dappstatus avatar Apr 05 '22 22:04 dappstatus

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 ;)

samberrry avatar Apr 06 '22 09:04 samberrry

To contribute to this feature try to make PR from this branch: https://github.com/binshops/laravel-blog/tree/no-locale-route-support

samberrry avatar Apr 06 '22 09:04 samberrry

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.

dappstatus avatar Apr 09 '22 03:04 dappstatus

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.

dappstatus avatar May 19 '22 09:05 dappstatus

New feature added: https://github.com/binshops/laravel-blog/releases/tag/v9.3.0

samberrry avatar Dec 29 '22 09:12 samberrry