Skip to content

Commit

Permalink
feat:增加abp的i18n本地化模块
Browse files Browse the repository at this point in the history
  • Loading branch information
whuanle committed Dec 9, 2023
1 parent 55b37a0 commit 07bd7d5
Show file tree
Hide file tree
Showing 28 changed files with 800 additions and 0 deletions.
62 changes: 62 additions & 0 deletions FreeRedis.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeRedis.DistributedCache"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "console_net8_cluster_client_side_caching", "examples\console_net8_cluster_client_side_caching\console_net8_cluster_client_side_caching.csproj", "{1C4387AA-C4BD-4388-97D1-3A769439705D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget", "nuget", "{1C5DEACA-4A4F-4C8F-999F-466B3B95CD18}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeRedis.Abp.Localization", "nuget\FreeRedis.Abp.Localization\FreeRedis.Abp.Localization.csproj", "{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "abpapi_net8", "examples\abpapi_net8\abpapi_net8.csproj", "{E6737319-1109-4645-94CD-43ED445CD7C8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "abpapi_net8_server", "examples\abpapi_net8_server\abpapi_net8_server.csproj", "{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "abpapi_net8_benchmark", "examples\abpapi_net8_benchmark\abpapi_net8_benchmark.csproj", "{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -225,6 +235,54 @@ Global
{1C4387AA-C4BD-4388-97D1-3A769439705D}.Release|x64.Build.0 = Release|Any CPU
{1C4387AA-C4BD-4388-97D1-3A769439705D}.Release|x86.ActiveCfg = Release|Any CPU
{1C4387AA-C4BD-4388-97D1-3A769439705D}.Release|x86.Build.0 = Release|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Debug|x64.ActiveCfg = Debug|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Debug|x64.Build.0 = Debug|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Debug|x86.ActiveCfg = Debug|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Debug|x86.Build.0 = Debug|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Release|Any CPU.Build.0 = Release|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Release|x64.ActiveCfg = Release|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Release|x64.Build.0 = Release|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Release|x86.ActiveCfg = Release|Any CPU
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9}.Release|x86.Build.0 = Release|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Debug|x64.ActiveCfg = Debug|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Debug|x64.Build.0 = Debug|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Debug|x86.ActiveCfg = Debug|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Debug|x86.Build.0 = Debug|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Release|Any CPU.Build.0 = Release|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Release|x64.ActiveCfg = Release|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Release|x64.Build.0 = Release|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Release|x86.ActiveCfg = Release|Any CPU
{E6737319-1109-4645-94CD-43ED445CD7C8}.Release|x86.Build.0 = Release|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Debug|x64.ActiveCfg = Debug|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Debug|x64.Build.0 = Debug|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Debug|x86.ActiveCfg = Debug|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Debug|x86.Build.0 = Debug|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Release|Any CPU.Build.0 = Release|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Release|x64.ActiveCfg = Release|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Release|x64.Build.0 = Release|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Release|x86.ActiveCfg = Release|Any CPU
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0}.Release|x86.Build.0 = Release|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Debug|x64.ActiveCfg = Debug|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Debug|x64.Build.0 = Debug|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Debug|x86.ActiveCfg = Debug|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Debug|x86.Build.0 = Debug|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Release|Any CPU.Build.0 = Release|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Release|x64.ActiveCfg = Release|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Release|x64.Build.0 = Release|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Release|x86.ActiveCfg = Release|Any CPU
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -246,6 +304,10 @@ Global
{7AC68251-83D4-423F-849C-04BD89F4BDFB} = {DD1426BF-F56F-49A8-9D4E-2BA0E3BEFC72}
{B0B0B55E-4D41-4419-83FE-309F72699A3A} = {D8045351-59E4-45B9-81F5-D21CC0996344}
{1C4387AA-C4BD-4388-97D1-3A769439705D} = {DD1426BF-F56F-49A8-9D4E-2BA0E3BEFC72}
{F7C7EEAA-DA3E-45CE-BBF6-2978179914E9} = {1C5DEACA-4A4F-4C8F-999F-466B3B95CD18}
{E6737319-1109-4645-94CD-43ED445CD7C8} = {DD1426BF-F56F-49A8-9D4E-2BA0E3BEFC72}
{6E98AC71-39C8-4EFB-ABB6-4B98C534BBD0} = {DD1426BF-F56F-49A8-9D4E-2BA0E3BEFC72}
{C9B345C2-E5AE-48C5-8055-8ED2EFF2C5D2} = {DD1426BF-F56F-49A8-9D4E-2BA0E3BEFC72}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B36AD060-F6AE-49C7-A310-B58B8467CE69}
Expand Down
42 changes: 42 additions & 0 deletions examples/abpapi_net8/ApiModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FreeRedis;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;

namespace abpapi_net8
{
[DependsOn(typeof(AbpLocalizationModule))]
public class ApiModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// 将 RedisClient 注册为单例
RedisClient redisClient = new RedisClient("127.0.0.1:6379,defaultDatabase=13");
LanguageRedisOptions redisOptions = new LanguageRedisOptions
{
KeyPrefix = "language",
Capacity = 20,
CheckExpired = TimeSpan.FromHours(1),
CheckNewLanguageExpired = TimeSpan.FromMinutes(1),
};
context.Services.AddSingleton<RedisClient>(redisClient);
context.Services.AddSingleton<LanguageRedisOptions>(redisOptions);


Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<ApiModule>();
});

Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Add<TestResource>("en")
// 注入 Redis 多语言
.AddFreeRedis(redisClient, redisOptions)
.AddVirtualJson("/Localization/Resources/Test");
});
}
}
}
49 changes: 49 additions & 0 deletions examples/abpapi_net8/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using FreeRedis;
using Microsoft.Extensions.Localization;
using Volo.Abp;
using Volo.Abp.Localization;

