当前位置: 首页 > article >正文

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

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

如图所示,前端使用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/Login
     public 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.kler.cn/a/415770.html

相关文章:

  • springboot旅游管理系统的设计与实现
  • 《C++助力无监督学习:挖掘数据潜在结构的高效之道》
  • 计算机网络基础(2):网络安全/ 网络通信介质
  • 查看k8s集群中pod和node的资源使用情况
  • Dapper简易入门
  • 【uniapp】轮播图
  • Ubuntu安装不同版本的opencv,并任意切换使用
  • 从单一设备到万物互联:鸿蒙生态崛起的未来之路
  • 【0346】Postgres内核 Startup Process 通过 signal 与 postmaster 交互实现 (5)
  • 代码随想录算法训练营第五十七天|Day57 图论
  • python学习——字典的创建与删除
  • 容器第二天(day039)
  • 05.ES9 06.ES10
  • 深入理解Rust的生命周期
  • ArcGIS 软件中路网数据的制作
  • Argon2-cffi与argon2-cffi-bindings:深入理解及其应用
  • SOLIDWORKS英文,怎么修改成中文
  • 【嵌入式C语言】关键字之类型修饰符
  • 高效数据集成:旺店通与金蝶云自动化处理调拨出库单
  • 网工日记:NAT相关概念
  • Linux系统 异常控制流
  • MacBook上安装 Windows 10 后,System 进程 CPU 占用 100% 的问题
  • 架构-微服务-服务配置
  • 2025 - AIDD - python的autodock vina 批量分子对接改进版本2.0-全自动对接,完全全自动对接
  • 微积分复习笔记 Calculus Volume 1 - 6.9 Calculus of the Hyperbolic Functions
  • Java的常识