Skip to content

Commit 62fcc22

Browse files
committed
Always change user setting when changing the language
1 parent 5865f2e commit 62fcc22

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

src/Abp.AspNetCore/AspNetCore/Mvc/Controllers/AbpLocalizationController.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using Abp.AspNetCore.Mvc.Extensions;
34
using Abp.Auditing;
5+
using Abp.Configuration;
46
using Abp.Localization;
7+
using Abp.Runtime.Caching;
58
using Abp.Runtime.Session;
69
using Abp.Timing;
710
using Abp.Web.Models;
@@ -15,10 +18,18 @@ namespace Abp.AspNetCore.Mvc.Controllers
1518
public class AbpLocalizationController : AbpController
1619
{
1720
protected IUrlHelper UrlHelper;
18-
19-
public AbpLocalizationController(IUrlHelper urlHelper)
21+
private readonly ISettingStore _settingStore;
22+
23+
private readonly ITypedCache<string, Dictionary<string, SettingInfo>> _userSettingCache;
24+
25+
public AbpLocalizationController(
26+
IUrlHelper urlHelper,
27+
ISettingStore settingStore,
28+
ICacheManager cacheManager)
2029
{
2130
UrlHelper = urlHelper;
31+
_settingStore = settingStore;
32+
_userSettingCache = cacheManager.GetUserSettingsCache();
2233
}
2334

2435
[DisableAuditing]
@@ -43,11 +54,7 @@ public virtual ActionResult ChangeCulture(string cultureName, string returnUrl =
4354

4455
if (AbpSession.UserId.HasValue)
4556
{
46-
SettingManager.ChangeSettingForUser(
47-
AbpSession.ToUserIdentifier(),
48-
LocalizationSettingNames.DefaultLanguage,
49-
cultureName
50-
);
57+
ChangeCultureForUser(cultureName);
5158
}
5259

5360
if (Request.IsAjaxRequest())
@@ -69,7 +76,37 @@ public virtual ActionResult ChangeCulture(string cultureName, string returnUrl =
6976
}
7077
}
7178

72-
return LocalRedirect("/"); //TODO: Go to app root
79+
return LocalRedirect("/");
80+
}
81+
82+
protected virtual void ChangeCultureForUser(string cultureName)
83+
{
84+
var languageSetting = _settingStore.GetSettingOrNull(
85+
AbpSession.TenantId,
86+
AbpSession.GetUserId(),
87+
LocalizationSettingNames.DefaultLanguage
88+
);
89+
90+
if (languageSetting == null)
91+
{
92+
_settingStore.Create(new SettingInfo(
93+
AbpSession.TenantId,
94+
AbpSession.UserId,
95+
LocalizationSettingNames.DefaultLanguage,
96+
cultureName
97+
));
98+
}
99+
else
100+
{
101+
_settingStore.Update(new SettingInfo(
102+
AbpSession.TenantId,
103+
AbpSession.UserId,
104+
LocalizationSettingNames.DefaultLanguage,
105+
cultureName
106+
));
107+
}
108+
109+
_userSettingCache.Remove(AbpSession.ToUserIdentifier().ToString());
73110
}
74111
}
75112
}

0 commit comments

Comments
 (0)