views:

178

answers:

1

I want to localize Web Service in ASP.NET application.

I can write the following code to detect preferred client culture:

CultureInfo culture = null;
if (Request.UserLanguages!=null && Request.UserLanguages.Length>0) {
  string lang = Request.UserLanguages[0];
  if (!String.IsNullOrEmpty(lang)) {
    try {
      culture = new CultureInfo(lang) ;
    }
    catch {}
  }
}

And use this expression to get string from resources:

WebResources.ResourceManager.GetString(stringName, culture);

But I want to use something similar to Page directive attributes:

Culture="Auto" UICulture="Auto"

Is it possible?

+1  A: 

Firstly, this all depends on the client transmitting the headers from which the UserLanguages collection gleans it's information. Most consumers of your service if they're not browsers will not transmit that information.

Secondly, no I don't believe it is possible to do it automatically, however you could write something into the HttpContext.Items collection for the values and then wrap up

public string GetResource(string Key)
{
    culture = HttpContext.Items["UserLanguage"];
    WebResources.ResourceManager.GetString(stringName, culture);
}

Then your code would just read: -

GetResource("Blah");

Thanks,

Phil.

Plip
Thank for your answer.
Unholy