Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Dcc Support multi-resource i18n #381

Merged
merged 5 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@

namespace Masa.BuildingBlocks.Globalization.I18n;

// ReSharper disable once InconsistentNaming
public static class I18nResourceExtensions
{
// ReSharper disable once InconsistentNaming
public static I18nResource UseDcc(
this I18nResource i18nResource,
string appId,
string configObjectPrefix,
params CultureModel[] supportedCultures)
=> i18nResource.UseDcc(appId, configObjectPrefix, supportedCultures.ToList());

// ReSharper disable once InconsistentNaming
public static I18nResource UseDcc(
this I18nResource i18nResource,
string appId,
Expand All @@ -15,8 +25,10 @@ public static I18nResource UseDcc(
{
var serviceProvider = MasaApp.GetServices().BuildServiceProvider();
var masaConfiguration = serviceProvider.GetRequiredService<IMasaConfiguration>();
var contributors = supportedCultures
.Select(supportedCulture => new DccI18nResourceContributor(appId, configObjectPrefix, supportedCulture.Culture, masaConfiguration)).ToList();
var contributors =
(supportedCultures.Any() ? supportedCultures : serviceProvider.GetRequiredService<IOptions<CultureSettings>>().Value.SupportedCultures)
.Select(supportedCulture => new DccI18nResourceContributor(appId, configObjectPrefix, supportedCulture.Culture, masaConfiguration))
.ToList();
foreach (var contributor in contributors)
{
i18nResource.AddContributor(contributor.CultureName, contributor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
global using Masa.Contrib.Globalization.I18n.Dcc;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Options;
global using System.Diagnostics.CodeAnalysis;
global using DccConstant = Masa.Contrib.Globalization.I18n.Dcc.Internal.Constant;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Globalization.I18n.Dcc.Tests;

public class CustomResource
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Masa.Contrib.Globalization.I18n.Dcc.Tests;

[TestClass]
// ReSharper disable once InconsistentNaming
public class I18nTest
{
private const string DEFAULT_RESOURCE = "Resources/I18n";
Expand Down Expand Up @@ -46,4 +47,49 @@ public void Test(string cultureName, string expectedValue)
var value = i18n.T(key);
Assert.AreEqual(expectedValue, value);
}

[DataTestMethod]
[DataRow("appid", "appid2", "zh-CN", "key", "吉姆")]
[DataRow("appid", "appid2", "en-US", "key", "JIM")]
public void Test2(string appId, string appId2, string cultureName, string key, string expectedValue)
{
var configObjectPrefix = "Culture";
var services = new ServiceCollection();
MasaApp.SetServiceCollection(services);
Mock<IMasaConfiguration> masaConfiguration = new();
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(new List<KeyValuePair<string, string>>()
{
new($"{configObjectPrefix}.{cultureName}:{key}", expectedValue),
new($"{configObjectPrefix}2.{cultureName}:{key}", $"{expectedValue}2")
});
var configuration = configurationBuilder.Build();
masaConfiguration.Setup(config => config.ConfigurationApi.Get(appId)).Returns(configuration);
masaConfiguration.Setup(config => config.ConfigurationApi.Get(appId2)).Returns(configuration);
services.AddSingleton(masaConfiguration.Object);

services.Configure<MasaI18nOptions>(options =>
{
options.Resources.Add<CustomResource>().UseDcc(appId2, $"{configObjectPrefix}2");
});
services.AddI18n(options =>
{
options.ResourcesDirectory = DEFAULT_RESOURCE;
}, options => options.UseDcc(appId, configObjectPrefix));

MasaApp.SetServiceCollection(services);

var serviceProvider = services.BuildServiceProvider();
var i18n = serviceProvider.GetService<II18n>();
Assert.IsNotNull(i18n);
i18n.SetUiCulture(cultureName);
var value = i18n.T(key);
Assert.AreEqual(expectedValue, value);

var customI18n = serviceProvider.GetService<II18n<CustomResource>>();
Assert.IsNotNull(customI18n);
customI18n.SetUiCulture(cultureName);
var value2 = customI18n.T(key);
Assert.AreEqual($"{expectedValue}2", value2);
}
}