Move ItemsProxy sort helpers
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using Jellyfin.Plugin.Multilang.Configuration;
|
||||
|
||||
namespace Jellyfin.Plugin.Multilang.Services;
|
||||
|
||||
@@ -52,6 +53,57 @@ public static class ItemsProxySorting
|
||||
}
|
||||
}
|
||||
|
||||
public static string ResolveSortLocale(string? configuredLocale, string? clientLocale)
|
||||
{
|
||||
var configured = (configuredLocale ?? string.Empty).Trim();
|
||||
return configured.Length > 0 && !configured.Equals("Auto", StringComparison.OrdinalIgnoreCase)
|
||||
? configured
|
||||
: (clientLocale ?? string.Empty).Trim();
|
||||
}
|
||||
|
||||
public static CultureInfo GetSortCulture(string? sortLocale)
|
||||
{
|
||||
var locale = (sortLocale ?? string.Empty).Trim();
|
||||
if (locale.Length == 0)
|
||||
return CultureInfo.InvariantCulture;
|
||||
|
||||
try
|
||||
{
|
||||
return CultureInfo.GetCultureInfo(locale);
|
||||
}
|
||||
catch (CultureNotFoundException)
|
||||
{
|
||||
return CultureInfo.InvariantCulture;
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] GetSortArticles(SortArticleCatalog articleCatalog, IEnumerable<SortArticleEntry> configuredArticles, string? sortLocale)
|
||||
{
|
||||
var locale = (sortLocale ?? string.Empty).Trim();
|
||||
if (locale.Length == 0)
|
||||
locale = "en";
|
||||
|
||||
var configured = configuredArticles.ToArray();
|
||||
var custom = configured.FirstOrDefault(e => e.Language.Equals(locale, StringComparison.OrdinalIgnoreCase))
|
||||
?? configured.FirstOrDefault(e => locale.StartsWith(e.Language + "-", StringComparison.OrdinalIgnoreCase));
|
||||
if (custom is not null)
|
||||
return SplitArticles(custom.Articles);
|
||||
|
||||
var builtIns = articleCatalog.GetBuiltIns();
|
||||
if (builtIns.TryGetValue(locale, out var exact))
|
||||
return exact;
|
||||
|
||||
var dash = locale.IndexOf('-', StringComparison.Ordinal);
|
||||
return dash > 0 && builtIns.TryGetValue(locale[..dash], out var languageOnly)
|
||||
? languageOnly
|
||||
: [];
|
||||
}
|
||||
|
||||
private static string[] SplitArticles(string articles)
|
||||
=> articles.Split([',', ';'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
private static bool TryGetItemsArray(JsonNode root, out JsonArray items)
|
||||
{
|
||||
if (root is JsonObject obj && obj["Items"] is JsonArray array)
|
||||
|
||||
Reference in New Issue
Block a user