-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathI18nTest.cs
95 lines (85 loc) · 3.79 KB
/
I18nTest.cs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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;
[TestClass]
// ReSharper disable once InconsistentNaming
public class I18nTest
{
private const string DEFAULT_RESOURCE = "Resources/I18n";
[TestInitialize]
public void Initialize()
{
I18nResourceResourceConfiguration.Resources = new();
}
[DataTestMethod]
[DataRow("zh-CN", "吉姆")]
[DataRow("en-US", "JIM")]
public void Test(string cultureName, string expectedValue)
{
var appId = "appid";
var configObjectPrefix = "Culture";
var key = "key";
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)
});
var configuration = configurationBuilder.Build();
masaConfiguration.Setup(config => config.ConfigurationApi.Get(appId)).Returns(configuration);
services.AddSingleton(masaConfiguration.Object);
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);
}
[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);
}
}