Repository navigation
Expand file tree
/
Copy pathLocalizedEntityHelper.cs
More file actions
151 lines (129 loc) · 4.9 KB
/
Copy pathLocalizedEntityHelper.cs
File metadata and controls
151 lines (129 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Globalization;
using SmartStore.Core;
using SmartStore.Core.Domain.Localization;
using SmartStore.Core.Html;
using SmartStore.Services.Seo;
namespace SmartStore.Services.Localization
{
public class LocalizedEntityHelper
{
private readonly ILanguageService _languageService;
private readonly ILocalizedEntityService _localizedEntityService;
private readonly IUrlRecordService _urlRecordService;
private readonly IWorkContext _workContext;
private readonly int _languageCount;
private readonly Language _defaultLanguage;
public LocalizedEntityHelper(
ILanguageService languageService,
ILocalizedEntityService localizedEntityService,
IUrlRecordService urlRecordService,
IWorkContext workContext)
{
_languageService = languageService;
_localizedEntityService = localizedEntityService;
_urlRecordService = urlRecordService;
_workContext = workContext;
_languageCount = _languageService.GetLanguagesCount(false);
_defaultLanguage = _languageService.GetLanguageById(_languageService.GetDefaultLanguageId());
}
public LocalizedValue<TProp> GetLocalizedValue<T, TProp>(T obj,
int id, // T is BaseEntity = EntityId, T is ISetting = StoreId
string localeKeyGroup,
string localeKey,
Func<T, TProp> fallback,
object requestLanguageIdOrObj, // Id or Language
bool returnDefaultValue = true,
bool ensureTwoPublishedLanguages = true,
bool detectEmptyHtml = false)
where T : class
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
TProp result = default;
var str = string.Empty;
Language currentLanguage = null;
Language requestLanguage = null;
if (!(requestLanguageIdOrObj is Language))
{
if (requestLanguageIdOrObj is int requestLanguageId)
{
requestLanguage = _languageService.GetLanguageById(requestLanguageId);
}
}
else
{
requestLanguage = (Language)requestLanguageIdOrObj;
}
if (requestLanguage == null)
{
requestLanguage = _workContext.WorkingLanguage;
}
// Ensure that we have at least two published languages
var loadLocalizedValue = true;
if (ensureTwoPublishedLanguages)
{
loadLocalizedValue = _languageCount > 1;
}
// Localized value
if (loadLocalizedValue)
{
str = _localizedEntityService.GetLocalizedValue(requestLanguage.Id, id, localeKeyGroup, localeKey);
if (detectEmptyHtml && HtmlUtils.IsEmptyHtml(str))
{
str = string.Empty;
}
if (!string.IsNullOrEmpty(str))
{
currentLanguage = requestLanguage;
result = str.Convert<TProp>(CultureInfo.InvariantCulture);
}
}
// Set default value if required
if (returnDefaultValue && string.IsNullOrEmpty(str))
{
currentLanguage = _defaultLanguage;
result = fallback(obj);
}
if (currentLanguage == null)
{
currentLanguage = requestLanguage;
}
return new LocalizedValue<TProp>(result, requestLanguage, currentLanguage);
}
public string GetSeName(
string entityName,
int entityId,
int? languageId,
bool returnDefaultValue = true,
bool ensureTwoPublishedLanguages = true)
{
string result = string.Empty;
if (languageId == null)
{
languageId = _workContext.WorkingLanguage.Id;
}
if (languageId > 0)
{
// Ensure that we have at least two published languages
bool loadLocalizedValue = true;
if (ensureTwoPublishedLanguages)
{
loadLocalizedValue = _languageCount > 1;
}
// Localized value
if (loadLocalizedValue)
{
result = _urlRecordService.GetActiveSlug(entityId, entityName, languageId.Value);
}
}
// Set default value if required
if (String.IsNullOrEmpty(result) && returnDefaultValue)
{
result = _urlRecordService.GetActiveSlug(entityId, entityName, 0);
}
return result;
}
}
}