Skip to content

Commit 1abfdf5

Browse files
author
Leonid
committed
Fix localization
1 parent ee85bc6 commit 1abfdf5

15 files changed

+82
-64
lines changed

eFormAPI/eFormAPI.Web/Abstractions/ILocalizationService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
public interface ILocalizationService
44
{
55
string GetString(string key);
6-
string GetString(string key, string defaultValue);
7-
string GetString(string format, params object[] args);
6+
7+
string GetStringWithFormat(string format,
8+
params object[] args);
89
}
910
}

eFormAPI/eFormAPI.Web/Services/AccountService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public async Task<OperationResult> ResetAdminPassword(string code)
178178
string.Join(" ", addPasswordResult.Errors.Select(x=>x.Description).ToArray()));
179179
}
180180

181-
return new OperationResult(true, _localizationService.GetString("YourEmailPasswordHasBeenReset", user.Email));
181+
return new OperationResult(true, _localizationService.GetStringWithFormat("YourEmailPasswordHasBeenReset", user.Email));
182182
}
183183

184184
public async Task<OperationResult> ResetPassword(ResetPasswordModel model)

eFormAPI/eFormAPI.Web/Services/AdminService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public async Task<OperationResult> UpdateUser(UserRegisterModel userRegisterMode
131131
if (user == null)
132132
{
133133
return new OperationResult(false,
134-
_localizationService.GetString("UserNotFoundUserName", userRegisterModel.UserName));
134+
_localizationService.GetStringWithFormat("UserNotFoundUserName", userRegisterModel.UserName));
135135
}
136136

137137
var isAdmin = await _userManager.IsInRoleAsync(user, EformRole.Admin);
@@ -191,7 +191,7 @@ public async Task<OperationResult> UpdateUser(UserRegisterModel userRegisterMode
191191
}
192192

193193
return new OperationResult(true,
194-
_localizationService.GetString("UserUserNameWasUpdated", user.UserName));
194+
_localizationService.GetStringWithFormat("UserUserNameWasUpdated", user.UserName));
195195
}
196196
catch (Exception exception)
197197
{
@@ -208,7 +208,7 @@ public async Task<OperationResult> CreateUser(UserRegisterModel userRegisterMode
208208
if (userResult != null)
209209
{
210210
return new OperationResult(false,
211-
_localizationService.GetString("UserUserNameAlreadyExist", userRegisterModel.UserName));
211+
_localizationService.GetStringWithFormat("UserUserNameAlreadyExist", userRegisterModel.UserName));
212212
}
213213

214214
if (!_dbContext.SecurityGroups.Any(x => x.Id == userRegisterModel.GroupId))
@@ -248,7 +248,7 @@ public async Task<OperationResult> CreateUser(UserRegisterModel userRegisterMode
248248
}
249249

250250
return new OperationResult(true,
251-
_localizationService.GetString("UserUserNameWasCreated", user.UserName));
251+
_localizationService.GetStringWithFormat("UserUserNameWasCreated", user.UserName));
252252
}
253253
catch (Exception exception)
254254
{
@@ -275,7 +275,7 @@ public async Task<OperationResult> DeleteUser(int userId)
275275

276276
if (user == null)
277277
{
278-
return new OperationResult(false, _localizationService.GetString("UserUserNameNotFound", userId));
278+
return new OperationResult(false, _localizationService.GetStringWithFormat("UserUserNameNotFound", userId));
279279
}
280280

281281
var result = await _userManager.DeleteAsync(user);
@@ -284,7 +284,7 @@ public async Task<OperationResult> DeleteUser(int userId)
284284
return new OperationResult(false, string.Join(" ", result.Errors.Select(x=>x.Description).ToArray()));
285285
}
286286

287-
return new OperationResult(true, _localizationService.GetString("UserParamWasDeleted", userId));
287+
return new OperationResult(true, _localizationService.GetStringWithFormat("UserParamWasDeleted", userId));
288288
}
289289
catch (Exception exception)
290290
{

eFormAPI/eFormAPI.Web/Services/CasesService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using eFormData;
99
using eFormShared;
1010
using Microting.eFormApi.BasePn.Abstractions;
11-
using Microting.eFormApi.BasePn.Infrastructure.Helpers;
1211
using Microting.eFormApi.BasePn.Infrastructure.Models.API;
1312

1413
namespace eFormAPI.Web.Services
@@ -76,7 +75,7 @@ public OperationResult Delete(int id)
7675
var core = _coreHelper.GetCore();
7776

7877
return core.CaseDeleteResult(id)
79-
? new OperationResult(true, _localizationService.GetString("CaseParamDeletedSuccessfully", id))
78+
? new OperationResult(true, _localizationService.GetStringWithFormat("CaseParamDeletedSuccessfully", id))
8079
: new OperationResult(false, _localizationService.GetString("CaseCouldNotBeRemoved"));
8180
}
8281
catch (Exception)

