I have found this link as a guide to make a multi language website which it should run with users preferred language which they have set on their browsers.
Get CultureInfo from current visitor and setting resources based on that?
as you see it has this code to do it
// Get Browser languages.
var userLanguages = Request.UserLanguages;
CultureInfo ci;
if (userLanguages.Count() > 0)
{
try
{
ci = new CultureInfo(userLanguages[0]);
}
catch(CultureNotFoundException)
{
ci = CultureInfo.InvariantCulture;
}
}
else
{
ci = CultureInfo.InvariantCulture;
}
// Here CultureInfo should already be set to either user's preferable language
// or to InvariantCulture if user transmitted invalid culture ID
but my question is that I do not know what exactly is the duty of CultureInfo.InvariantCulture
and it does not work at all in my project. it is always null.
I changed the code to this , It works fine but I am not sure about possible exceptions may it have. I really appreciate any kind of help.
here is what I have and it works completely fine but just not sure about possible exceptions. I want the default language to be "en-US"
public ActionResult Index()
{
CultureInfo ci;
var userLanguages = Request.UserLanguages;
if (userLanguages == null)
{
ci = new CultureInfo("en-US");
}
else if (userLanguages.Count() > 0)
{
try
{
ci = new CultureInfo(userLanguages[0]);
}
catch (CultureNotFoundException)
{
ci = new CultureInfo("en-US");
}
}
else
{
ci = new CultureInfo("en-US");
}
return RedirectToAction(ci.TwoLetterISOLanguageName, "Home");
}