Skip to content
.NET 开发者指北.NET 开发者指北
CMS
.NET指北
FreeKit
Docker
关于
博客
github icon
    • FreeKit 指北
      • FreeKit 基础包
        • FreeKit Extras 扩展包
          • AspNetCore Identity FreeSql的实现
            • Email 邮件
              • Modularity 模块化
                • Localization 本地化
                  • IGeekFan.Localization.FreeSql
                    • LocalCulture(本地化语言)
                      • LocalResource(资源)
                        • 安装包
                          • 配置FreeSql服务
                            • 替换默认的IStringLocalizerFactory
                              • 中间件引用
                                • 增加一些默认数据

                              Localization 本地化

                              calendar icon2022年6月2日timer icon大约 1 分钟word icon约 414 字

                              此页内容
                              • IGeekFan.Localization.FreeSql
                              • LocalCulture(本地化语言)
                              • LocalResource(资源)
                              • 安装包
                              • 配置FreeSql服务
                              • 替换默认的IStringLocalizerFactory
                              • 中间件引用
                              • 增加一些默认数据

                              # Localization 本地化

                              # IGeekFan.Localization.FreeSql

                              基于数据库存储的本地化,可以让你的程序支持多语言,会创建二个表。

                              # LocalCulture(本地化语言)

                              类型名称长度
                              longId11
                              stringName50
                              stringDisplayName50

                              # LocalResource(资源)

                              类型名称长度
                              longId11
                              stringKey50
                              stringValue500
                              longCultureId11

                              可支持自定义长度,通过FluentAPI进行设置

                              # 安装包

                              dotnet add package IGeekFan.Localization.FreeSql
                              dotnet add package FreeSql.Provider.Sqlite
                              
                              1
                              2

                              # 配置FreeSql服务

                              • 定义扩展方法
                              public static IServiceCollection AddFreeSql(this IServiceCollection services, IConfiguration c)
                              {
                                  IFreeSql fsql = new FreeSqlBuilder()
                                              .UseConnectionString(DataType.Sqlite, c["ConnectionStrings:DefaultConnection"])
                                              .UseAutoSyncStructure(true)
                                              .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
                                              .UseMonitorCommand(
                                                  cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText)
                                              ).Build();
                              
                                  services.AddSingleton(fsql);
                              
                                  return services;
                              }
                              
                              1
                              2
                              3
                              4
                              5
                              6
                              7
                              8
                              9
                              10
                              11
                              12
                              13
                              14
                              • appsettings.json
                                "ConnectionStrings": {
                                  "DefaultConnection": "Data Source=|DataDirectory|\\web.db;"
                                }
                              
                              1
                              2
                              3
                              • Program.cs配置服务
                              builder.Services.AddFreeSql(c);
                              
                              1

                              # 替换默认的IStringLocalizerFactory

                              builder.Services.AddSingleton<IStringLocalizerFactory, FreeSqlStringLocalizerFactory>(); 
                              builder.Services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
                              
                              1
                              2

                              # 中间件引用

                              var supportedCultures = new List<CultureInfo>
                              {
                                  new("en-US"),
                                  new("ja-JP"),
                                  new("fr-FR"),
                                  new("zh-CN")
                              };
                              var options = new RequestLocalizationOptions
                              {
                                  DefaultRequestCulture = new RequestCulture("zh-CN"),
                                  SupportedCultures = supportedCultures,
                                  SupportedUICultures = supportedCultures
                              };
                              app.UseRequestLocalization(options);
                              
                              1
                              2
                              3
                              4
                              5
                              6
                              7
                              8
                              9
                              10
                              11
                              12
                              13
                              14

                              # 增加一些默认数据

                               public static class CodeFirstExtension
                                  {
                                      public static ICodeFirst SeedData(this ICodeFirst @this)
                                      {
                                          @this.Entity<LocalCulture>(e =>
                                          {
                                              e.HasData(new List<LocalCulture>()
                                                  {
                                                      new("en-US","en-US") ,
                                                      new("ja-JP","ja-JP")
                                                      {
                                                          Resources=new List<LocalResource>()
                                                          {
                                                              new("Hello","こんにちは"),
                                                              new("Request Localization Sample","リクエストローカライズ"),
                                                          }
                                                      },
                                                      new("fr-FR","fr-FR")
                                                      {
                                                          Resources=new List<LocalResource>()
                                                          {
                                                              new("Hello","Bonjour"),
                                                              new("Request Localization Sample","Demander un échantillon localisé"),
                                                          }
                                                      },
                                                      new("zh-CN","中文")
                                                      {
                                                          Resources=new List<LocalResource>()
                                                          {
                                                              new("Hello","你好"),
                                                              new("Request Localization Sample","请求资源示例"),
                                                          }
                                                      }
                                                  });
                                          });
                                          return @this;
                                      }
                                  }
                              
                              1
                              2
                              3
                              4
                              5
                              6
                              7
                              8
                              9
                              10
                              11
                              12
                              13
                              14
                              15
                              16
                              17
                              18
                              19
                              20
                              21
                              22
                              23
                              24
                              25
                              26
                              27
                              28
                              29
                              30
                              31
                              32
                              33
                              34
                              35
                              36
                              37
                              38

                              在fsql定义中增加此方法的引用

                              + fsql.CodeFirst.SeedData();
                              services.AddSingleton(fsql);
                              
                              1
                              2
                              [Route("[controller]/[action]")]
                              public class CultureController : Controller
                              {      
                                  private readonly ILogger<CultureController> _logger;
                                  private readonly IStringLocalizer<CultureController> _stringLocalizer;
                              
                                  public CultureController(ILogger<CultureController> logger, IStringLocalizer<CultureController> stringLocalizer)
                                  {
                                      _logger = logger;
                                      _stringLocalizer = stringLocalizer;
                                  }
                                  
                                  [HttpGet]
                                  public string Hello()
                                  {
                                      return _stringLocalizer["Hello"];
                                  } 
                              }
                              
                              1
                              2
                              3
                              4
                              5
                              6
                              7
                              8
                              9
                              10
                              11
                              12
                              13
                              14
                              15
                              16
                              17
                              18
                              • https://localhost:7273/Culture/Hello?culture=en-US&ui-culture=en-USopen in new window
                              • https://localhost:7273/Culture/Hello?culture=zh-CN&ui-culture=zh-CNopen in new window
                              edit icon在 GitHub 上编辑此页open in new window
                              上次编辑于: 2022/8/24 20:14:02
                              贡献者: igeekfan
                              上一页
                              Modularity 模块化
                              MIT Licensed | Copyright © 2021-present luoyunchong
                              苏ICP备16046457号-1

                              该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                              详情