Redesign site as bilingual CV
This commit is contained in:
@@ -7,6 +7,7 @@ namespace LowLevelGuyCom.Controllers;
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
private const string LanguageCookieName = "site-lang";
|
||||
|
||||
public HomeController(ILogger<HomeController> logger)
|
||||
{
|
||||
@@ -15,7 +16,25 @@ public class HomeController : Controller
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
return View(UseJapanese() ? "IndexJa" : "Index");
|
||||
}
|
||||
|
||||
[Route("ja")]
|
||||
public IActionResult IndexJa()
|
||||
{
|
||||
return Redirect("/?lang=ja");
|
||||
}
|
||||
|
||||
[Route("cv")]
|
||||
public IActionResult Cv()
|
||||
{
|
||||
return View(UseJapanese() ? "CvJa" : "Cv");
|
||||
}
|
||||
|
||||
[Route("cv/ja")]
|
||||
public IActionResult CvJa()
|
||||
{
|
||||
return Redirect("/cv?lang=ja");
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
@@ -28,4 +47,66 @@ public class HomeController : Controller
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
|
||||
private bool UseJapanese()
|
||||
{
|
||||
string? queryLanguage = Request.Query["lang"].FirstOrDefault();
|
||||
if (TryNormalizeLanguage(queryLanguage, out bool queryIsJapanese))
|
||||
{
|
||||
Response.Cookies.Append(
|
||||
LanguageCookieName,
|
||||
queryIsJapanese ? "ja" : "en",
|
||||
new CookieOptions
|
||||
{
|
||||
MaxAge = TimeSpan.FromDays(365),
|
||||
SameSite = SameSiteMode.Lax
|
||||
});
|
||||
|
||||
return queryIsJapanese;
|
||||
}
|
||||
|
||||
if (TryNormalizeLanguage(Request.Cookies[LanguageCookieName], out bool cookieIsJapanese))
|
||||
return cookieIsJapanese;
|
||||
|
||||
return BrowserPrefersJapanese();
|
||||
}
|
||||
|
||||
private static bool TryNormalizeLanguage(string? language, out bool isJapanese)
|
||||
{
|
||||
isJapanese = false;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(language))
|
||||
return false;
|
||||
|
||||
string normalized = language.Trim().ToLowerInvariant();
|
||||
if (normalized.StartsWith("ja"))
|
||||
{
|
||||
isJapanese = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (normalized.StartsWith("en"))
|
||||
{
|
||||
isJapanese = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool BrowserPrefersJapanese()
|
||||
{
|
||||
string acceptLanguage = Request.Headers.AcceptLanguage.ToString();
|
||||
if (string.IsNullOrWhiteSpace(acceptLanguage))
|
||||
return false;
|
||||
|
||||
foreach (string segment in acceptLanguage.Split(',', StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
string language = segment.Split(';', 2)[0].Trim();
|
||||
if (TryNormalizeLanguage(language, out bool isJapanese))
|
||||
return isJapanese;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user