Skip to content

Commit 91fb0eb

Browse files
committed
nopSolutions#4149 Bulk insert delete plugin locale resources
1 parent d328edc commit 91fb0eb

File tree

18 files changed

+582
-834
lines changed

18 files changed

+582
-834
lines changed

src/Libraries/Nop.Services/Localization/ILocalizationService.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,33 @@ void SaveLocalizedSetting<TSettings>(TSettings settings, Expression<Func<TSettin
179179
/// <param name="languageCulture">Language culture code. If null or empty, then a resource will be added for all languages</param>
180180
void AddOrUpdatePluginLocaleResource(string resourceName, string resourceValue, string languageCulture = null);
181181

182+
/// <summary>
183+
/// Add locale resources
184+
/// </summary>
185+
/// <param name="resources">Resource name-value pairs</param>
186+
/// <param name="languageId">Language identifier; pass null to add the passed resources for all languages</param>
187+
void AddPluginLocaleResource(IDictionary<string, string> resources, int? languageId = null);
188+
182189
/// <summary>
183190
/// Delete a locale resource
184191
/// </summary>
185192
/// <param name="resourceName">Resource name</param>
186193
void DeletePluginLocaleResource(string resourceName);
187194

195+
/// <summary>
196+
/// Delete locale resources
197+
/// </summary>
198+
/// <param name="resourceNames">Resource names</param>
199+
/// <param name="languageId">Language identifier; pass null to delete the passed resources from all languages</param>
200+
void DeletePluginLocaleResources(IList<string> resourceNames, int? languageId = null);
201+
202+
/// <summary>
203+
/// Delete locale resources by the passed name prefix
204+
/// </summary>
205+
/// <param name="resourceNamePrefix">Resource name prefix</param>
206+
/// <param name="languageId">Language identifier; pass null to delete resources by prefix from all languages</param>
207+
void DeletePluginLocaleResources(string resourceNamePrefix, int? languageId = null);
208+
188209
/// <summary>
189210
/// Get localized friendly name of a plugin
190211
/// </summary>

src/Libraries/Nop.Services/Localization/LocalizationService.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public partial class LocalizationService : ILocalizationService
3838
private readonly IStaticCacheManager _staticCacheManager;
3939
private readonly IWorkContext _workContext;
4040
private readonly LocalizationSettings _localizationSettings;
41-
41+
4242
#endregion
4343

4444
#region Ctor
@@ -132,15 +132,16 @@ protected virtual void UpdateLocaleStringResources(IList<LocaleStringResource> r
132132
using (var xmlReader = XmlReader.Create(xmlStreamReader))
133133
while (xmlReader.ReadToFollowing("Language"))
134134
{
135-
if (xmlReader.NodeType != XmlNodeType.Element)
135+
if (xmlReader.NodeType != XmlNodeType.Element)
136136
continue;
137137

138138
using var languageReader = xmlReader.ReadSubtree();
139139
while (languageReader.ReadToFollowing("LocaleResource"))
140140
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.GetAttribute("Name") is string name)
141141
{
142142
using var lrReader = languageReader.ReadSubtree();
143-
if (lrReader.ReadToFollowing("Value") && lrReader.NodeType == XmlNodeType.Element) result.Add((name, lrReader.ReadString()));
143+
if (lrReader.ReadToFollowing("Value") && lrReader.NodeType == XmlNodeType.Element)
144+
result.Add((name, lrReader.ReadString()));
144145
}
145146

146147
break;
@@ -295,7 +296,7 @@ orderby l.ResourceName
295296

296297
//performance optimization of the site startup
297298
key = _cacheKeyService.PrepareKeyForDefaultCache(
298-
loadPublicLocales.Value ? NopLocalizationDefaults.LocaleStringResourcesAllPublicCacheKey :NopLocalizationDefaults.LocaleStringResourcesAllAdminCacheKey,
299+
loadPublicLocales.Value ? NopLocalizationDefaults.LocaleStringResourcesAllPublicCacheKey : NopLocalizationDefaults.LocaleStringResourcesAllAdminCacheKey,
299300
languageId);
300301

301302
return _staticCacheManager.Get(key, () =>
@@ -706,6 +707,29 @@ public virtual void AddOrUpdatePluginLocaleResource(string resourceName, string
706707
}
707708
}
708709

710+
/// <summary>
711+
/// Add locale resources
712+
/// </summary>
713+
/// <param name="resources">Resource name-value pairs</param>
714+
/// <param name="languageId">Language identifier; pass null to add the passed resources for all languages</param>
715+
public virtual void AddPluginLocaleResource(IDictionary<string, string> resources, int? languageId = null)
716+
{
717+
//first delete all previous locales with the passed names if they exist
718+
DeletePluginLocaleResources(resources.Keys.ToList(), languageId);
719+
720+
//insert new locale resources
721+
var locales = _languageService.GetAllLanguages(true)
722+
.Where(language => !languageId.HasValue || language.Id == languageId.Value)
723+
.SelectMany(language => resources.Select(resource => new LocaleStringResource
724+
{
725+
LanguageId = language.Id,
726+
ResourceName = resource.Key,
727+
ResourceValue = resource.Value
728+
}))
729+
.ToList();
730+
_lsrRepository.Insert(locales);
731+
}
732+
709733
/// <summary>
710734
/// Delete a locale resource
711735
/// </summary>
@@ -720,6 +744,29 @@ public virtual void DeletePluginLocaleResource(string resourceName)
720744
}
721745
}
722746

