|
4 | 4 | namespace Masa.Contrib.Globalization.I18n.Dcc.Tests;
|
5 | 5 |
|
6 | 6 | [TestClass]
|
| 7 | +// ReSharper disable once InconsistentNaming |
7 | 8 | public class I18nTest
|
8 | 9 | {
|
9 | 10 | private const string DEFAULT_RESOURCE = "Resources/I18n";
|
@@ -46,4 +47,49 @@ public void Test(string cultureName, string expectedValue)
|
46 | 47 | var value = i18n.T(key);
|
47 | 48 | Assert.AreEqual(expectedValue, value);
|
48 | 49 | }
|
| 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 | + } |
49 | 95 | }
|
0 commit comments