Skip to content

Commit

Permalink
Add fallback languages (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: rlauu <[email protected]>
  • Loading branch information
rlauuzo and rlauuzo authored May 13, 2024
1 parent 4f82190 commit 27ec12d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ public int MetadataUpdateInHours
get => _metadataUpdateInHours;
set => _metadataUpdateInHours = value < 1 ? 1 : value;
}

/// <summary>
/// Gets or sets the fallback languages.
/// </summary>
public string FallbackLanguages { get; set; } = string.Empty;
}
}
9 changes: 9 additions & 0 deletions Jellyfin.Plugin.Tvdb/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ <h2 class="sectionTitle">TheTVDB Settings:</h2>
How many hours ago should the metadata be last updated on TheTvdb. Should be greater than the value of cache time in hours.
</div>
</div>
<div class="inputContainer">
<label for="fallbackLanguages">Fallback Languages:</label>
<input type="text" id="fallbackLanguages" is="emby-input" />
<div class="fieldDescription">
If the preferred metadata language is not available, the plugin will attempt to retrieve metadata in these languages (in the order listed). Separate language codes with commas (e.g., en, fr, de, ja).
</div>
</div>
<br />
<div>
<button is="emby-button" type="submit" data-theme="b" class="raised button-submit block">
Expand All @@ -64,6 +71,7 @@ <h2 class="sectionTitle">TheTVDB Settings:</h2>
document.getElementById('cacheDurationInHours').value = config.CacheDurationInHours;
document.getElementById('cacheDurationInDays').value = config.CacheDurationInDays;
document.getElementById('metadataUpdateInHours').value = config.MetadataUpdateInHours;
document.getElementById('fallbackLanguages').value = config.FallbackLanguages;
Dashboard.hideLoadingMsg();
});
},
Expand All @@ -76,6 +84,7 @@ <h2 class="sectionTitle">TheTVDB Settings:</h2>
config.CacheDurationInHours = document.getElementById('cacheDurationInHours').value;
config.CacheDurationInDays = document.getElementById('cacheDurationInDays').value;
config.MetadataUpdateInHours = document.getElementById('metadataUpdateInHours').value;
config.FallbackLanguages = document.getElementById('fallbackLanguages').value;
ApiClient.updatePluginConfiguration(TvdbPluginConfiguration.uniquePluginId, config).then(function (result) {
Dashboard.processPluginConfigurationUpdateResult(result);
});
Expand Down
28 changes: 25 additions & 3 deletions Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace Jellyfin.Plugin.Tvdb;
/// </summary>
public static class TvdbSdkExtensions
{
/// <summary>
/// Gets the fallback languages which have been selected, ignoring whitespace and empty entries.
/// </summary>
private static string[]? FallbackLanguages => TvdbPlugin.Instance?.Configuration.FallbackLanguages.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

/// <summary>
/// Get the translated Name, or <see langword="null"/>.
/// </summary>
Expand All @@ -28,7 +33,13 @@ public static class TvdbSdkExtensions
return translations?
.NameTranslations?
.FirstOrDefault(translation => IsMatch(translation.Language, language) && translation.IsAlias != true)?
.Name;
.Name
?? FallbackLanguages?
.Select(lang => translations?
.NameTranslations?
.FirstOrDefault(translation => IsMatch(translation.Language, lang) && translation.IsAlias != true)?
.Name)
.FirstOrDefault(name => name != null);
}

/// <summary>
Expand All @@ -41,7 +52,12 @@ public static class TvdbSdkExtensions
{
return translations?
.FirstOrDefault(translation => IsMatch(translation.Key, language))
.Value;
.Value
?? FallbackLanguages?
.Select(lang => translations?
.FirstOrDefault(translation => IsMatch(translation.Key, lang))
.Value)
.FirstOrDefault(name => name != null);
}

/// <summary>
Expand All @@ -55,7 +71,13 @@ public static class TvdbSdkExtensions
return translations?
.OverviewTranslations?
.FirstOrDefault(translation => IsMatch(translation.Language, language))?
.Overview;
.Overview
?? FallbackLanguages?
.Select(lang => translations?
.OverviewTranslations?
.FirstOrDefault(translation => IsMatch(translation.Language, lang))?
.Overview)
.FirstOrDefault(overview => overview != null);
}

private static bool IsMatch(this string translation, string? language)
Expand Down

0 comments on commit 27ec12d

Please sign in to comment.