Skip to content

Commit

Permalink
feat(i18n): able to finetune transliterator (jellyfin#12378)
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia authored Sep 11, 2024
1 parent c67b78b commit 81aca67
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Jellyfin.Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ namespace Jellyfin.Extensions
/// </summary>
public static partial class StringExtensions
{
private static readonly Lazy<Transliterator> _transliterator = new(() => Transliterator.GetInstance(
"Any-Latin; Latin-Ascii; Lower; NFD; [:Nonspacing Mark:] Remove; [:Punctuation:] Remove;"));
private static readonly Lazy<string> _transliteratorId = new(() =>
Environment.GetEnvironmentVariable("JELLYFIN_TRANSLITERATOR_ID")
?? "Any-Latin; Latin-Ascii; Lower; NFD; [:Nonspacing Mark:] Remove; [:Punctuation:] Remove;");

private static readonly Lazy<Transliterator?> _transliterator = new(() =>
{
try
{
return Transliterator.GetInstance(_transliteratorId.Value);
}
catch (ArgumentException)
{
return null;
}
});

// Matches non-conforming unicode chars
// https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
Expand Down Expand Up @@ -108,7 +121,7 @@ public static ReadOnlySpan<char> RightPart(this ReadOnlySpan<char> haystack, cha
/// <returns>The transliterated string.</returns>
public static string Transliterated(this string text)
{
return _transliterator.Value.Transliterate(text);
return (_transliterator.Value is null) ? text : _transliterator.Value.Transliterate(text);
}
}
}

0 comments on commit 81aca67

Please sign in to comment.