Base solution for your next web application
Open Closed

Pass Current Language in Query String for #3366


User avatar
0
moustafa created

Hello everyone i have a small issue hope you can help me with my application contains four different languages, in the public website i have for example News Page so i can write single news (item) with the four languages and then user can select the language he wants to read news in the frontend but suppose that user wants to share news item url in social media like Facebook so he want to be sure that he shared the url with the language he understand like Spain not English so i tried to do this , so when the user copy and past url in Facebook feed the news page will show up with the default language (English) of the application and the same if the user navigate to that url he will see the news page with the default language of the application and he was expected as the user who shared the news url he will see page with Spain not English so he probably get confused so i think if we can pass current language in the query string it will be more convenient how can we achieve that?

Thank you


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can get user's language in clinet side using

    abp.localization.currentLanguage.name
    

    . If you want to get current language on the server side, you can get current thread's culture.

    Thanks.

  • User Avatar
    0
    moustafa created

    Hello @ismcagdas it;s seems to br that you didn't read the post very well i know how to get current language of users in both client side and server side that is not problem at all so let me explain the issue with an example

    in frontend i have link like that <a class="postlink" href="http://127.0.0.1/news?id=10">http://127.0.0.1/news?id=10</a> because website contains four different language for anonymous users he will see that page in default language of the application and the same will happens if i share this url on Facebook or Google+ Feed but what i need to achieve is to make this url like that <a class="postlink" href="http://127.0.0.1/en/news?id=10">http://127.0.0.1/en/news?id=10</a> for English <a class="postlink" href="http://127.0.0.1/ar/news?id=10">http://127.0.0.1/ar/news?id=10</a> for Arabic <a class="postlink" href="http://127.0.0.1/sp/news?id=10">http://127.0.0.1/sp/news?id=10</a> for Spain and so on

    by using this behavior user will get the correct language from the first time he visited my website and everyone be happy :) hope this example is clear enough Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Actually I read it but maybe I didn't understand it very well. I also thought that, you will add language to url like this.

    http://127.0.0.1/news?id=10&culture=ar&ui-culture=ar
    

    If you do it like this, localization will work just like you want, see <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Localization#how-current-language-is-determined">https://aspnetboilerplate.com/Pages/Doc ... determined</a>.

    Thanks.

  • User Avatar
    0
    moustafa created

    Hi @ismcagdas

    yes that what i meant actually as mention in documentation ASP.NET Core's default provider uses culture & ui-culture URL query string values if present but ASP.NET MVC 5.x don't , so how can i implement same behavior here as i still using ASP.NET MVC 5.x

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In that case, you need to create a new current culture setter class deriving from <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web/Web/Localization/CurrentCultureSetter.cs">https://github.com/aspnetboilerplate/as ... eSetter.cs</a> and you can override SetCurrentCulture(HttpContext httpContext) method to check and set language from query string first.

    Of course, you also need to use ReplaceService in PreInitialize of your module. to replace current implementation with your version

    Configuration.ReplaceService<ICurrentCultureSetter, MyCurrentCultureSetter>(DependencyLifeStyle..Transient);
    

    It would be good to add this to ABP as well, can you also create an issue on ABP repository for this ?

    Thanks.

  • User Avatar
    0
    moustafa created

    Hi @ismcagdas Thank you for ur reply and help BWT i created ticket as u ask :) [https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2232])

    thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks :), It will be in the next release of ABP.