Browse Source

feat(notification): use default culture settings

pull/1319/head
colin 7 months ago
parent
commit
6d806f2fb7
  1. 2
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs
  2. 48
      aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs

2
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationData.cs

@ -71,7 +71,7 @@ public class NotificationData : IHasExtraProperties
TrySetData("formUser", formUser); TrySetData("formUser", formUser);
TrySetData("createTime", createTime); TrySetData("createTime", createTime);
TrySetData(LocalizerKey, true); TrySetData(LocalizerKey, true);
TrySetData(CultureKey, culture ?? "en"); TrySetData(CultureKey, culture ?? CultureInfo.CurrentCulture.Name);
if (description != null) if (description != null)
{ {
TrySetData("description", description); TrySetData("description", description);

48
aspnet-core/services/LY.MicroService.RealtimeMessage.HttpApi.Host/EventBus/Distributed/NotificationEventHandler.cs

@ -17,6 +17,7 @@ using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Json; using Volo.Abp.Json;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.Settings;
using Volo.Abp.TextTemplating; using Volo.Abp.TextTemplating;
using Volo.Abp.Uow; using Volo.Abp.Uow;
@ -43,6 +44,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary> /// </summary>
protected AbpNotificationsPublishOptions Options { get; } protected AbpNotificationsPublishOptions Options { get; }
/// <summary> /// <summary>
/// Reference to <see cref="ISettingProvider"/>.
/// </summary>
protected ISettingProvider SettingProvider { get; }
/// <summary>
/// Reference to <see cref="ICurrentTenant"/>. /// Reference to <see cref="ICurrentTenant"/>.
/// </summary> /// </summary>
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
@ -96,6 +101,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary> /// </summary>
public NotificationEventHandler( public NotificationEventHandler(
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
ISettingProvider settingProvider,
ITenantConfigurationCache tenantConfigurationCache, ITenantConfigurationCache tenantConfigurationCache,
IJsonSerializer jsonSerializer, IJsonSerializer jsonSerializer,
ITemplateRenderer templateRenderer, ITemplateRenderer templateRenderer,
@ -112,6 +118,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
Options = options.Value; Options = options.Value;
TenantConfigurationCache = tenantConfigurationCache; TenantConfigurationCache = tenantConfigurationCache;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
SettingProvider = settingProvider;
JsonSerializer = jsonSerializer; JsonSerializer = jsonSerializer;
TemplateRenderer = templateRenderer; TemplateRenderer = templateRenderer;
BackgroundJobManager = backgroundJobManager; BackgroundJobManager = backgroundJobManager;
@ -138,7 +145,8 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
var culture = eventData.Data.Culture; var culture = eventData.Data.Culture;
if (culture.IsNullOrWhiteSpace()) if (culture.IsNullOrWhiteSpace())
{ {
culture = CultureInfo.CurrentCulture.Name; var cultureSet = await SettingProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage);
culture = cultureSet ?? CultureInfo.CurrentCulture.Name;
} }
using (CultureHelper.Use(culture, culture)) using (CultureHelper.Use(culture, culture))
{ {
@ -173,24 +181,32 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{ {
return; return;
} }
var culture = eventData.Data.TryGetData(NotificationData.CultureKey)?.ToString();
if (notification.NotificationType == NotificationType.System) if (culture.IsNullOrWhiteSpace())
{ {
using (CurrentTenant.Change(null)) var cultureSet = await SettingProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage);
culture = cultureSet ?? CultureInfo.CurrentCulture.Name;
}
using (CultureHelper.Use(culture, culture))
{
if (notification.NotificationType == NotificationType.System)
{ {
await SendToTenantAsync(null, notification, eventData); using (CurrentTenant.Change(null))
{
await SendToTenantAsync(null, notification, eventData);
var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync(); var allActiveTenants = await TenantConfigurationCache.GetTenantsAsync();
foreach (var activeTenant in allActiveTenants) foreach (var activeTenant in allActiveTenants)
{ {
await SendToTenantAsync(activeTenant.Id, notification, eventData); await SendToTenantAsync(activeTenant.Id, notification, eventData);
}
} }
} }
} else
else {
{ await SendToTenantAsync(eventData.TenantId, notification, eventData);
await SendToTenantAsync(eventData.TenantId, notification, eventData); }
} }
} }
@ -247,7 +263,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{ NotificationKeywords.CreationTime, eventData.CreationTime.ToString(Options.DateTimeFormat) }, { NotificationKeywords.CreationTime, eventData.CreationTime.ToString(Options.DateTimeFormat) },
}); });
} }
catch(Exception ex) catch (Exception ex)
{ {
Logger.LogWarning("Formatting template notification failed, message will be discarded, cause :{message}", ex.Message); Logger.LogWarning("Formatting template notification failed, message will be discarded, cause :{message}", ex.Message);
return; return;
@ -362,7 +378,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
return userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName)); return userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName));
} }
catch(Exception ex) catch (Exception ex)
{ {
Logger.LogWarning("Failed to get user subscription, message will not be received by the user, reason: {message}", ex.Message); Logger.LogWarning("Failed to get user subscription, message will not be received by the user, reason: {message}", ex.Message);
} }
@ -468,7 +484,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
subscriptionUsers.ToList(), subscriptionUsers.ToList(),
notificationInfo.TenantId)); notificationInfo.TenantId));
} }
catch(Exception ex) catch (Exception ex)
{ {
Logger.LogWarning("Failed to push to background job, notification will be discarded, error cause: {message}", ex.Message); Logger.LogWarning("Failed to push to background job, notification will be discarded, error cause: {message}", ex.Message);
} }

Loading…
Cancel
Save