eFormAPI/eFormAPI.Web/Services/EntitySearchService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public OperationResult CreateEntityGroup(AdvEntitySearchableGroupEditModel editM
6767
}
6868

6969
return new OperationResult(true,
70-
_localizationService.GetString("ParamCreatedSuccessfully", groupCreate.MicrotingUUID));
70+
_localizationService.GetStringWithFormat("ParamCreatedSuccessfully", groupCreate.MicrotingUUID));
7171
}
7272
catch (Exception)
7373
{
@@ -112,7 +112,7 @@ public OperationResult UpdateEntityGroup(AdvEntitySearchableGroupEditModel editM
112112
}
113113

114114
return new OperationResult(true,
115-
_localizationService.GetString("ParamUpdatedSuccessfully", editModel.GroupUid));
115+
_localizationService.GetStringWithFormat("ParamUpdatedSuccessfully", editModel.GroupUid));
116116
}
117117
catch (Exception)
118118
{
@@ -174,7 +174,7 @@ public OperationResult DeleteEntityGroup(string entityGroupUid)
174174

175175

176176
return core.EntityGroupDelete(entityGroupUid)
177-
? new OperationResult(true, _localizationService.GetString("ParamDeletedSuccessfully", entityGroupUid))
177+
? new OperationResult(true, _localizationService.GetStringWithFormat("ParamDeletedSuccessfully", entityGroupUid))
178178
: new OperationResult(false, _localizationService.GetString("ErrorWhenDeletingSearchableList"));
179179
}
180180
catch (Exception)
@@ -190,7 +190,7 @@ public OperationResult SendSearchableGroup(string entityGroupUid)
190190
var core = _coreHelper.GetCore();
191191

192192

193-
return new OperationResult(true, _localizationService.GetString("ParamDeletedSuccessfully", entityGroupUid));
193+
return new OperationResult(true, _localizationService.GetStringWithFormat("ParamDeletedSuccessfully", entityGroupUid));
194194
}
195195
catch (Exception)
196196
{

eFormAPI/eFormAPI.Web/Services/EntitySelectService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public OperationResult CreateEntityGroup(AdvEntitySelectableGroupEditModel editM
6767
}
6868

6969
return new OperationResult(true,
70-
_localizationService.GetString("ParamCreatedSuccessfully", groupCreate.MicrotingUUID));
70+
_localizationService.GetStringWithFormat("ParamCreatedSuccessfully", groupCreate.MicrotingUUID));
7171
}
7272
catch (Exception)
7373
{
@@ -114,7 +114,7 @@ public OperationResult UpdateEntityGroup(AdvEntitySelectableGroupEditModel editM
114114
}
115115

116116
return new OperationResult(true,
117-
_localizationService.GetString("ParamUpdatedSuccessfully", editModel.GroupUid));
117+
_localizationService.GetStringWithFormat("ParamUpdatedSuccessfully", editModel.GroupUid));
118118
}
119119
catch (Exception)
120120
{
@@ -175,7 +175,7 @@ public OperationResult DeleteEntityGroup(string entityGroupUid)
175175

176176

177177
return core.EntityGroupDelete(entityGroupUid)
178-
? new OperationResult(true, _localizationService.GetString("ParamDeletedSuccessfully", entityGroupUid))
178+
? new OperationResult(true, _localizationService.GetStringWithFormat("ParamDeletedSuccessfully", entityGroupUid))
179179
: new OperationResult(false, _localizationService.GetString("ErrorWhileDeletingSelectableList"));
180180
}
181181
catch (Exception)
@@ -192,7 +192,7 @@ public OperationResult SendSearchableGroup(string entityGroupUid)
192192
var core = _coreHelper.GetCore();
193193

194194

