Extract truncation logic to helper and add tests

This commit is contained in:
854562
2026-07-20 22:00:10 +02:00
committed by GitHub
parent c632417dda
commit 2cd2f36fe4
5 changed files with 63 additions and 11 deletions
@@ -261,6 +261,24 @@ namespace Emby.Server.Implementations.Localization
_cultures);
}
/// <inheritdoc />
public string? GetLanguageDisplayName(string language)
{
if (string.IsNullOrEmpty(language))
{
return null;
}
var displayName = FindLanguageInfo(language)?.DisplayName;
if (displayName is null)
{
return null;
}
// Truncate at the first delimiter to avoid cluttered display names
return displayName.Split([';', ','], StringSplitOptions.None)[0].Trim();
}
/// <inheritdoc />
public IReadOnlyList<CountryInfo> GetCountries()
{
@@ -172,11 +172,7 @@ public class MediaStreamRepository : IMediaStreamRepository
if (!string.IsNullOrEmpty(dto.Language))
{
var culture = _localization.FindLanguageInfo(dto.Language);
// Truncate at the first delimiter to avoid cluttered display names
dto.LocalizedLanguage = culture?.DisplayName is { } name
? name.Split([';', ','])[0].Trim()
: null;
dto.LocalizedLanguage = _localization.GetLanguageDisplayName(dto.Language);
}
if (dto.Type is MediaStreamType.Audio)
@@ -732,9 +732,7 @@ namespace MediaBrowser.MediaEncoding.Probing
stream.LocalizedOriginal = _localization.GetLocalizedString("Original");
if (!string.IsNullOrEmpty(stream.Language))
{
stream.LocalizedLanguage = _localization.FindLanguageInfo(stream.Language)?.DisplayName is { } name
? name.Split([';', ','])[0].Trim()
: null;
stream.LocalizedLanguage = _localization.GetLanguageDisplayName(stream.Language);
}
stream.Channels = streamInfo.Channels;
@@ -776,9 +774,7 @@ namespace MediaBrowser.MediaEncoding.Probing
stream.LocalizedHearingImpaired = _localization.GetLocalizedString("HearingImpaired");
if (!string.IsNullOrEmpty(stream.Language))
{
stream.LocalizedLanguage = _localization.FindLanguageInfo(stream.Language)?.DisplayName is { } name
? name.Split([';', ','])[0].Trim()
: null;
stream.LocalizedLanguage = _localization.GetLanguageDisplayName(stream.Language);
}
if (string.IsNullOrEmpty(stream.Title))
@@ -72,6 +72,14 @@ public interface ILocalizationManager
/// <returns>The correct <see cref="CultureDto" /> for the given language.</returns>
CultureDto? FindLanguageInfo(string language);
/// <summary>
/// Gets a human-readable display name for the given language code.
/// Truncates at the first semicolon or comma to avoid cluttered ISO-639-2 names.
/// </summary>
/// <param name="language">An ISO language code.</param>
/// <returns>The display name, or null if not found.</returns>
string? GetLanguageDisplayName(string language);
/// <summary>
/// Returns the language in ISO 639-2/T when the input is ISO 639-2/B.
/// </summary>
@@ -119,6 +119,40 @@ namespace Jellyfin.Server.Implementations.Tests.Localization
Assert.Equal(code, culture.ThreeLetterISOLanguageName);
}
[Theory]
[InlineData("ell", "Greek")] // Comma truncation
[InlineData("nld", "Dutch")] // Semicolon truncation
[InlineData("ron", "Romanian")] // Semicolon truncation, multiple
[InlineData("eng", "English")] // No truncation
[InlineData("zh-CN", "Chinese (Simplified)")] // No truncation, with parentheses
public async Task GetLanguageDisplayName_DelimitedName_ReturnsTruncatedName(string language, string expected)
{
var localizationManager = Setup(new ServerConfiguration
{
UICulture = "en-US"
});
await localizationManager.LoadAll();
var result = localizationManager.GetLanguageDisplayName(language);
Assert.Equal(expected, result);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("xyz")]
public async Task GetLanguageDisplayName_InvalidInput_ReturnsNull(string? language)
{
var localizationManager = Setup(new ServerConfiguration
{
UICulture = "en-US"
});
await localizationManager.LoadAll();
var result = localizationManager.GetLanguageDisplayName(language!);
Assert.Null(result);
}
[Fact]
public async Task GetParentalRatings_Default_Success()
{