Skip to content

Commit 210ea10

Browse files
Ivan KiralIvan Kiral
authored andcommitted
Change culture in controller
1 parent 024a226 commit 210ea10

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

DancingGoat.Tests/ArticlesControllerTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ public async Task Index_ReturnsAViewResult_WithAnArticle()
2222
{
2323
// Arrange
2424
var config = new Mock<IConfiguration>();
25-
25+
26+
CultureInfo.CurrentUICulture = new CultureInfo("en-US");
27+
2628
MockHttpMessageHandler mockHttp = new MockHttpMessageHandler();
27-
mockHttp.When($"https://deliver.kontent.ai/975bf280-fd91-488c-994c-2f04416e5ee3/items?elements.url_pattern%5Beq%5D=on_roasts&depth=1&language={CultureInfo.CurrentCulture}&system.type=article")
29+
mockHttp.When($"https://deliver.kontent.ai/975bf280-fd91-488c-994c-2f04416e5ee3/items?elements.url_pattern%5Beq%5D=on_roasts&depth=1&language={CultureInfo.CurrentUICulture}&system.type=article")
2830
.Respond("application/json", File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"on_roasts.json")));
2931
IDeliveryClient client = DeliveryClientBuilder.WithProjectId("975bf280-fd91-488c-994c-2f04416e5ee3").WithDeliveryHttpClient(new DeliveryHttpClient(mockHttp.ToHttpClient())).WithTypeProvider(new CustomTypeProvider()).Build();
3032
var factory = new Mock<IDeliveryClientFactory>();
3133
factory.Setup(m => m.Get()).Returns(client);
3234

33-
ArticlesController controller = new ArticlesController(config.Object, factory.Object);
35+
3436

37+
ArticlesController controller = new ArticlesController(config.Object, factory.Object);
3538
// Act
3639
var result = await controller.Show("on_roasts");
3740

DancingGoat/Controllers/ArticlesController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Threading.Tasks;
34
using DancingGoat.Models;
45
using Microsoft.AspNetCore.Mvc;
@@ -8,6 +9,7 @@
89
using Kontent.Ai.Urls.Delivery.QueryParameters.Filters;
910
using Microsoft.AspNetCore.Http.Extensions;
1011
using Microsoft.AspNetCore.Localization;
12+
using Microsoft.Extensions.Localization;
1113

1214
namespace DancingGoat.Controllers
1315
{
@@ -38,11 +40,8 @@ public async Task<ActionResult> Show(string urlSlug)
3840
new DepthParameter(1),
3941
new LanguageParameter(Language)
4042
);
41-
42-
var requestCultureFeature = Request.HttpContext.Features.Get<IRequestCultureFeature>();
43-
var culture = requestCultureFeature?.RequestCulture.Culture;
4443

45-
if (response.Items[0].System.Language != culture?.Name)
44+
if (response.Items[0].System.Language != Language)
4645
{
4746
var urlParts = Request.GetDisplayUrl().Split('/');
4847
urlParts[3] = response.Items[0].System.Language;

DancingGoat/Controllers/ControllerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ControllerBase : Controller
1111
{
1212
protected readonly IDeliveryClient _client;
1313

14-
protected string Language => CultureInfo.CurrentCulture.Name;
14+
protected string Language => CultureInfo.CurrentUICulture.Name;
1515

1616
public ControllerBase(IDeliveryClientFactory deliveryClientFactory) : this()
1717
{

DancingGoat/Infrastructure/RouteRequestCultureProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Globalization;
4-
using System.Threading;
1+
using System.Collections.Generic;
52
using System.Threading.Tasks;
63
using Microsoft.AspNetCore.Http;
74
using Microsoft.AspNetCore.Localization;

0 commit comments

Comments
 (0)