.NET Framework Developer Center > .NET Development Forums > ASMX Web Services and XML Serialization > Is it possible to get/detect culture in web service
Ask a questionAsk a question
 

AnswerIs it possible to get/detect culture in web service

  • Tuesday, November 03, 2009 9:00 AMdevBrix Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,
    I have a situation where I need to detect the language the browser is set to in a webservice.
    I have tried all sorts including httpRequest.UserLanguages etc but I get a count of 0 but I know the browser has 2 languages in there.

    The scenario is this.
    I have a website asp.net 1.1 with some java applet calling a asmx webservice and within this webservice without passing any locale etc... I must be able to detect the language of the browser.Is this possible?

    thanks a lot


    Thanks for your help

Answers

  • Tuesday, November 03, 2009 3:58 PMBalaji Baskar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    Hi,
    I have a situation where I need to detect the language the browser is set to in a webservice.
    I have tried all sorts including httpRequest.UserLanguages etc but I get a count of 0 but I know the browser has 2 languages in there.

    The scenario is this.
    I have a website asp.net 1.1 with some java applet calling a asmx webservice and within this webservice without passing any locale etc... I must be able to detect the language of the browser.Is this possible?


    Is this something like this your were looking for..?

            [WebMethod]
            public string GetCulture()
            {
                string result = string.Empty;
                System.Globalization.CultureInfo cultureInfo = ResolveCulture();
                if (cultureInfo != null)
                    result = cultureInfo.DisplayName;
                return result;
            }
    
            [WebMethod]
            public string GetCountry()
            {
                string result = string.Empty;
                System.Globalization.RegionInfo resolveCountry = ResolveCountry();
                if (resolveCountry != null)
                    result = resolveCountry.DisplayName;
                return result;
            }
    
            public static System.Globalization.CultureInfo ResolveCulture()
            {
                string[] languages = HttpContext.Current.Request.UserLanguages;
                if (languages == null || languages.Length == 0)
                    return null;
                try
                {
                    string language = languages[0].ToLowerInvariant().Trim();
                    return System.Globalization.CultureInfo.CreateSpecificCulture(language);
                }
                catch (ArgumentException)
                {
                    return null;
                }
            }
    
            public static System.Globalization.RegionInfo ResolveCountry()
            {
                System.Globalization.CultureInfo culture = ResolveCulture();
                if (culture != null)
                    return new System.Globalization.RegionInfo(culture.LCID);
                return null;
            }
    

    Balaji Baskar

All Replies

  • Tuesday, November 03, 2009 3:00 PMJohn SaundersMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I don't think there's a way to do this in general.

    If you want to know the culture, then the Java applet should send the culture explicitly.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
  • Tuesday, November 03, 2009 3:58 PMBalaji Baskar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    Hi,
    I have a situation where I need to detect the language the browser is set to in a webservice.
    I have tried all sorts including httpRequest.UserLanguages etc but I get a count of 0 but I know the browser has 2 languages in there.

    The scenario is this.
    I have a website asp.net 1.1 with some java applet calling a asmx webservice and within this webservice without passing any locale etc... I must be able to detect the language of the browser.Is this possible?


    Is this something like this your were looking for..?

            [WebMethod]
            public string GetCulture()
            {
                string result = string.Empty;
                System.Globalization.CultureInfo cultureInfo = ResolveCulture();
                if (cultureInfo != null)
                    result = cultureInfo.DisplayName;
                return result;
            }
    
            [WebMethod]
            public string GetCountry()
            {
                string result = string.Empty;
                System.Globalization.RegionInfo resolveCountry = ResolveCountry();
                if (resolveCountry != null)
                    result = resolveCountry.DisplayName;
                return result;
            }
    
            public static System.Globalization.CultureInfo ResolveCulture()
            {
                string[] languages = HttpContext.Current.Request.UserLanguages;
                if (languages == null || languages.Length == 0)
                    return null;
                try
                {
                    string language = languages[0].ToLowerInvariant().Trim();
                    return System.Globalization.CultureInfo.CreateSpecificCulture(language);
                }
                catch (ArgumentException)
                {
                    return null;
                }
            }
    
            public static System.Globalization.RegionInfo ResolveCountry()
            {
                System.Globalization.CultureInfo culture = ResolveCulture();
                if (culture != null)
                    return new System.Globalization.RegionInfo(culture.LCID);
                return null;
            }
    

    Balaji Baskar
  • Tuesday, November 03, 2009 7:28 PMdevBrix Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your reply.
    the problem with the Java applet is I know NOTHING about java applet at all . How can I do it?
    what is the equivalent in java of "HttpContext.Current.Request.UserLanguages[0] or how can I detect the browser in java.

    any suggestions?

    ideally it should be done in the java applet but i have not got a clue about it.
    Any help?

    Thanks for your help
  • Tuesday, November 03, 2009 7:31 PMdevBrix Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Balaji
    thanks for your code. I have similar code but the problem is that  in a webservice
    "string[] languages = HttpContext.Current.Request.UserLanguages;" is always empty.It doesnt work in a webservice.If it did my problems would be resolved.
    I even tried  HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"] but again is null in a web service.

    do you know how I could achieve the same in Java?
    any suggestions?

    Thanks for your help
  • Tuesday, November 03, 2009 9:50 PMBalaji Baskar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, The code that i have provided was tested in webservice project. you would see 2 webmethods (GetCulture(), GetCountry()) and they call 2 public methods (ResolveCulture(), ResolveCountry()).

    I tried running this code and got this result back:
    HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]
    //result: en-us

    from the browser, same result

     

    <script language="javascript">

    document.write(navigator.browserLanguage)

     

    </script>



    Balaji Baskar
  • Wednesday, November 04, 2009 5:33 AMdevBrix Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,
    Thanks for your reply.Still doesnt work.
    I have created a noddy test

    1)Create a new Project Web application
    2)Created a new WebService and copied your methods
    3)IN the Web Application I have added a web reference to the webService
    4)called your methods
    5)they return null

    I hope I m doing something wrong.

    What I am saying is that in the webservice it's all null.
    I wonder how you did it in the webservice.Did you have the same set up as I did?

    thanks a lot for your help.


    Thanks for your help