aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs
blob: 3b8fe5ca60d8a4341025a1a6a3ff9e4c5140f770 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using BitFaster.Caching;
using Emby.Server.Implementations.Localization;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Localization
{
    public class LocalizationManagerTests
    {
        [Fact]
        public void GetCountries_All_Success()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "de-DE"
            });
            var countries = localizationManager.GetCountries().ToList();

            Assert.Equal(140, countries.Count);

            var germany = countries.FirstOrDefault(x => x.Name.Equals("DE", StringComparison.Ordinal));
            Assert.NotNull(germany);
            Assert.Equal("Germany", germany!.DisplayName);
            Assert.Equal("DEU", germany.ThreeLetterISORegionName);
            Assert.Equal("DE", germany.TwoLetterISORegionName);
        }

        [Fact]
        public async Task GetCultures_All_Success()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "de-DE"
            });
            await localizationManager.LoadAll();
            var cultures = localizationManager.GetCultures().ToList();

            Assert.Equal(496, cultures.Count);

            var germany = cultures.FirstOrDefault(x => x.TwoLetterISOLanguageName.Equals("de", StringComparison.Ordinal));
            Assert.NotNull(germany);
            Assert.Equal("deu", germany!.ThreeLetterISOLanguageName);
            Assert.Equal("German", germany.DisplayName);
            Assert.Equal("German", germany.Name);
            Assert.Contains("deu", germany.ThreeLetterISOLanguageNames);
            Assert.Contains("ger", germany.ThreeLetterISOLanguageNames);
        }

        [Fact]
        public async Task TryGetISO6392TFromB_Success()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "de-DE"
            });
            await localizationManager.LoadAll();

            string? isoT;

            // Translation ger -> deu
            Assert.True(localizationManager.TryGetISO6392TFromB("ger", out isoT));
            Assert.Equal("deu", isoT);

            // chi -> zho
            Assert.True(localizationManager.TryGetISO6392TFromB("chi", out isoT));
            Assert.Equal("zho", isoT);

            // eng is already ISO 639-2/T
            Assert.False(localizationManager.TryGetISO6392TFromB("eng", out isoT));
            Assert.Null(isoT);
        }

        [Theory]
        [InlineData("de")]
        [InlineData("deu")]
        [InlineData("ger")]
        [InlineData("german")]
        public async Task FindLanguageInfo_Valid_Success(string identifier)
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "de-DE"
            });
            await localizationManager.LoadAll();

            var germany = localizationManager.FindLanguageInfo(identifier);
            Assert.NotNull(germany);

            Assert.Equal("deu", germany!.ThreeLetterISOLanguageName);
            Assert.Equal("German", germany.DisplayName);
            Assert.Equal("German", germany.Name);
            Assert.Contains("deu", germany.ThreeLetterISOLanguageNames);
            Assert.Contains("ger", germany.ThreeLetterISOLanguageNames);
        }

        [Theory]
        [InlineData("mul", "Multiple languages")]
        [InlineData("und", "Undetermined")]
        [InlineData("mis", "Uncoded languages")]
        [InlineData("zxx", "No linguistic content; Not applicable")]
        public async Task FindLanguageInfo_ISO6392Only_Success(string code, string expectedDisplayName)
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "en-US"
            });
            await localizationManager.LoadAll();

            var culture = localizationManager.FindLanguageInfo(code);
            Assert.NotNull(culture);
            Assert.Equal(expectedDisplayName, culture.DisplayName);
            Assert.Equal(code, culture.ThreeLetterISOLanguageName);
        }

        [Fact]
        public async Task GetParentalRatings_Default_Success()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "de-DE"
            });
            await localizationManager.LoadAll();
            var ratings = localizationManager.GetParentalRatings().ToList();

            Assert.Equal(56, ratings.Count);

            var tvma = ratings.FirstOrDefault(x => x.Name.Equals("TV-MA", StringComparison.Ordinal));
            Assert.NotNull(tvma);
            Assert.Equal(17, tvma!.RatingScore!.Score);
        }

        [Fact]
        public async Task GetParentalRatings_ConfiguredCountryCode_Success()
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                MetadataCountryCode = "DE"
            });
            await localizationManager.LoadAll();
            var ratings = localizationManager.GetParentalRatings().ToList();

            Assert.Equal(24, ratings.Count);

            var fsk = ratings.FirstOrDefault(x => x.Name.Equals("FSK-12", StringComparison.Ordinal));
            Assert.NotNull(fsk);
            Assert.Equal(12, fsk!.RatingScore!.Score);
        }

        [Theory]
        [InlineData("CA-R", "CA", 18, 1)]
        [InlineData("FSK-16", "DE", 16, null)]
        [InlineData("FSK-18", "DE", 18, null)]
        [InlineData("FSK-18", "US", 18, null)]
        [InlineData("TV-MA", "US", 17, 1)]
        [InlineData("XXX", "asdf", 1000, null)]
        [InlineData("Germany: FSK-18", "DE", 18, null)]
        [InlineData("Rated : R", "US", 17, 0)]
        [InlineData("Rated: R", "US", 17, 0)]
        [InlineData("Rated R", "US", 17, 0)]
        [InlineData(" PG-13 ", "US", 13, 0)]
        public async Task GetRatingLevel_GivenValidString_Success(string value, string countryCode, int? expectedScore, int? expectedSubScore)
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                MetadataCountryCode = countryCode
            });
            await localizationManager.LoadAll();
            var score = localizationManager.GetRatingScore(value);
            Assert.NotNull(score);
            Assert.Equal(expectedScore, score.Score);
            Assert.Equal(expectedSubScore, score.SubScore);
        }

        [Theory]
        [InlineData("0", 0, null)]
        [InlineData("1", 1, null)]
        [InlineData("6", 6, null)]
        [InlineData("12", 12, null)]
        [InlineData("42", 42, null)]
        [InlineData("9999", 9999, null)]
        public async Task GetRatingLevel_GivenValidAge_Success(string value, int? expectedScore, int? expectedSubScore)
        {
            var localizationManager = Setup(new ServerConfiguration { MetadataCountryCode = "nl" });
            await localizationManager.LoadAll();
            var score = localizationManager.GetRatingScore(value);
            Assert.NotNull(score);
            Assert.Equal(expectedScore, score.Score);
            Assert.Equal(expectedSubScore, score.SubScore);
        }

        [Fact]
        public async Task GetRatingLevel_GivenUnratedString_Success()
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                UICulture = "de-DE"
            });
            await localizationManager.LoadAll();
            Assert.Null(localizationManager.GetRatingScore("NR"));
            Assert.Null(localizationManager.GetRatingScore("unrated"));
            Assert.Null(localizationManager.GetRatingScore("Not Rated"));
            Assert.Null(localizationManager.GetRatingScore("n/a"));
        }

        [Theory]
        [InlineData("-NO RATING SHOWN-")]
        [InlineData(":NO RATING SHOWN:")]
        public async Task GetRatingLevel_Split_Success(string value)
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                UICulture = "en-US"
            });
            await localizationManager.LoadAll();

            Assert.Null(localizationManager.GetRatingScore(value));
        }

        [Theory]
        [InlineData("TV-MA", "DE", 17, 1)] // US-only rating, DE country code
        [InlineData("PG-13", "FR", 13, 0)] // US-only rating, FR country code
        [InlineData("R", "JP", 17, 0)] // US-only rating, JP country code
        public async Task GetRatingScore_FallbackPrioritizesUS_Success(string rating, string countryCode, int expectedScore, int? expectedSubScore)
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                MetadataCountryCode = countryCode
            });
            await localizationManager.LoadAll();

            var score = localizationManager.GetRatingScore(rating);

            Assert.NotNull(score);
            Assert.Equal(expectedScore, score.Score);
            Assert.Equal(expectedSubScore, score.SubScore);
        }

        [Theory]
        [InlineData("US:INVALID", "US")] // Colon separator, known country code, unknown rating
        [InlineData("us:INVALID", "US")] // Colon separator, lowercase country code
        [InlineData("DE-INVALID", "US")] // Hyphen separator, known language prefix, unknown rating
        [InlineData("ca:INVALID", "US")] // Colon separator, known country code (Canada)
        public async Task GetRatingScore_UnknownRatingWithKnownCountry_ReturnsNull(string rating, string countryCode)
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                MetadataCountryCode = countryCode
            });
            await localizationManager.LoadAll();

            Assert.Null(localizationManager.GetRatingScore(rating));
        }

        [Theory]
        [InlineData("us:R", "DE", 17, 0)] // Colon separator, explicit US country, valid US rating
        [InlineData("US:PG-13", "DE", 13, 0)] // Colon separator, explicit US country, valid US rating
        [InlineData("ca:R", "US", 18, 1)] // Colon separator, Canada country code, valid CA rating
        public async Task GetRatingScore_ValidRatingWithCountrySeparator_ReturnsScore(string rating, string countryCode, int expectedScore, int? expectedSubScore)
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                MetadataCountryCode = countryCode
            });
            await localizationManager.LoadAll();

            var score = localizationManager.GetRatingScore(rating);
            Assert.NotNull(score);
            Assert.Equal(expectedScore, score.Score);
            Assert.Equal(expectedSubScore, score.SubScore);
        }

        [Theory]
        [InlineData("Default", "Default")]
        [InlineData("HeaderLiveTV", "Live TV")]
        public void GetLocalizedString_Valid_Success(string key, string expected)
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                UICulture = "en-US"
            });

            var translated = localizationManager.GetLocalizedString(key);
            Assert.NotNull(translated);
            Assert.Equal(expected, translated);
        }

        [Fact]
        public void GetLocalizedString_Invalid_Success()
        {
            var localizationManager = Setup(new ServerConfiguration()
            {
                UICulture = "en-US"
            });

            var key = "SuperInvalidTranslationKeyThatWillNeverBeAdded";

            var translated = localizationManager.GetLocalizedString(key);
            Assert.NotNull(translated);
            Assert.Equal(key, translated);
        }

        [Fact]
        public void GetLocalizedString_WithCulture_ReturnsTranslation()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "en-US"
            });

            var translated = localizationManager.GetLocalizedString("Artists", "de");
            Assert.Equal("Interpreten", translated);
        }

        [Fact]
        public void GetLocalizedString_WithCulture_FallsBackToEnUs()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "en-US"
            });

            // A culture with no translation file should fall back to en-US
            var translated = localizationManager.GetLocalizedString("Artists", "zz");
            Assert.Equal("Artists", translated);
        }

        [Fact]
        public void GetLocalizedString_WithBcp47Normalization_ReturnsTranslation()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "en-US"
            });

            // es-419 is stored as es_419 in Jellyfin
            var translated = localizationManager.GetLocalizedString("Default", "es-419");
            Assert.NotEqual("Default", translated);
        }

        [Fact]
        public void GetServerLocalizedString_UsesServerCulture()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "de"
            });

            // Even if CurrentUICulture is fr, GetServerLocalizedString should use the server's "de"
            var previousCulture = CultureInfo.CurrentUICulture;
            try
            {
                CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
                var translated = localizationManager.GetServerLocalizedString("Artists");
                Assert.Equal("Interpreten", translated);
            }
            finally
            {
                CultureInfo.CurrentUICulture = previousCulture;
            }
        }

        [Fact]
        public void GetLocalizedString_UsesCurrentUICulture()
        {
            var localizationManager = Setup(new ServerConfiguration
            {
                UICulture = "en-US"
            });

            var previousCulture = CultureInfo.CurrentUICulture;
            try
            {
                CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("de");
                var translated = localizationManager.GetLocalizedString("Artists");
                Assert.Equal("Interpreten", translated);
            }
            finally
            {
                CultureInfo.CurrentUICulture = previousCulture;
            }
        }

        [Fact]
        public void GetSupportedUICultures_IncludesCommonCultures()
        {
            var supported = LocalizationManager.GetSupportedUICultures();
            Assert.Contains(supported, c => c.Name.Equals("de", StringComparison.OrdinalIgnoreCase));
            Assert.Contains(supported, c => c.Name.Equals("en-US", StringComparison.OrdinalIgnoreCase));
            Assert.Contains(supported, c => c.Name.Equals("fr", StringComparison.OrdinalIgnoreCase));
            // Underscore variants get normalized to BCP-47 hyphen form for CultureInfo compatibility.
            Assert.Contains(supported, c => c.Name.Equals("es-419", StringComparison.OrdinalIgnoreCase));
        }

        private LocalizationManager Setup(ServerConfiguration config)
        {
            var mockConfiguration = new Mock<IServerConfigurationManager>();
            mockConfiguration.SetupGet(x => x.Configuration).Returns(config);

            return new LocalizationManager(mockConfiguration.Object, new NullLogger<LocalizationManager>());
        }
    }
}