AspNetCore.Docs/aspnet/fundamentals/localization/sample/Controllers/InfoController.cs

70 lines
1.9 KiB
C#

using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Localization;
using Microsoft.AspNet.Mvc;
using Microsoft.Extensions.Localization;
namespace Localization.StarterWeb.Controllers
{
public class InfoController : Controller
{
private readonly IStringLocalizer<InfoController> _localizer;
private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
public InfoController(IStringLocalizer<InfoController> localizer,
IStringLocalizer<SharedResource> sharedLocalizer)
{
_localizer = localizer;
_sharedLocalizer = sharedLocalizer;
}
public string TestLoc()
{
string msg = "Shared resx: " + _sharedLocalizer["Hello!"] +
" Info resx " + _localizer["Hello!"];
return msg;
}
public IActionResult Index()
{
return View();
}
public IActionResult About()
{
ViewData["Message"] = _sharedLocalizer["Your application description page."];
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = _sharedLocalizer["Your contact page."];
return View();
}
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
public IActionResult Error()
{
return View();
}
}
}
namespace Localization.StarterWeb.LocSample
{
public class SharedResource
{
}
}