0

I want to create a multi language website. I found out that one way to do it is to display the webpage in their own language based on the preferred language of user's browser. I tried the code below and it seems it works fine.

public ActionResult Index()
    {

        var userLanguages = Request.UserLanguages;
        string preferredLanguage = "";

        preferredLanguage = userLanguages[0];

        if (preferredLanguage == "fr-FR") 

        {

            return RedirectToAction("fr", "Home");
        }
        else 
        {
            return RedirectToAction("en", "Home");
        }

    }

I thought this was very simple and so I searched around and found this link

I am a little confused - should I add this code and what exactly does it do? When I debug the code I find every time that ci is null. How should I use 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

Appreciate any help.

with the help of Alex, its my code right now, It works well but I am worried about possible extensions

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");
    }
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • 1
    Is this just for your home page only? And do you intend to show a different URL (e.g. `mysite/home` and `mysite/home/fr`) to explicitly show the language setting? – Alex P Sep 21 '18 at 10:49
  • I do it some how it shows like this ( myurl.com/fr) or (myurl.com/en) @AlexP thank you very much for your care – neda Derakhshesh Sep 21 '18 at 11:09
  • I omit home by some changes in map.route @AlexP – neda Derakhshesh Sep 21 '18 at 11:09
  • 1
    The second part of the code sets the culture to a given language. This means that things like dates, numbers etc. are correctly formatted for that language. You'll likely need to do that. – Alex P Sep 21 '18 at 11:29

1 Answers1

2

Try this.

public ActionResult Index()
{
    CultureInfo ci;
    var userLanguages = Request.UserLanguages;

    if (userLanguages.Count() > 0)
    {
        try
        {
            ci = new CultureInfo(userLanguages[0]);
        }
        catch (CultureNotFoundException)
        {
            ci = CultureInfo.InvariantCulture;
        }
    }
    else
    {
        ci = CultureInfo.InvariantCulture;
    }

    return RedirectToAction(ci.TwoLetterISOLanguageName, "Home");
}

The TwoLetterISOLanguageName gets the two letter language code (e.g. fr-FR = fr). This avoids the if...else statement for the redirect.

Alex P
  • 12,249
  • 5
  • 51
  • 70
  • oh thank you very very much. it works completely fine. and thank you very very much for the explanation. it works great – neda Derakhshesh Sep 21 '18 at 12:14
  • I will appreciate very much if you could look at my question, I updated the code in two part. the first problem is that if users do not have any languages in their preferred list, then there were no exception to handle it while its null, and the other,I debugged the code in this part `ci = CultureInfo.InvariantCulture;` is always null and when code run on the else part, browser shows 404 and it seems it shows something like `iv` and this was not what I wanted.I just want to ask you please look at it and inform me if something is not fine.please let me know if I should ask it in a new question – neda Derakhshesh Sep 22 '18 at 12:17