How to safely get a localized string in Web Api Controllers?

I am trying to localize the answers of my web api.

I created a resource file (Messges.resx) in my project containing all the lines that are localized. And using the Accept-Language header to determine the user's language.

In the answers, I can set the line as:

response = Messages.KEY_GOOD_MORNING 

which will receive a string for this name in the language of the current thread culture. I will need to change the current stream culture to the culture found from the Accept-Language header. But I do not want to change the culture of the stream , as this will also change the formatting of the number / date, which is problematic for me.

Another alternative that I see is using ResourceManager and passing culture as -

 Messages.ResourceManager.GetString("KEY_GOOD_MORNING", CultureInfo.CreateSpecificCulture(lang.value)) 

This way, I don’t have to change the flow culture. But the problem with this approach is that the string names are no longer stored in the type . A typo in the string name passed to GetString () can cause null values ​​to be returned.

Is there any other approach I can take to avoid both problems above?

+5
source share
2 answers

First, you should check out this msdn discussion.

For a better approach to this problem. You must set the globalization setting in the web.config file.

 <configuration> <system.web> <globalization enableClientBasedCulture="true|false" requestEncoding="any valid encoding string" responseEncoding="any valid encoding string" fileEncoding="any valid encoding string" responseHeaderEncoding = "any valid encoding string" resourceProviderFactoryType = string enableBestFitResponseEncoding = "true|false" culture="any valid culture string" uiCulture="any valid culture string"/> 

For the case of English culture

 <globalization enableClientBasedCulture="false" uiCulture="en-US" culture="en-US" /> 

With this, you can use the culture stream with configuration. You can also set culturethread with browser settings to auto, you can be set to automatic and ignore some cases. For more information, you can check the link above.

If you want to embrace browser culture

 <globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto" /> 

Or with a default action

 <globalization enableClientBasedCulture="true" uiCulture="auto:en-US" culture="auto:en-US" /> 

For best practice, you never change the culture information of a computer stream. But you can change the current culture of a session-based thread, this is just thread-based information, not the whole system. Hope this will be helpful.

+3
source

Just think, but you can use your approach and use nameof() construct / keyword to keep type safe, as that was your first question. "The problem with this approach is that string names are no longer type safe. Typo in the name the string passed to GetString () may return null values. "

An example of using nameof :

 Messages.ResourceManager.GetString(nameof(Messages.KEY_GOOD_MORNING), CultureInfo.CreateSpecificCulture(lang.value)) 

What he does and the documentation:

+2
source

Source: https://habr.com/ru/post/1264758/


All Articles