locked
Reading Culture value problem RRS feed

  • Question

  • User-715140818 posted

    I am working on globalization project.

    I need to get culture information based on Country Passed in below method.

    I have my locale table as follow.

     Id      Country                   Culture

    1       <st1:country-region w:st="on"><st1:place w:st="on">US</st1:place></st1:country-region>                            en-US

    2       FR                             fr-FR

     In my ListData.cs I have method having country as parameter.So I write method as follow.

     public static string GetCulture(string Country)

            {   Entities dataContext = new Entities(System.Configuration.ConfigurationManager.ConnectionStrings["Entities"].ConnectionString);

     var  v = ((from c in dataContext.Locale

                          where (c.Country == Country)

                          select new { c.Culture }.ToString()));

                  return v.ToString();

           }

    In my HomeController.cs I have method which retrieves Country c based on query string

     public ActionResult Index()

            {

     

                if (!string.IsNullOrEmpty(Request.QueryString["c"]))

                    System.Web.HttpContext.Current.Session["CountryCode"] = Request.QueryString["c"];

                else

                    System.Web.HttpContext.Current.Session["CountryCode"] = "US";

    }

     

    For eg if querystring is like http://localhost:1234/?c=”FR”

    Then in homecontoller c is set to FR.That is again set in session.

    Now I need to set culture based on Country passed.If County passed is “FR”,then my culture should become fr-FR.Si in ListData.cs I wrote method GetCulture.

     I don’t have to use

    System.Threading.Thread.CurrentThread.CurrentCulture.ToString() to retrieve browset culture information.

     Method I created in ListData.cs is used in many pages.So I declare variable to hold Culture returned through method in  GetCulture() and try to use that variable value in other pages like HomeController.cs as follow.

     

     public void FillData()

      {

                string sLocale = HyDataProvider.ListData.GetLocale(System.Web.HttpContext.Current.Session["CountryCode"].ToString());

               ddlMake.DataSource = HY.DataProvider.ListData.GetMakes(null, System.Web.HttpContext.Current.Session["Country"].ToString(), sLocale);

                ddlMake.DataTextField = "Name";

                ddlMake.DataValueField = "Id";

                ddlMake.DataBind(); 

            }

     

    There is lot of other method will have sLocale as parameter value.I have to set sLocale as Culture based on Country passed.If Country passed is “FR” then sLocale should be “fr-FR”.This value should come from my Locale table not browser current culture thread.You can see there is already one parameter value passed as System.Web.HttpContext.Current.Session["Country"].ToString().This session Country will get value from query string declared in HomeController.

    Only problem is sLocale.It is not able to get value from method declared in ListData.cs.WHen I try to access sLocale I am getting error as object instance not set to current object.

     

     

      

     

     

     

     

     

     

     

     

    Tuesday, August 3, 2010 6:48 PM

Answers

  • User-715140818 posted

     I figured out

    public static string GetLocale(string CountryCode)

            {

           Entities dataContext = new Entities(System.Configuration.ConfigurationManager.ConnectionStrings["Entities"].ConnectionString);

     var v = ((from c in dataContext.Locales

                          where (c.CountryCode == CountryCode)

                          select new { sLocale = c.LanguageCulture }).FirstOrDefault();

    return v.ToString();

    }

    It should have FirstORDefault.

    But query return LanguageCulture="fr-FR" when I pass countryCode="FR" but I need it to return only "fr-FR" not LanguageCulture="fr-FR".

    Can anybody help me out.It's very urgent.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 4, 2010 6:29 AM