net9 abp vnext 多语言通过数据库动态管理

devtools/2024/11/30 15:17:30/

通过数据库加载实现动态管理,用户可以自己修改界面显示的文本,满足国际化需求

如图所示,前端使用tdesign vnext

新建表TSYS_Localization与TSYS_LocalizationDetail

国旗图标下载网址flag-icons: Free Country Flags in SVG

在Shared下创建下图3个文件

 [Table("TSYS_LocalizationDetail")]public class LocalizedResource{public long Id { get; set; }public long CultureId { get; set; }        [FreeSql.DataAnnotations.Navigate(nameof(CultureId))]public virtual LocalizedCulture? Culture { get; set; }public string Type { get; set; }public string Key { get; set; }public string Value { get; set; }public string Enable { get; set; }public DateTime CreationTime { get; set; }[FreeSql.DataAnnotations.JsonMap]public ExtraPropertyDictionary ExtraProperties { get; set; } = new ExtraPropertyDictionary();//public string LocalizedCultureName { get; set; }public LocalizedString GetLocalizedString(string cultureName){return new LocalizedString(Key, Value);}}[Table("TSYS_Localization")]public class LocalizedCulture{private string isDefault;public long Id { get; set; }        public string Culture { get; set; }public string CultureName { get; set; }public string Icon { get; set; }public bool IsDefault { get => isDefault == "Y" ? true : false; set => isDefault = value ? "Y" : "N"; }}
public static class DatabaseLocalizationExtensions
{public static LocalizationResourceBase AddDatabase([NotNull] this LocalizationResourceBase localizationResource, string resourceName){Check.NotNull(localizationResource, nameof(localizationResource));localizationResource.Contributors.Add(new DatabaseLocalizationResourceContributor(resourceName));return localizationResource;}
}
 public class DatabaseLocalizationResourceContributor : ILocalizationResourceContributor{private IFreeSql _freeSql;private readonly string _resourceName = null;public bool IsDynamic => false;private Dictionary<string, Dictionary<string, LocalizedString>> _resources = new Dictionary<string, Dictionary<string, LocalizedString>>();public DatabaseLocalizationResourceContributor(string resourceName = null){_resourceName = resourceName;}public void Initialize(LocalizationResourceInitializationContext context){_freeSql = context.ServiceProvider.GetRequiredService<IFreeSql>();var cultureName= CultureInfo.CurrentCulture.Name;if (cultureName!=null){var dic = _freeSql.Select<LocalizedResource>().Include(t => t.Culture).Where(t => t.Culture.Culture == cultureName).ToDictionary(t => string.IsNullOrEmpty(t.Type) ? t.Key : $"{t.Type}.{t.Key}", t => new LocalizedString(t.Key, t.Value));if (dic.Count>0){_resources.Add(cultureName, dic);}}            }public LocalizedString GetOrNull(string cultureName, string name){if (!_resources.ContainsKey(cultureName)){var dic = _freeSql.Select<LocalizedResource>().Include(t => t.Culture).Where(t => t.Culture.Culture == cultureName).ToDictionary(t => string.IsNullOrEmpty(t.Type) ? t.Key : $"{t.Type}.{t.Key}", t => new LocalizedString(t.Key, t.Value));if (dic.Count > 0 && !_resources.ContainsKey(cultureName)){_resources.Add(cultureName, dic);}}if (_resources.ContainsKey(cultureName) && _resources[cultureName].ContainsKey(name)){return _resources[cultureName][name];}else{if (!_freeSql.Select<LocalizedResource>().Any(t => t.Key == name)){var localization = _freeSql.Queryable<LocalizedCulture>().ToList();List<LocalizedResource> localizedResources = new List<LocalizedResource>();foreach (var item in localization){LocalizedResource localizedResource = new LocalizedResource();localizedResource.Id = YitIdHelper.NextId();localizedResource.Key = name;localizedResource.CultureId = item.Id;localizedResource.Value = name;localizedResource.Type = "";localizedResource.CreationTime = DateTime.Now;localizedResource.Enable = "Y";localizedResources.Add(localizedResource);}_freeSql.Insert<LocalizedResource>(localizedResources).ExecuteAffrows();}                if (cultureName.StartsWith("zh")){                 return new LocalizedString(name, name);}else{return new LocalizedString(name, name);}}}public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary){if (!_resources.ContainsKey(cultureName)){_resources.Add(cultureName, dictionary);}else{foreach (var item in dictionary){if (_resources[cultureName].ContainsKey(item.Key)){_resources[cultureName][item.Key] = item.Value;}else{_resources[cultureName].Add(item.Key, item.Value);}}}}//调用登录时接口时触发https://localhost:44356/Account/Loginpublic async Task FillAsync(string cultureName, Dictionary<string, LocalizedString> dictionary){if (!_resources.ContainsKey(cultureName)){_resources.Add(cultureName, dictionary);}else{foreach (var item in dictionary){if (_resources[cultureName].ContainsKey(item.Key)){_resources[cultureName][item.Key] = item.Value; }else{_resources[cultureName].Add(item.Key, item.Value);}}}}public Task<IEnumerable<string>> GetSupportedCulturesAsync(){var cultures=_freeSql.Select<LocalizedCulture>().ToList(t => t.Culture).AsEnumerable<string>();return Task.FromResult(cultures);}}

前端更改语言时调用后端加载多语言json

合并json使用lodash库

import merge from 'lodash/merge';

后台接口数据示例


http://www.ppmy.cn/devtools/138231.html

相关文章

VUE前端实现天爱滑块验证码--详细教程

第一步&#xff1a; Git地址&#xff1a;tianai-captcha-demo: 滑块验证码demo 找到目录 src/main/resources/static,拷贝 static 并改名为 tac 即可。 第二步&#xff1a; 将改为 tac 的文件&#xff0c;放进项目根目录中&#xff0c;如下图&#xff1a; 第三步&#xff1…

【RAG多模态】mR^2AG:基于知识的多模态检索-反思增强生成方法浅尝

在基于知识的视觉问答&#xff08;Knowledge-based VQA&#xff09;任务中&#xff0c;输入通常是一对图像和问题 ( I , Q ) (I, Q) (I,Q)&#xff0c;并且依赖于可访问的知识库。文章提出的 m R 2 A G mR^2AG mR2AG框架通过两个新颖的反思操作来解耦生成过程&#xff0c;从而…

解决水库安全监测难题 长期无外接电源 低功耗设备智能化监测系统

解决水库安全监测难题 长期无外接电源 低功耗设备智能化监测系统 国内某水库安全监测项目需要监测点分散&#xff0c;且无外接供电。项目年限为4年&#xff0c;不允许使用太阳能电板。因此&#xff0c;我们需要设备具备低功耗且内置电池的功能。为了满足客户的要求&#xff0c;…

TiDB 架构

整体架构 与传统的单机数据库相比&#xff0c;TiDB 具有以下优势&#xff1a; 纯分布式架构&#xff0c;拥有良好的扩展性&#xff0c;支持弹性的扩缩容支持 SQL&#xff0c;对外暴露 MySQL 的网络协议&#xff0c;并兼容大多数 MySQL 的语法&#xff0c;在大多数场景下可以直…

k8s 1.28 聚合层部署信息记录

–requestheader-client-ca-file –requestheader-allowed-namesfront-proxy-client –requestheader-extra-headers-prefixX-Remote-Extra- –requestheader-group-headersX-Remote-Group –requestheader-username-headersX-Remote-User –proxy-client-cert-file –proxy-cl…

初识Hive

初识Hive 什么是 Hive&#xff1f; Hive 是基于 Hadoop 的数据仓库工具&#xff0c;用于处理存储在 Hadoop 上的结构化和半结构化数据。Hive 可以让用户通过 SQL 类似的语言&#xff08;HiveQL&#xff09;来查询和分析数据&#xff0c;而不需要了解复杂的 MapReduce 编程模型…

Tomcat(39)如何在Tomcat中配置SSL会话缓存?

在Tomcat中配置SSL会话缓存是优化SSL/TLS性能的一个重要步骤&#xff0c;尤其是在处理大量并发SSL连接时。通过会话缓存&#xff0c;可以避免重复的SSL握手过程&#xff0c;从而减少CPU使用和提高响应速度。以下是在Tomcat中配置SSL会话缓存的详细步骤。 1. 配置server.xml S…

2017年

B C A B B D B 度 16*23*44*32m 得出 m4(m为其他顶点) 顶点个数43411 折半查找的判定二叉树 左多 or左右一样 B B D