From 06f5af783b03810551c0e7a3049cf6c8602298bd Mon Sep 17 00:00:00 2001 From: softworkz Date: Sat, 25 Jun 2016 01:53:38 +0200 Subject: [PATCH] Adjust language for image results from TMDB when querying complex language codes (> 2 chars, qualified by country) --- .../BoxSets/MovieDbBoxSetImageProvider.cs | 2 +- .../Movies/MovieDbImageProvider.cs | 6 +-- .../Movies/MovieDbProvider.cs | 41 ++++++++++++++++--- .../TheMovieDb/MovieDbEpisodeImageProvider.cs | 6 ++- .../TV/TheMovieDb/MovieDbProviderBase.cs | 2 +- .../TheMovieDb/MovieDbSeriesImageProvider.cs | 6 +-- 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs b/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs index b3e73740d9..2dce13ebc3 100644 --- a/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs +++ b/MediaBrowser.Providers/BoxSets/MovieDbBoxSetImageProvider.cs @@ -83,7 +83,7 @@ namespace MediaBrowser.Providers.BoxSets VoteCount = i.vote_count, Width = i.width, Height = i.height, - Language = i.iso_639_1, + Language = MovieDbProvider.AdjustImageLanguage(i.iso_639_1, language), ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score diff --git a/MediaBrowser.Providers/Movies/MovieDbImageProvider.cs b/MediaBrowser.Providers/Movies/MovieDbImageProvider.cs index f9e9bc947f..49f341f26f 100644 --- a/MediaBrowser.Providers/Movies/MovieDbImageProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbImageProvider.cs @@ -63,6 +63,8 @@ namespace MediaBrowser.Providers.Movies { var list = new List(); + var language = item.GetPreferredMetadataLanguage(); + var results = await FetchImages((BaseItem)item, null, _jsonSerializer, cancellationToken).ConfigureAwait(false); if (results == null) @@ -85,7 +87,7 @@ namespace MediaBrowser.Providers.Movies VoteCount = i.vote_count, Width = i.width, Height = i.height, - Language = i.iso_639_1, + Language = MovieDbProvider.AdjustImageLanguage(i.iso_639_1, language), ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score @@ -107,8 +109,6 @@ namespace MediaBrowser.Providers.Movies })); } - var language = item.GetPreferredMetadataLanguage(); - var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); return list.OrderByDescending(i => diff --git a/MediaBrowser.Providers/Movies/MovieDbProvider.cs b/MediaBrowser.Providers/Movies/MovieDbProvider.cs index 6a2e727439..27b61225bd 100644 --- a/MediaBrowser.Providers/Movies/MovieDbProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbProvider.cs @@ -267,9 +267,21 @@ namespace MediaBrowser.Providers.Movies if (!string.IsNullOrEmpty(preferredLanguage)) { + preferredLanguage = NormalizeLanguage(preferredLanguage); + languages.Add(preferredLanguage); + + if (preferredLanguage.Length == 5) // like en-US + { + // Currenty, TMDB supports 2-letter language codes only + // They are planning to change this in the future, thus we're + // supplying both codes if we're having a 5-letter code. + languages.Add(preferredLanguage.Substring(0, 2)); + } } + languages.Add("null"); + if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase)) { languages.Add("en"); @@ -280,18 +292,35 @@ namespace MediaBrowser.Providers.Movies public static string NormalizeLanguage(string language) { - // They require this to be uppercase - // https://emby.media/community/index.php?/topic/32454-fr-follow-tmdbs-new-language-api-update/?p=311148 - var parts = language.Split('-'); - - if (parts.Length == 2) + if (!string.IsNullOrEmpty(language)) { - language = parts[0] + "-" + parts[1].ToUpper(); + // They require this to be uppercase + // https://emby.media/community/index.php?/topic/32454-fr-follow-tmdbs-new-language-api-update/?p=311148 + var parts = language.Split('-'); + + if (parts.Length == 2) + { + language = parts[0] + "-" + parts[1].ToUpper(); + } } return language; } + public static string AdjustImageLanguage(string imageLanguage, string requestLanguage) + { + if (!string.IsNullOrEmpty(imageLanguage) + && !string.IsNullOrEmpty(requestLanguage) + && requestLanguage.Length > 2 + && imageLanguage.Length == 2 + && requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase)) + { + return requestLanguage; + } + + return imageLanguage; + } + /// /// Fetches the main result. /// diff --git a/MediaBrowser.Providers/TV/TheMovieDb/MovieDbEpisodeImageProvider.cs b/MediaBrowser.Providers/TV/TheMovieDb/MovieDbEpisodeImageProvider.cs index 719779674b..0feb92e897 100644 --- a/MediaBrowser.Providers/TV/TheMovieDb/MovieDbEpisodeImageProvider.cs +++ b/MediaBrowser.Providers/TV/TheMovieDb/MovieDbEpisodeImageProvider.cs @@ -57,8 +57,10 @@ namespace MediaBrowser.Providers.TV return list; } + var language = item.GetPreferredMetadataLanguage(); + var response = await GetEpisodeInfo(seriesId, seasonNumber.Value, episodeNumber.Value, - item.GetPreferredMetadataLanguage(), cancellationToken).ConfigureAwait(false); + language, cancellationToken).ConfigureAwait(false); var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false); @@ -71,12 +73,12 @@ namespace MediaBrowser.Providers.TV VoteCount = i.vote_count, Width = i.width, Height = i.height, + Language = MovieDbProvider.AdjustImageLanguage(i.iso_639_1, language), ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score })); - var language = item.GetPreferredMetadataLanguage(); var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); diff --git a/MediaBrowser.Providers/TV/TheMovieDb/MovieDbProviderBase.cs b/MediaBrowser.Providers/TV/TheMovieDb/MovieDbProviderBase.cs index a6df245b01..821c26e4be 100644 --- a/MediaBrowser.Providers/TV/TheMovieDb/MovieDbProviderBase.cs +++ b/MediaBrowser.Providers/TV/TheMovieDb/MovieDbProviderBase.cs @@ -151,7 +151,7 @@ namespace MediaBrowser.Providers.TV public string file_path { get; set; } public int height { get; set; } public string id { get; set; } - public object iso_639_1 { get; set; } + public string iso_639_1 { get; set; } public double vote_average { get; set; } public int vote_count { get; set; } public int width { get; set; } diff --git a/MediaBrowser.Providers/TV/TheMovieDb/MovieDbSeriesImageProvider.cs b/MediaBrowser.Providers/TV/TheMovieDb/MovieDbSeriesImageProvider.cs index 06481f8f2f..ad46db6773 100644 --- a/MediaBrowser.Providers/TV/TheMovieDb/MovieDbSeriesImageProvider.cs +++ b/MediaBrowser.Providers/TV/TheMovieDb/MovieDbSeriesImageProvider.cs @@ -66,6 +66,8 @@ namespace MediaBrowser.Providers.TV var tmdbImageUrl = tmdbSettings.images.secure_base_url + "original"; + var language = item.GetPreferredMetadataLanguage(); + list.AddRange(GetPosters(results).Select(i => new RemoteImageInfo { Url = tmdbImageUrl + i.file_path, @@ -73,7 +75,7 @@ namespace MediaBrowser.Providers.TV VoteCount = i.vote_count, Width = i.width, Height = i.height, - Language = i.iso_639_1, + Language = MovieDbProvider.AdjustImageLanguage(i.iso_639_1, language), ProviderName = Name, Type = ImageType.Primary, RatingType = RatingType.Score @@ -91,8 +93,6 @@ namespace MediaBrowser.Providers.TV RatingType = RatingType.Score })); - var language = item.GetPreferredMetadataLanguage(); - var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase); return list.OrderByDescending(i =>