Home > Software engineering >  How to set language dynamically in _host.cshtml
How to set language dynamically in _host.cshtml

Time:01-05

In _host.cshtml page I have this code

<!DOCTYPE html>
@{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
}
<html>
@if (cultureInfo.Name == "ar")
{
    <dir ="rtl"></dir>
    <lang ="ar"></lang>
}
else
{
    <dir ="ltr"></dir>
    <lang ="en"></lang>
}
<head></head>
<body></body>
</html>

but I get this warning

Warning (active) HTML0003 Missing attribute name.

On ("rtl","ar","rtl","en")
How can I make it work

CodePudding user response:

You are indeed missing the attribute name.... I suspect what you want to do is this:

<!DOCTYPE html>
@{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    string dir, lang;
    if (cultureInfo.Name == "ar")
    {
       dir = "rtl";
       lang = "ar";
    }
    else
    {
       dir = "ltr";
       lang = "en";
    }
}
<html lang="@lang" dir="@dir">
<head></head>
<body></body>
</html>
  •  Tags:  
  • Related