195-
return new OperationResult(true, _localizationService.GetString("ParamDeletedSuccessfully", entityGroupUid));
195+
return new OperationResult(true, _localizationService.GetStringWithFormat("ParamDeletedSuccessfully", entityGroupUid));
196196
}
197197
catch (Exception)
198198
{
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
24
using Castle.Core.Internal;
35
using eFormAPI.Web.Abstractions;
6+
using JetBrains.Annotations;
47
using Microsoft.Extensions.Localization;
58

69
namespace eFormAPI.Web.Services
@@ -15,31 +18,36 @@ public LocalizationService(IStringLocalizerFactory factory)
1518
Assembly.GetEntryAssembly().FullName);
1619
}
1720

18-
public string GetString(string key)
21+
public string GetString([NotNull] string key)
1922
{
20-
//If changed, weird stuff happens
23+
if (key.IsNullOrEmpty())
24+
{
25+
return key;
26+
}
2127
var str = _localizer[key];
2228
return str.Value;
2329
}
2430

25-
public string GetString(string format, params object[] args)
31+
public string GetStringWithFormat([NotNull] string format,
32+
params object[] args)
2633
{
27-
//If changed, weird stuff happens
34+
if (format.IsNullOrEmpty())
35+
{
36+
return format;
37+
}
38+
2839
var message = _localizer[format];
2940
if (message?.Value == null)
3041
{
3142
return null;
3243
}
33-
return string.Format(message.Value, args);
34-
}
3544

36-
public string GetString(string key, string defaultValue)
37-
{
38-
if (key.IsNullOrEmpty())
45+
if (args != null && args.Any())
3946
{
40-
return defaultValue;
47+
return string.Format(message.Value, args);
4148
}
42-
return GetString(key);
49+
50+
return message.Value;
4351
}
4452
}
4553
}

eFormAPI/eFormAPI.Web/Services/MenuService.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Castle.Core.Internal;
56
using eFormAPI.Web.Abstractions;
67
using eFormAPI.Web.Abstractions.Security;
78
using eFormAPI.Web.Infrastructure;
@@ -65,7 +66,9 @@ public async Task<OperationDataResult<MenuModel>> GetCurrentUserMenu()
6566
.OrderBy(p => p.Position)
6667
.Select(p => new MenuItemModel()
6768
{
68-
Name = _localizationService.GetString(p.LocaleName, p.Name),
69+
Name = p.LocaleName.IsNullOrEmpty()
70+
? p.Name
71+
: _localizationService.GetString(p.LocaleName),
6972
Position = p.Position,
7073
E2EId = p.E2EId,
7174
Link = p.Link,
@@ -74,7 +77,9 @@ public async Task<OperationDataResult<MenuModel>> GetCurrentUserMenu()
7477
.OrderBy(c => c.Position)
7578
.Select(x => new MenuItemModel()
7679
{
77-
Name = _localizationService.GetString(x.LocaleName, x.Name),
80+
Name = x.LocaleName.IsNullOrEmpty()
81+
? x.Name
82+
: _localizationService.GetString(x.LocaleName),
7883
Position = x.Position,
7984
Link = x.Link,
8085
E2EId = x.E2EId
@@ -87,7 +92,9 @@ public async Task<OperationDataResult<MenuModel>> GetCurrentUserMenu()
8792
.OrderBy(p => p.Position)
8893
.Select(p => new MenuItemModel()
8994
{
90-
Name = _localizationService.GetString(p.LocaleName, p.Name),
95+
Name = p.LocaleName.IsNullOrEmpty()
96+
? p.Name
97+
: _localizationService.GetString(p.LocaleName),
9198
Position = p.Position,
9299
E2EId = p.E2EId,
93100
Link = p.Link,
@@ -96,7 +103,9 @@ public async Task<OperationDataResult<MenuModel>> GetCurrentUserMenu()
96103
.OrderBy(c => c.Position)
97104
.Select(x => new MenuItemModel()
98105
{
99-
Name = _localizationService.GetString(x.LocaleName, x.Name),
106+
Name = x.LocaleName.IsNullOrEmpty()
107+
? x.Name
108+
: _localizationService.GetString(x.LocaleName),
100109
Position = x.Position,
101110
Link = x.Link,
102111
E2EId = x.E2EId

eFormAPI/eFormAPI.Web/Services/SimpleSitesService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public OperationResult Create(SimpleSiteModel simpleSiteModel)
4141

4242
return siteDto != null
4343
? new OperationResult(true,
44-
_localizationService.GetString("DeviceUserParamCreatedSuccessfully", siteDto.SiteName))
44+
_localizationService.GetStringWithFormat("DeviceUserParamCreatedSuccessfully", siteDto.SiteName))
4545
: new OperationResult(false, _localizationService.GetString("DeviceUserCouldNotBeCreated"));
4646
}
4747
catch (Exception ex)
@@ -72,7 +72,7 @@ public OperationDataResult<Site_Dto> Edit(int id)
7272
return siteDto != null
7373
? new OperationDataResult<Site_Dto>(true, siteDto)
7474
: new OperationDataResult<Site_Dto>(false,
75-
_localizationService.GetString("DeviceUserParamCouldNotBeEdited", id));
75+
_localizationService.GetStringWithFormat("DeviceUserParamCouldNotBeEdited", id));
7676
}
7777

7878
public OperationResult Update(SimpleSiteModel simpleSiteModel)
@@ -93,7 +93,7 @@ public OperationResult Update(SimpleSiteModel simpleSiteModel)
9393
return isUpdated
9494
? new OperationResult(true, _localizationService.GetString("DeviceUserUpdatedSuccessfully"))
9595
: new OperationResult(false,
96-
_localizationService.GetString("DeviceUserParamCouldNotBeUpdated", simpleSiteModel.Id));
96+
_localizationService.GetStringWithFormat("DeviceUserParamCouldNotBeUpdated", simpleSiteModel.Id));
9797
}
9898

9999
return new OperationResult(false, _localizationService.GetString("DeviceUserCouldNotBeObtained"));
@@ -116,13 +116,13 @@ public OperationResult Delete(int id)
116116

117117
return core.SiteDelete(siteNameDto.SiteUId)
118118
? new OperationResult(true,
119-
_localizationService.GetString("DeviceUserParamDeletedSuccessfully", siteNameDto.SiteName))
119+
_localizationService.GetStringWithFormat("DeviceUserParamDeletedSuccessfully", siteNameDto.SiteName))
120120
: new OperationResult(false,
121-
_localizationService.GetString("DeviceUserParamCouldNotBeDeleted", siteNameDto.SiteName));
121+
_localizationService.GetStringWithFormat("DeviceUserParamCouldNotBeDeleted", siteNameDto.SiteName));
122122
}
123123
catch (Exception)
124124
{
125-
return new OperationResult(false, _localizationService.GetString("DeviceUserParamCouldNotBeDeleted", id));
125+
return new OperationResult(false, _localizationService.GetStringWithFormat("DeviceUserParamCouldNotBeDeleted", id));
126126
}
127127
}
128128
}

