diff --git a/src/com/archos/mediascraper/Scraper.java b/src/com/archos/mediascraper/Scraper.java index 4a01abaf..6b67b98c 100644 --- a/src/com/archos/mediascraper/Scraper.java +++ b/src/com/archos/mediascraper/Scraper.java @@ -18,6 +18,8 @@ import android.os.Bundle; import android.util.Log; +import androidx.preference.PreferenceManager; + import com.archos.mediascraper.preprocess.SearchInfo; import com.archos.mediascraper.preprocess.SearchPreprocessor; import com.archos.mediascraper.xml.BaseScraper2; @@ -27,6 +29,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Locale; + public class Scraper { private static final Logger log = LoggerFactory.getLogger(Scraper.class); @@ -104,4 +108,27 @@ public ScrapeDetailResult getAutoDetails(SearchInfo info) { return mMovieScraper.search(info); } + public static String getLanguage(Context context) { + String defaultLanguage = Locale.getDefault().getLanguage(); + String result; + // if defaultLanguage is of the form xx-yy take result as xx + if (defaultLanguage.length() > 2 && defaultLanguage.charAt(2) == '-') { + result = defaultLanguage.substring(0, 2); + } else { + result = defaultLanguage; + } + // zh = Mandarin -> Chinese Simplified (zh-cn) or Chinese + // cn = Cantonese -> Chinese Traditional (zh-tw) + if (defaultLanguage.toLowerCase().startsWith("zh-tw")) { + log.warn("getLanguage: curing defaultScraperLanguage=" + defaultLanguage + " zh-tw -> to cn"); + result = "cn"; // Traditional Chinese + } + // treat brazilian portuguese as an exception, tmdb understands pt-br + if (defaultLanguage.toLowerCase().startsWith("pt-br")) { + log.warn("getLanguage: curing defaultScraperLanguage=" + defaultLanguage + ", keeping pt-br"); + result = "pt-br"; // Brazilian Portuguese + } + return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString("favScraperLang", result); + } + } diff --git a/src/com/archos/mediascraper/xml/MovieScraper3.java b/src/com/archos/mediascraper/xml/MovieScraper3.java index ddddda35..fee73bc7 100644 --- a/src/com/archos/mediascraper/xml/MovieScraper3.java +++ b/src/com/archos/mediascraper/xml/MovieScraper3.java @@ -24,6 +24,7 @@ import com.archos.mediascraper.ScrapeDetailResult; import com.archos.mediascraper.ScrapeSearchResult; import com.archos.mediascraper.ScrapeStatus; +import com.archos.mediascraper.Scraper; import com.archos.mediascraper.ScraperCache; import com.archos.mediascraper.SearchResult; import com.archos.mediascraper.preprocess.MovieSearchInfo; @@ -96,7 +97,7 @@ public ScrapeSearchResult getMatches2(SearchInfo info, int maxItems) { if (tmdb == null) reauth(); if (searchService == null) searchService = tmdb.searchService(); // get configured language - String language = getLanguage(mContext); + String language = Scraper.getLanguage(mContext); log.debug("movie search:" + searchInfo.getName() + " year:" + searchInfo.getYear() + " language:" + language); SearchMovieResult searchResult = SearchMovie2.search(searchInfo.getName(), language, searchInfo.getYear(), maxItems, searchService, adultScrape); // TODO: this triggers scrape for all search results, is this intended? @@ -112,7 +113,7 @@ public ScrapeSearchResult getMatches2(SearchInfo info, int maxItems) { @Override protected ScrapeDetailResult getDetailsInternal(SearchResult result, Bundle options) { // TODO: why it searches every first level result? - String language = getLanguage(mContext); + String language = Scraper.getLanguage(mContext); log.debug("getDetailsInternal: language=" + language); long movieId = result.getId(); @@ -168,10 +169,6 @@ protected ScrapeDetailResult getDetailsInternal(SearchResult result, Bundle opti return new ScrapeDetailResult(tag, true, null, ScrapeStatus.OKAY, null); } - public static String getLanguage(Context context) { - return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString("favScraperLang", Locale.getDefault().getLanguage()); - } - @Override protected String internalGetPreferenceName() { return PREFERENCE_NAME; diff --git a/src/com/archos/mediascraper/xml/ShowScraper4.java b/src/com/archos/mediascraper/xml/ShowScraper4.java index 82cf5464..3eb27dab 100644 --- a/src/com/archos/mediascraper/xml/ShowScraper4.java +++ b/src/com/archos/mediascraper/xml/ShowScraper4.java @@ -117,7 +117,7 @@ public ScrapeSearchResult getMatches2(SearchInfo info, int maxItems) { } TvShowSearchInfo searchInfo = (TvShowSearchInfo) info; // get configured language - String language = getLanguage(mContext); + String language = Scraper.getLanguage(mContext); log.debug("getMatches2: tvshow search:" + searchInfo.getShowName() + " s:" + searchInfo.getSeason() + " e:" + searchInfo.getEpisode() + ", maxItems=" + maxItems + ", language=" + language); @@ -429,10 +429,6 @@ private Bundle buildBundle(Map allEpisodes, Bundle options) return bundle; } - public static String getLanguage(Context context) { - return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString("favScraperLang", Locale.getDefault().getLanguage()); - } - public static boolean isShowAlreadyKnown(Integer showId, Context context) { ContentResolver contentResolver = context.getContentResolver(); String[] baseProjection = {ScraperStore.Show.ONLINE_ID};