Skip to content

Commit 97edccf

Browse files
authored
feat: Dcc Support multi-resource i18n (#381)
* feat: Support multi-resource i18n addition * feat(i18n): key is not case sensitive * feat: Dcc Support multi-resource i18n
1 parent 6c3d412 commit 97edccf

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

src/Contrib/Globalization/Masa.Contrib.Globalization.I18n.Dcc/Extensions/I18nResourceExtensions.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@
55

66
namespace Masa.BuildingBlocks.Globalization.I18n;
77

8+
// ReSharper disable once InconsistentNaming
89
public static class I18nResourceExtensions
910
{
11+
// ReSharper disable once InconsistentNaming
12+
public static I18nResource UseDcc(
13+
this I18nResource i18nResource,
14+
string appId,
15+
string configObjectPrefix,
16+
params CultureModel[] supportedCultures)
17+
=> i18nResource.UseDcc(appId, configObjectPrefix, supportedCultures.ToList());
18+
19+
// ReSharper disable once InconsistentNaming
1020
public static I18nResource UseDcc(
1121
this I18nResource i18nResource,
1222
string appId,
@@ -15,8 +25,10 @@ public static I18nResource UseDcc(
1525
{
1626
var serviceProvider = MasaApp.GetServices().BuildServiceProvider();
1727
var masaConfiguration = serviceProvider.GetRequiredService<IMasaConfiguration>();
18-
var contributors = supportedCultures
19-
.Select(supportedCulture => new DccI18nResourceContributor(appId, configObjectPrefix, supportedCulture.Culture, masaConfiguration)).ToList();
28+
var contributors =
29+
(supportedCultures.Any() ? supportedCultures : serviceProvider.GetRequiredService<IOptions<CultureSettings>>().Value.SupportedCultures)
30+
.Select(supportedCulture => new DccI18nResourceContributor(appId, configObjectPrefix, supportedCulture.Culture, masaConfiguration))
31+
.ToList();
2032
foreach (var contributor in contributors)
2133
{
2234
i18nResource.AddContributor(contributor.CultureName, contributor);

src/Contrib/Globalization/Masa.Contrib.Globalization.I18n.Dcc/_Imports.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
global using Masa.Contrib.Globalization.I18n.Dcc;
99
global using Microsoft.Extensions.Configuration;
1010
global using Microsoft.Extensions.DependencyInjection;
11+
global using Microsoft.Extensions.Options;
1112
global using System.Diagnostics.CodeAnalysis;
1213
global using DccConstant = Masa.Contrib.Globalization.I18n.Dcc.Internal.Constant;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Globalization.I18n.Dcc.Tests;
5+
6+
public class CustomResource
7+
{
8+
9+
}

src/Contrib/Globalization/Tests/Masa.Contrib.Globalization.I18n.Dcc.Tests/I18nTest.cs

+46
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Masa.Contrib.Globalization.I18n.Dcc.Tests;
55

66
[TestClass]
7+
// ReSharper disable once InconsistentNaming
78
public class I18nTest
89
{
910
private const string DEFAULT_RESOURCE = "Resources/I18n";
@@ -46,4 +47,49 @@ public void Test(string cultureName, string expectedValue)
4647
var value = i18n.T(key);
4748
Assert.AreEqual(expectedValue, value);
4849
}
50+
51+
[DataTestMethod]
52+
[DataRow("appid", "appid2", "zh-CN", "key", "吉姆")]
53+
[DataRow("appid", "appid2", "en-US", "key", "JIM")]
54+
public void Test2(string appId, string appId2, string cultureName, string key, string expectedValue)
55+
{
56+
var configObjectPrefix = "Culture";
57+
var services = new ServiceCollection();
58+
MasaApp.SetServiceCollection(services);
59+
Mock<IMasaConfiguration> masaConfiguration = new();
60+
var configurationBuilder = new ConfigurationBuilder();
61+
configurationBuilder.AddInMemoryCollection(new List<KeyValuePair<string, string>>()
62+
{
63+
new($"{configObjectPrefix}.{cultureName}:{key}", expectedValue),
64+
new($"{configObjectPrefix}2.{cultureName}:{key}", $"{expectedValue}2")
65+
});
66+
var configuration = configurationBuilder.Build();
67+
masaConfiguration.Setup(config => config.ConfigurationApi.Get(appId)).Returns(configuration);
68+
masaConfiguration.Setup(config => config.ConfigurationApi.Get(appId2)).Returns(configuration);
69+
services.AddSingleton(masaConfiguration.Object);
70+
71+
services.Configure<MasaI18nOptions>(options =>
72+
{
73+
options.Resources.Add<CustomResource>().UseDcc(appId2, $"{configObjectPrefix}2");
74+
});
75+
services.AddI18n(options =>
76+
{
77+
options.ResourcesDirectory = DEFAULT_RESOURCE;
78+
}, options => options.UseDcc(appId, configObjectPrefix));
79+
80+
MasaApp.SetServiceCollection(services);
81+
82+
var serviceProvider = services.BuildServiceProvider();
83+
var i18n = serviceProvider.GetService<II18n>();
84+
Assert.IsNotNull(i18n);
85+
i18n.SetUiCulture(cultureName);
86+
var value = i18n.T(key);
87+
Assert.AreEqual(expectedValue, value);
88+
89+
var customI18n = serviceProvider.GetService<II18n<CustomResource>>();
90+
Assert.IsNotNull(customI18n);
91+
customI18n.SetUiCulture(cultureName);
92+
var value2 = customI18n.T(key);
93+
Assert.AreEqual($"{expectedValue}2", value2);
94+
}
4995
}

0 commit comments

Comments
 (0)