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("createTime", createTime);
TrySetData(LocalizerKey, true);
TrySetData(CultureKey, culture ?? "en");
TrySetData(CultureKey, culture ?? CultureInfo.CurrentCulture.Name);
if (description != null)
{
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.Localization;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Settings;
using Volo.Abp.TextTemplating;
using Volo.Abp.Uow;
@ -43,6 +44,10 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary>
protected AbpNotificationsPublishOptions Options { get; }
/// <summary>
/// Reference to <see cref="ISettingProvider"/>.
/// </summary>
protected ISettingProvider SettingProvider { get; }
/// <summary>
/// Reference to <see cref="ICurrentTenant"/>.
/// </summary>
protected ICurrentTenant CurrentTenant { get; }
@ -96,6 +101,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
/// </summary>
public NotificationEventHandler(
ICurrentTenant currentTenant,
ISettingProvider settingProvider,
ITenantConfigurationCache tenantConfigurationCache,
IJsonSerializer jsonSerializer,
ITemplateRenderer templateRenderer,
@ -112,6 +118,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
Options = options.Value;
TenantConfigurationCache = tenantConfigurationCache;
CurrentTenant = currentTenant;
SettingProvider = settingProvider;
JsonSerializer = jsonSerializer;
TemplateRenderer = templateRenderer;
BackgroundJobManager = backgroundJobManager;
@ -138,7 +145,8 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
var culture = eventData.Data.Culture;
if (culture.IsNullOrWhiteSpace())
{
culture = CultureInfo.CurrentCulture.Name;
var cultureSet = await SettingProvider.GetOrNullAsync(LocalizationSettingNames.DefaultLanguage);
culture = cultureSet ?? CultureInfo.CurrentCulture.Name;
}
using (CultureHelper.Use(culture, culture))
{
@ -173,24 +181,32 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{
return;
}
if (notification.NotificationType == NotificationType.System)
var culture = eventData.Data.TryGetData(NotificationData.CultureKey)?.ToString();
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)
{
await SendToTenantAsync(activeTenant.Id, notification, eventData);
foreach (var activeTenant in allActiveTenants)
{
await SendToTenantAsync(activeTenant.Id, notification, eventData);
}
}
}
}
else
{
await SendToTenantAsync(eventData.TenantId, notification, eventData);
else
{
await SendToTenantAsync(eventData.TenantId, notification, eventData);
}
}
}
@ -247,7 +263,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
{ 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);
return;
@ -362,7 +378,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
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);
}
@ -468,7 +484,7 @@ namespace LY.MicroService.RealtimeMessage.EventBus.Distributed
subscriptionUsers.ToList(),
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);
}

Loading…
Cancel
Save