namespace abpapi_net8
{
internal class Program
{
static async Task Main(string[] args)
{
RedisClient redisClient = new RedisClient("127.0.0.1:6379,defaultDatabase=13");

await redisClient.HMSetAsync("language:en", new Dictionary<string, string>
{
{ "Hello", "Hello" },
{ "World", "World"},
});
await redisClient.HMSetAsync("language:zh-CN", new Dictionary<string, string>
{
{ "Hello", "你好" },
{ "World", "世界" },
});
await redisClient.HMSetAsync("language:zh", new Dictionary<string, string>
{
{ "Hello", "你好" },
{ "World", "世界" },
});

var provider = AbpApplicationFactory.Create<ApiModule>();
provider.Initialize();
var localizer = provider.ServiceProvider.GetRequiredService<IStringLocalizer<TestResource>>();
using (CultureHelper.Use("en"))
{
if (localizer["Hello"].Value != "Hello")
{
throw new Exception();
}
}
using (CultureHelper.Use("zh"))
{
if (localizer["Hello"].Value != "你好")
{
throw new Exception();
}
}
}
}
}
12 changes: 12 additions & 0 deletions examples/abpapi_net8/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"abpapi_net8": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:51294;http://localhost:51295"
}
}
}
6 changes: 6 additions & 0 deletions examples/abpapi_net8/TestResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace abpapi_net8
{
public class TestResource
{
}
}
18 changes: 18 additions & 0 deletions examples/abpapi_net8/abpapi_net8.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Volo.Abp.Localization" Version="7.4.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\nuget\FreeRedis.Abp.Localization\FreeRedis.Abp.Localization.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions examples/abpapi_net8_benchmark/Ben.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using abpapi_net8;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Volo.Abp;
using Volo.Abp.Localization;

namespace abpapi_net8_benchmark
{
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net80)]
//[SimpleJob(RuntimeMoniker.NativeAot70)]
// GC 分析
[MemoryDiagnoser]
// 线程体积
[ThreadingDiagnoser]
public partial class Ben
{
private IStringLocalizer<TestResource> _stringLocalizer;

[GlobalSetup]
public void Setup()
{
var application = AbpApplicationFactory.Create<ApiModule>();

application.Initialize();

_stringLocalizer = application.ServiceProvider.GetRequiredService<IStringLocalizer<TestResource>>();

var language = "zh";
using (CultureHelper.Use(language))
{
var value = _stringLocalizer["Hello"];
}
}


[Benchmark]
public void GetValue()
{
using (CultureHelper.Use("zh"))
{
var value = _stringLocalizer["Hello"];
}
}
}
}
14 changes: 14 additions & 0 deletions examples/abpapi_net8_benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using BenchmarkDotNet.Running;

namespace abpapi_net8_benchmark
{

internal class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Ben>();
Console.ReadLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net8.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
</PropertyGroup>
</Project>
20 changes: 20 additions & 0 deletions examples/abpapi_net8_benchmark/abpapi_net8_benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\abpapi_net8\ApiModule.cs" Link="ApiModule.cs" />
<Compile Include="..\abpapi_net8\TestResource.cs" Link="TestResource.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\nuget\FreeRedis.Abp.Localization\FreeRedis.Abp.Localization.csproj" />
</ItemGroup>
</Project>
80 changes: 80 additions & 0 deletions examples/abpapi_net8_server/AppModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;
using Volo.Abp;
using FreeRedis;
using Volo.Abp.Localization;
using Volo.Abp.VirtualFileSystem;

namespace abpapi_net8_server
{
[DependsOn(typeof(AbpAspNetCoreMvcModule), typeof(AbpLocalizationModule))]
public class AppModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// 将 RedisClient 注册为单例
RedisClient redisClient = new RedisClient("127.0.0.1:6379,defaultDatabase=13");

redisClient.HMSet("language:en", new Dictionary<string, string>
{
{ "Hello", "Hello" },
{ "World", "World"},
});
redisClient.HMSet("language:zh-CN", new Dictionary<string, string>
{
{ "Hello", "你好" },
{ "World", "世界" },
});
redisClient.HMSet("language:zh", new Dictionary<string, string>
{
{ "Hello", "你好" },
{ "World", "世界" },
});

LanguageRedisOptions redisOptions = new LanguageRedisOptions
{
KeyPrefix = "language",
Capacity = 20,
CheckExpired = TimeSpan.FromHours(1),
CheckNewLanguageExpired = TimeSpan.FromMinutes(1),
};
context.Services.AddSingleton<RedisClient>(redisClient);
context.Services.AddSingleton<LanguageRedisOptions>(redisOptions);

Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<AppModule>();
});

Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Add<TestResource>("en")
// 注入 Redis 多语言
.AddFreeRedis(redisClient, redisOptions);
});

context.Services.AddSwaggerGen();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();

// Configure the HTTP request pipeline.
if (env.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseAuthorization();

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseConfiguredEndpoints();
}
}
}
Loading

0 comments on commit 07bd7d5

Please sign in to comment.