747+
/// <summary>
748+
/// Delete locale resources
749+
/// </summary>
750+
/// <param name="resourceNames">Resource names</param>
751+
/// <param name="languageId">Language identifier; pass null to delete the passed resources from all languages</param>
752+
public virtual void DeletePluginLocaleResources(IList<string> resourceNames, int? languageId = null)
753+
{
754+
_lsrRepository.Delete(locale => (!languageId.HasValue || locale.LanguageId == languageId.Value) &&
755+
resourceNames.Contains(locale.ResourceName, StringComparer.InvariantCultureIgnoreCase));
756+
}
757+
758+
/// <summary>
759+
/// Delete locale resources by the passed name prefix
760+
/// </summary>
761+
/// <param name="resourceNamePrefix">Resource name prefix</param>
762+
/// <param name="languageId">Language identifier; pass null to delete resources by prefix from all languages</param>
763+
public virtual void DeletePluginLocaleResources(string resourceNamePrefix, int? languageId = null)
764+
{
765+
_lsrRepository.Delete(locale => (!languageId.HasValue || locale.LanguageId == languageId.Value) &&
766+
!string.IsNullOrEmpty(locale.ResourceName) &&
767+
locale.ResourceName.StartsWith(resourceNamePrefix, StringComparison.InvariantCultureIgnoreCase));
768+
}
769+
723770
/// <summary>
724771
/// Get localized friendly name of a plugin
725772
/// </summary>

src/Plugins/Nop.Plugin.DiscountRules.CustomerRoles/CustomerRoleDiscountRequirementRule.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Microsoft.AspNetCore.Mvc;
45
using Microsoft.AspNetCore.Mvc.Infrastructure;
@@ -85,8 +86,8 @@ public DiscountRequirementValidationResult CheckRequirement(DiscountRequirementV
8586
public string GetConfigurationUrl(int discountId, int? discountRequirementId)
8687
{
8788
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
88-
89-
return urlHelper.Action("Configure", "DiscountRulesCustomerRoles",
89+
90+
return urlHelper.Action("Configure", "DiscountRulesCustomerRoles",
9091
new { discountId = discountId, discountRequirementId = discountRequirementId }, _webHelper.CurrentRequestProtocol);
9192
}
9293

@@ -96,9 +97,12 @@ public string GetConfigurationUrl(int discountId, int? discountRequirementId)
9697
public override void Install()
9798
{
9899
//locales
99-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole", "Required customer role");
100-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Hint", "Discount will be applied if customer is in the selected customer role.");
101-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Select", "Select customer role");
100+
_localizationService.AddPluginLocaleResource(new Dictionary<string, string>
101+
{
102+
["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole"] = "Required customer role",
103+
["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Hint"] = "Discount will be applied if customer is in the selected customer role.",
104+
["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Select"] = "Select customer role"
105+
});
102106

103107
base.Install();
104108
}
@@ -117,9 +121,7 @@ public override void Uninstall()
117121
}
118122

119123
//locales
120-
_localizationService.DeletePluginLocaleResource("Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole");
121-
_localizationService.DeletePluginLocaleResource("Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Hint");
122-
_localizationService.DeletePluginLocaleResource("Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Select");
124+
_localizationService.DeletePluginLocaleResources("Plugins.DiscountRules.CustomerRoles");
123125

124126
base.Uninstall();
125127
}

src/Plugins/Nop.Plugin.ExternalAuth.Facebook/FacebookAuthenticationMethod.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Nop.Core;
1+
using System.Collections.Generic;
2+
using Nop.Core;
23
using Nop.Services.Authentication.External;
34
using Nop.Services.Configuration;
45
using Nop.Services.Localization;
@@ -60,11 +61,14 @@ public override void Install()
6061
_settingService.SaveSetting(new FacebookExternalAuthSettings());
6162