eFormAPI/eFormAPI.Web/Services/SitesService.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using eFormAPI.Web.Infrastructure.Models;
66
using eFormShared;
77
using Microting.eFormApi.BasePn.Abstractions;
8-
using Microting.eFormApi.BasePn.Infrastructure.Models;
98
using Microting.eFormApi.BasePn.Infrastructure.Models.API;
109

1110
namespace eFormAPI.Web.Services
@@ -58,7 +57,7 @@ public OperationResult Update(SiteNameModel siteNameModel)
5857
catch (Exception)
5958
{
6059
return new OperationResult(false,
61-
_localizationService.GetString("SiteParamCouldNotBeUpdated", siteNameModel.Id));
60+
_localizationService.GetStringWithFormat("SiteParamCouldNotBeUpdated", siteNameModel.Id));
6261
}
6362
}
6463

@@ -71,20 +70,20 @@ public OperationResult Delete(int id)
7170

7271
if (siteDto.Equals(null))
7372
{
74-
return new OperationResult(false, _localizationService.GetString("SiteParamNotFound", id));
73+
return new OperationResult(false, _localizationService.GetStringWithFormat("SiteParamNotFound", id));
7574
}
7675

7776
return core.Advanced_SiteItemDelete(id)
7877
? new OperationResult(true,
79-
_localizationService.GetString("SiteParamDeletedSuccessfully", siteDto.SiteName))
78+
_localizationService.GetStringWithFormat("SiteParamDeletedSuccessfully", siteDto.SiteName))
8079
: new OperationResult(false,
81-
_localizationService.GetString("SiteParamCouldNotBeDeleted", siteDto.SiteName));
80+
_localizationService.GetStringWithFormat("SiteParamCouldNotBeDeleted", siteDto.SiteName));
8281
}
8382

8483
catch (Exception)
8584
{
8685
return new OperationDataResult<SiteNameModel>(false,
87-
_localizationService.GetString("SiteParamCouldNotBeDeleted", id));
86+
_localizationService.GetStringWithFormat("SiteParamCouldNotBeDeleted", id));
8887
}
8988
}
9089
}

0 commit comments

Comments
 (0)