I am trying to localize responses of my web api.
I created a Resource file (Messges.resx) in my project containing all the strings that are localized. And using Accept-Language header to identify user's language.
In the responses I can set the string as:
response = Messages.KEY_GOOD_MORNING
which will get the string for that name in language of the current thread culture. I will have to change thread's current culture to the culture found from Accept-Language header. But I don't want to change the culture of the thread since this will also change number/date formatting which is problematic for me.
Another alternative I see is- using ResourceManager and passing the culture as-
Messages.ResourceManager.GetString("KEY_GOOD_MORNING", CultureInfo.CreateSpecificCulture(lang.value))
This way I won't have to change the culture of the thread. But the problem with this approach is- string names aren't type safe anymore. A typo in the string name passed to GetString() may result into null values returned.
Is there any other approach I can adopt to avoid both the problems above?
Firstly you should be check this msdn discussion
For best approach for this problem. You should be set globalization setting on your 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 English culture case
<globalization
enableClientBasedCulture="false"
uiCulture="en-US"
culture="en-US" />
With that you'll be able to use culture thread with config. Also you can be set culturethread with browser settings to auto, you can be set to auto and ignore some cases. For more information you can check link above.
If you want to accept browser's culture
<globalization
enableClientBasedCulture="true"
uiCulture="auto"
culture="auto" />
Or with default action
<globalization
enableClientBasedCulture="true"
uiCulture="auto:en-US"
culture="auto:en-US" />
For best practice don't you ever change your computer thread culture info. But you can change session based change current thread culture, it's just thread based info not the all system. I hope it'll be usefull.
Just a thought, but you could use your approach and use the nameof()
construct/keyword to keep type safety - as that was your question initially "ut the problem with this approach is- string names aren't type safe anymore. A typo in the string name passed to GetString() may result into null values returned."
Example using nameof
:
Messages.ResourceManager.GetString(nameof(Messages.KEY_GOOD_MORNING), CultureInfo.CreateSpecificCulture(lang.value))
What it does & documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With