From 139412284276479921632ee5ef1dabe76c5388b4 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 4 Jan 2023 21:23:57 -0800 Subject: [PATCH] Fixed: Displaying audio and subtitle languages in UI --- frontend/src/EpisodeFile/MediaInfo.js | 12 ++++----- .../src/Settings/UI/UISettingsConnector.js | 2 +- .../src/Utilities/String/getLanguageName.js | 27 +++++++++++++++++++ .../Localization/LocalizationService.cs | 16 +++++++---- src/NzbDrone.Core/Parser/IsoLanguage.cs | 3 ++- .../Localization/LanguageResource.cs | 7 +++++ .../Localization/LocalizationController.cs | 12 +++++++++ 7 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 frontend/src/Utilities/String/getLanguageName.js create mode 100644 src/Sonarr.Api.V3/Localization/LanguageResource.cs diff --git a/frontend/src/EpisodeFile/MediaInfo.js b/frontend/src/EpisodeFile/MediaInfo.js index e0f493a96..562a720b7 100644 --- a/frontend/src/EpisodeFile/MediaInfo.js +++ b/frontend/src/EpisodeFile/MediaInfo.js @@ -1,6 +1,7 @@ import _ from 'lodash'; import PropTypes from 'prop-types'; import React from 'react'; +import getLanguageName from 'Utilities/String/getLanguageName'; import * as mediaInfoTypes from './mediaInfoTypes'; function formatLanguages(languages) { @@ -8,7 +9,7 @@ function formatLanguages(languages) { return null; } - const splitLanguages = _.uniq(languages.split(' / ')); + const splitLanguages = _.uniq(languages.split('/')).map((l) => getLanguageName(l)); if (splitLanguages.length > 3) { return ( @@ -40,18 +41,15 @@ function MediaInfo(props) { return ( { - !!audioCodec && - audioCodec + audioCodec ? audioCodec : '' } { - !!audioCodec && !!audioChannels && - ' - ' + audioCodec && audioChannels ? ' - ' : '' } { - !!audioChannels && - audioChannels.toFixed(1) + audioChannels ? audioChannels.toFixed(1) : '' } ); diff --git a/frontend/src/Settings/UI/UISettingsConnector.js b/frontend/src/Settings/UI/UISettingsConnector.js index 66d03d226..585effea3 100644 --- a/frontend/src/Settings/UI/UISettingsConnector.js +++ b/frontend/src/Settings/UI/UISettingsConnector.js @@ -9,7 +9,7 @@ import createSettingsSectionSelector from 'Store/Selectors/createSettingsSection import UISettings from './UISettings'; const SECTION = 'ui'; -const FILTER_LANGUAGES = ['Any', 'Unknown']; +const FILTER_LANGUAGES = ['Any', 'Unknown', 'Original']; function createFilteredLanguagesSelector() { return createSelector( diff --git a/frontend/src/Utilities/String/getLanguageName.js b/frontend/src/Utilities/String/getLanguageName.js new file mode 100644 index 000000000..db957e643 --- /dev/null +++ b/frontend/src/Utilities/String/getLanguageName.js @@ -0,0 +1,27 @@ +import createAjaxRequest from 'Utilities/createAjaxRequest'; + +function getTranslations() { + return createAjaxRequest({ + global: false, + dataType: 'json', + url: '/localization/language' + }).request; +} + +let languageNames = new Intl.DisplayNames(['en'], { type: 'language' }); + +getTranslations().then((data) => { + const names = new Intl.DisplayNames([data.identifier], { type: 'language' }); + + if (names) { + languageNames = names; + } +}); + +export default function getLanguageName(code) { + if (!languageNames) { + return code; + } + + return languageNames.of(code) ?? code; +} diff --git a/src/NzbDrone.Core/Localization/LocalizationService.cs b/src/NzbDrone.Core/Localization/LocalizationService.cs index 606639f38..aefe7c04b 100644 --- a/src/NzbDrone.Core/Localization/LocalizationService.cs +++ b/src/NzbDrone.Core/Localization/LocalizationService.cs @@ -20,6 +20,7 @@ namespace NzbDrone.Core.Localization Dictionary GetLocalizationDictionary(); string GetLocalizedString(string phrase); string GetLocalizedString(string phrase, string language); + string GetLanguageIdentifier(); } public class LocalizationService : ILocalizationService, IHandleAsync @@ -45,14 +46,14 @@ namespace NzbDrone.Core.Localization public Dictionary GetLocalizationDictionary() { - var language = GetSetLanguageFileName(); + var language = GetLanguageFileName(); return GetLocalizationDictionary(language); } public string GetLocalizedString(string phrase) { - var language = GetSetLanguageFileName(); + var language = GetLanguageFileName(); return GetLocalizedString(phrase, language); } @@ -66,7 +67,7 @@ namespace NzbDrone.Core.Localization if (language.IsNullOrWhiteSpace()) { - language = GetSetLanguageFileName(); + language = GetLanguageFileName(); } if (language == null) @@ -84,19 +85,24 @@ namespace NzbDrone.Core.Localization return phrase; } - private string GetSetLanguageFileName() + public string GetLanguageIdentifier() { var isoLanguage = IsoLanguages.Get((Language)_configService.UILanguage); var language = isoLanguage.TwoLetterCode; if (isoLanguage.CountryCode.IsNotNullOrWhiteSpace()) { - language = string.Format("{0}_{1}", language, isoLanguage.CountryCode); + language = $"{language}-{isoLanguage.CountryCode.ToUpperInvariant()}"; } return language; } + private string GetLanguageFileName() + { + return GetLanguageIdentifier().Replace("-", "_").ToLowerInvariant(); + } + private Dictionary GetLocalizationDictionary(string language) { if (string.IsNullOrEmpty(language)) diff --git a/src/NzbDrone.Core/Parser/IsoLanguage.cs b/src/NzbDrone.Core/Parser/IsoLanguage.cs index 6d8f9bca8..0beed92f5 100644 --- a/src/NzbDrone.Core/Parser/IsoLanguage.cs +++ b/src/NzbDrone.Core/Parser/IsoLanguage.cs @@ -1,4 +1,4 @@ -using NzbDrone.Core.Languages; +using NzbDrone.Core.Languages; namespace NzbDrone.Core.Parser { @@ -8,6 +8,7 @@ namespace NzbDrone.Core.Parser public string ThreeLetterCode { get; set; } public string CountryCode { get; set; } public Language Language { get; set; } + public string CountyCodeLower => CountryCode?.ToLower(); public IsoLanguage(string twoLetterCode, string countryCode, string threeLetterCode, Language language) { diff --git a/src/Sonarr.Api.V3/Localization/LanguageResource.cs b/src/Sonarr.Api.V3/Localization/LanguageResource.cs new file mode 100644 index 000000000..964c1bf70 --- /dev/null +++ b/src/Sonarr.Api.V3/Localization/LanguageResource.cs @@ -0,0 +1,7 @@ +namespace Sonarr.Api.V3.Localization +{ + public class LanguageResource + { + public string Identifier { get; set; } + } +} diff --git a/src/Sonarr.Api.V3/Localization/LocalizationController.cs b/src/Sonarr.Api.V3/Localization/LocalizationController.cs index e350b8497..e984d8e8e 100644 --- a/src/Sonarr.Api.V3/Localization/LocalizationController.cs +++ b/src/Sonarr.Api.V3/Localization/LocalizationController.cs @@ -26,5 +26,17 @@ namespace Sonarr.Api.V3.Localization { return _localizationService.GetLocalizationDictionary().ToResource(); } + + [HttpGet("language")] + [Produces("application/json")] + public LanguageResource GetLanguage() + { + var identifier = _localizationService.GetLanguageIdentifier(); + + return new LanguageResource + { + Identifier = identifier + }; + } } }