6263
//locales
63-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientKeyIdentifier", "App ID/API Key");
64-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientKeyIdentifier.Hint", "Enter your app ID/API key here. You can find it on your FaceBook application page.");
65-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientSecret", "App Secret");
66-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientSecret.Hint", "Enter your app secret here. You can find it on your FaceBook application page.");
67-
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.ExternalAuth.Facebook.Instructions", "<p>To configure authentication with Facebook, please follow these steps:<br/><br/><ol><li>Navigate to the <a href=\"https://developers.facebook.com/apps\" target =\"_blank\" > Facebook for Developers</a> page and sign in. If you don't already have a Facebook account, use the <b>Sign up for Facebook</b> link on the login page to create one.</li><li>Tap the <b>+ Add a New App button</b> in the upper right corner to create a new App ID. (If this is your first app with Facebook, the text of the button will be <b>Create a New App</b>.)</li><li>Fill out the form and tap the <b>Create App ID button</b>.</li><li>The <b>Product Setup</b> page is displayed, letting you select the features for your new app. Click <b>Get Started</b> on <b>Facebook Login</b>.</li><li>Click the <b>Settings</b> link in the menu at the left, you are presented with the <b>Client OAuth Settings</b> page with some defaults already set.</li><li>Enter \"{0:s}signin-facebook\" into the <b>Valid OAuth Redirect URIs</b> field.</li><li>Click <b>Save Changes</b>.</li><li>Click the <b>Dashboard</b> link in the left navigation.</li><li>Copy your App ID and App secret below.</li></ol><br/><br/></p>");
64+
_localizationService.AddPluginLocaleResource(new Dictionary<string, string>
65+
{
66+
["Plugins.ExternalAuth.Facebook.ClientKeyIdentifier"] = "App ID/API Key",
67+
["Plugins.ExternalAuth.Facebook.ClientKeyIdentifier.Hint"] = "Enter your app ID/API key here. You can find it on your FaceBook application page.",
68+
["Plugins.ExternalAuth.Facebook.ClientSecret"] = "App Secret",
69+
["Plugins.ExternalAuth.Facebook.ClientSecret.Hint"] = "Enter your app secret here. You can find it on your FaceBook application page.",
70+
["Plugins.ExternalAuth.Facebook.Instructions"] = "<p>To configure authentication with Facebook, please follow these steps:<br/><br/><ol><li>Navigate to the <a href=\"https://developers.facebook.com/apps\" target =\"_blank\" > Facebook for Developers</a> page and sign in. If you don't already have a Facebook account, use the <b>Sign up for Facebook</b> link on the login page to create one.</li><li>Tap the <b>+ Add a New App button</b> in the upper right corner to create a new App ID. (If this is your first app with Facebook, the text of the button will be <b>Create a New App</b>.)</li><li>Fill out the form and tap the <b>Create App ID button</b>.</li><li>The <b>Product Setup</b> page is displayed, letting you select the features for your new app. Click <b>Get Started</b> on <b>Facebook Login</b>.</li><li>Click the <b>Settings</b> link in the menu at the left, you are presented with the <b>Client OAuth Settings</b> page with some defaults already set.</li><li>Enter \"{0:s}signin-facebook\" into the <b>Valid OAuth Redirect URIs</b> field.</li><li>Click <b>Save Changes</b>.</li><li>Click the <b>Dashboard</b> link in the left navigation.</li><li>Copy your App ID and App secret below.</li></ol><br/><br/></p>"
71+
});
6872

6973
base.Install();
7074
}
@@ -78,11 +82,7 @@ public override void Uninstall()
7882
_settingService.DeleteSetting<FacebookExternalAuthSettings>();
7983

8084
//locales
81-
_localizationService.DeletePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientKeyIdentifier");
82-
_localizationService.DeletePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientKeyIdentifier.Hint");
83-
_localizationService.DeletePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientSecret");
84-
_localizationService.DeletePluginLocaleResource("Plugins.ExternalAuth.Facebook.ClientSecret.Hint");
85-
_localizationService.DeletePluginLocaleResource("Plugins.ExternalAuth.Facebook.Instructions");
85+
_localizationService.DeletePluginLocaleResources("Plugins.ExternalAuth.Facebook");
8686

8787
base.Uninstall();
8888
}

0 commit comments

Comments
 (0)