From 663a6c7f5c7ab17e3353e9d44ac993dadd586ed5 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 16 Oct 2024 10:40:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=9D=99=E6=80=81=E5=9B=BD?= =?UTF-8?q?=E9=99=85=E5=8C=96+=E6=B5=8B=E8=AF=95ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/Progranm.cs | 4 +++ IRaCIS.Core.Application/TestService.cs | 14 +++++++-- .../Repository/IRepository.cs | 21 ++----------- .../Repository/Repository.cs | 6 ++-- .../_IRaCIS/Globalization/I18n.cs | 31 +++++++++++++++++++ 5 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 IRaCIS.Core.Infrastructure/_IRaCIS/Globalization/I18n.cs diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index 48e5a45dd..c030e8517 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Localization; using Newtonsoft.Json; using Serilog; using System; @@ -259,6 +260,9 @@ var hangfireJobService = app.Services.GetRequiredService(); await hangfireJobService.InitHangfireJobTaskAsync(); +var localizer = app.Services.GetRequiredService(); +I18n.SetLocalizer(localizer); + try { #region 运行环境 部署平台 diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 419a86ebb..e85e65479 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -28,7 +28,6 @@ using NPOI.XWPF.UserModel; using System.Globalization; using System.Runtime.InteropServices; using System.Text; -using Tea; namespace IRaCIS.Core.Application.Service @@ -57,12 +56,23 @@ namespace IRaCIS.Core.Application.Service } [AllowAnonymous] - public IResponseOutput GetTest(Guid trialId,int num, TestModel testModel) + public IResponseOutput GetTest() { //throw new BusinessValidationFailedException("手动抛出的异常"); return ResponseOutput.Ok(_userInfo.IP); } + + public IResponseOutput GetTestI18n() + { + var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US; + + //CultureInfo.CurrentCulture = new CultureInfo(StaticData.CultureInfo.en_US); + //CultureInfo.CurrentUICulture = new CultureInfo(StaticData.CultureInfo.en_US); + + return ResponseOutput.Ok(I18n.T("TaskAllocation_DoctorConfigExists")); + } + } diff --git a/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs b/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs index bd33d1a93..ddecbf4d5 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/IRepository.cs @@ -71,23 +71,9 @@ namespace IRaCIS.Core.Infra.EFCore bool autoSave = false, bool ignoreQueryFilter = false) where T : Entity; } - public class Repository : IRepository + public class Repository(IRaCISDBContext _dbContext, IMapper _mapper, IUserInfo _userInfo) : IRepository { - #region 构造 基本 - private IRaCISDBContext _dbContext { get; } - public IMapper _mapper { get; set; } - - public IUserInfo _userInfo { get; set; } - - public IStringLocalizer _localizer { get; set; } - - public Repository(IRaCISDBContext dbContext, IMapper mapper, IUserInfo userInfo, IStringLocalizer localizer) - { - _localizer = localizer; - _dbContext = dbContext; - _mapper = mapper; - _userInfo = userInfo; - } + /// /// 设置是使用哪个仓储 默认不跟踪 @@ -171,7 +157,7 @@ namespace IRaCIS.Core.Infra.EFCore if (dbEntity == null) { - throw new BusinessValidationFailedException(_localizer["Repository_UpdateError"]); + throw new BusinessValidationFailedException(I18n.T("Repository_UpdateError")); } var dbBeforEntity = dbEntity.Clone(); @@ -450,7 +436,6 @@ namespace IRaCIS.Core.Infra.EFCore } - #endregion diff --git a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs index d27e4021f..98b87f116 100644 --- a/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs +++ b/IRaCIS.Core.Infra.EFCore/Repository/Repository.cs @@ -100,7 +100,7 @@ namespace IRaCIS.Core.Infra.EFCore if (dbEntity == null) { - throw new BusinessValidationFailedException(_localizer["Repository_UpdateError"]); + throw new BusinessValidationFailedException(I18n.T("Repository_UpdateError")); } var dbBeforEntity = dbEntity.Clone(); @@ -156,7 +156,7 @@ namespace IRaCIS.Core.Infra.EFCore if (searchEntity == null) { - throw new BusinessValidationFailedException(_localizer["Repository_UpdateError"]); + throw new BusinessValidationFailedException(I18n.T("Repository_UpdateError")); } _dbContext.EntityModifyPartialFiled(searchEntity, updateFactory); @@ -208,7 +208,7 @@ namespace IRaCIS.Core.Infra.EFCore if (waitDelete == null) { - throw new BusinessValidationFailedException(_localizer["Repository_DeleteError"]); + throw new BusinessValidationFailedException(I18n.T("Repository_DeleteError")); } await DeleteAsync(waitDelete, autoSave); diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Globalization/I18n.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Globalization/I18n.cs new file mode 100644 index 000000000..58e54d4d4 --- /dev/null +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Globalization/I18n.cs @@ -0,0 +1,31 @@ +using Microsoft.Extensions.Localization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + namespace IRaCIS.Core.Infrastructure.Extention; + +public static class I18n +{ + private static IStringLocalizer _localizer; + + public static void SetLocalizer(IStringLocalizer localizer) + { + _localizer = localizer; + } + + + + public static string T(string key) + { + return _localizer[key]; + } + + public static string T(string key, params object[] arguments) + { + return _localizer[key, arguments]; + } + +}