locked
Localization strategie for asp.net core 3.1 application RRS feed

  • Question

  • User1286808301 posted

    Hello.
    I would like to set localization like that in my asp.net core 3.1 application:

    - QueryStringRequestCultureProvider will be a way to set the header when the server receive the culture get parameter
    - It's also possible to pass "culture" parameter with another "forceCookie=true" parameter to QueryStringRequestCultureProvider to force using cookie instead of Header. In this case, header will no longer use and a cookie will be sent
    - As long as no query is given, I want to set automaticaly a header with a default "en" culture (default behavior).
    - finaly, I want to use 3 type of resource file like Example.en-US.resx or Example.en.resx (when cookie or header is defined) and Example.resx as fallback if no cookie and header was found.

    In fact, my application will coded to use @Localizer["Native String"] of the default resource file (Example.resx). But if a header or a cookie is provided with en-US, the application looking for the Example.en-US.resx first and if not exists It look for Example.en.resx. The default behavior is to have a en-US header by default for each request. Override this default header if an url like ?culture=es-MX is provided. Or disable header default and set a cookie if URL like ?culture=es-MX&forceCookie=true is provided

    I have created a github base project to show you my actual code. If someone could show me how to do this ;)

    Thanks in advance for your help.

    Monday, February 3, 2020 7:26 AM

Answers

All replies

  • User-821857111 posted

    The default behavior is to have a en-US header by default for each request.
     You can't set the Accept header. But you can set the default culture: https://www.mikesdotnetting.com/article/345/localisation-in-asp-net-core-razor-pages-cultures.

    You can also add, remove and change the order of the RequestCultureProviders as needed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 3, 2020 7:58 AM
  • User1286808301 posted

    Hi. Thanks for your anwser.

    But are you sure I can't set the accept header ? The documentation here provide an ApplyCurrentCultureToResponseHeaders option to force header to be sent...

    Monday, February 3, 2020 9:01 AM
  • User1286808301 posted

    The documentation here provide an ApplyCurrentCultureToResponseHeaders option to force header to be sent...

    Sorry. Here I confused Accept-Language and Content-Language. Therefore the default behavior is correct for me

    I'm glade to discover your website. In your anwser, the tutorial provided is better as those I finded on the web ;-)
    My example on github works but the configuration is better in your own example ;-)

    I going to test with your example. I keep this topic unresolved for a time if some other question or problem appears.

    Monday, February 3, 2020 11:46 AM
  • User1286808301 posted

    Ok. After some tests, the default behavior respect my needs. Thanks Mikesdotnetting wink

    Tuesday, February 4, 2020 3:33 AM