diff --git a/library/src/androidMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.android.kt b/library/src/androidMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.android.kt new file mode 100644 index 00000000000..1a87b512db9 --- /dev/null +++ b/library/src/androidMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.android.kt @@ -0,0 +1,24 @@ +package com.lagradost.cloudstream3.utils + +import java.util.Locale + +// TODO: add androidMain actual using LocaleListCompat.getAdjustedDefault() to respect +// per-app language preferences set via AppCompatDelegate.setApplicationLocales() + +actual fun getCurrentLocale(): String = + Locale.getDefault().toLanguageTag() + +actual fun localizedLanguageName(ietfTag: String, localizedTo: String): String? { + val localeOfLangCode = Locale.forLanguageTag(ietfTag) + val localeOfLocalizeTo = Locale.forLanguageTag(localizedTo) + val displayName = localeOfLangCode.getDisplayName(localeOfLocalizeTo) + + // Locale.getDisplayName() falls back to the raw tag or "language (country)" form + // when it doesn't know how to render the name. + val langCodeWithCountry = "${localeOfLangCode.language} (" + val failed = + displayName.equals(ietfTag, ignoreCase = true) || + displayName.contains(langCodeWithCountry, ignoreCase = true) + + return if (failed) null else displayName +} \ No newline at end of file diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelper.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelper.kt index 7becf4d19c0..b08f9996652 100644 --- a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelper.kt +++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelper.kt @@ -1,13 +1,6 @@ package com.lagradost.cloudstream3.utils import me.xdrop.fuzzywuzzy.FuzzySearch -import java.util.Locale - -// If you find a way to use SettingsGeneral getCurrentLocale() -// instead of this function do it. -fun getCurrentLocale(): String { - return Locale.getDefault().toLanguageTag() -} @Suppress( "unused", @@ -48,23 +41,10 @@ object SubtitleHelper { val ISO_639_3: String, // ISO 639-6 missing as it's intended to differentiate specific dialects and variants val openSubtitles: String, // inconsistent codes that do not conform ISO 639 ) { - fun localizedName(localizedTo: String? = null): String { - // Use system locale to localize language name - val localeOfLangCode = Locale.forLanguageTag(this.IETF_tag) - val localeOfLocalizeTo = Locale.forLanguageTag(localizedTo ?: getCurrentLocale()) - val sysLocalizedName = localeOfLangCode.getDisplayName(localeOfLocalizeTo) - - val langCodeWithCountry = "${localeOfLangCode.language} (" // ${localeOfLangCode.country})" - val failedToLocalize = - sysLocalizedName.equals(this.IETF_tag, ignoreCase = true) || - sysLocalizedName.contains(langCodeWithCountry, ignoreCase = true) - - return if (failedToLocalize) + fun localizedName(localizedTo: String? = null): String = + localizedLanguageName(this.IETF_tag, localizedTo ?: getCurrentLocale()) // fallback to native language name - this.nativeName - else - sysLocalizedName - } + ?: this.nativeName fun nameNextToFlagEmoji(localizedTo: String? = null): String { // fallback to [A][A] -> [?] question mak flag diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.kt new file mode 100644 index 00000000000..38a254da8c9 --- /dev/null +++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.kt @@ -0,0 +1,13 @@ +package com.lagradost.cloudstream3.utils + +/** + * Returns the current locale as an IETF BCP 47 language tag. + */ +expect fun getCurrentLocale(): String + +/** + * Returns the display name of [ietfTag] localized into [localizedTo]. + * Returns null if the platform couldn't produce a meaningful name + * (i.e. it just echoed back the tag or contained a bare language code with parentheses). + */ +expect fun localizedLanguageName(ietfTag: String, localizedTo: String): String? \ No newline at end of file diff --git a/library/src/jsMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.js.kt b/library/src/jsMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.js.kt new file mode 100644 index 00000000000..da7544d0b1d --- /dev/null +++ b/library/src/jsMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.js.kt @@ -0,0 +1,18 @@ +package com.lagradost.cloudstream3.utils + +actual fun getCurrentLocale(): String = + js("navigator.language") as? String ?: "en" + +actual fun localizedLanguageName(ietfTag: String, localizedTo: String): String? = + try { + val dn = IntlDisplayNames(arrayOf(localizedTo), js("({ type: 'language' })")) + val name = dn.of(ietfTag) + if (name.isNullOrBlank() || name.equals(ietfTag, ignoreCase = true)) null else name + } catch (e: Throwable) { + null + } + +@JsName("Intl.DisplayNames") +external class IntlDisplayNames(locales: Array, options: dynamic) { + fun of(code: String): String? +} \ No newline at end of file diff --git a/library/src/jvmMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.jvm.kt b/library/src/jvmMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.jvm.kt new file mode 100644 index 00000000000..a43358ccb65 --- /dev/null +++ b/library/src/jvmMain/kotlin/com/lagradost/cloudstream3/utils/SubtitleHelperPlatform.jvm.kt @@ -0,0 +1,21 @@ +package com.lagradost.cloudstream3.utils + +import java.util.Locale + +actual fun getCurrentLocale(): String = + Locale.getDefault().toLanguageTag() + +actual fun localizedLanguageName(ietfTag: String, localizedTo: String): String? { + val localeOfLangCode = Locale.forLanguageTag(ietfTag) + val localeOfLocalizeTo = Locale.forLanguageTag(localizedTo) + val displayName = localeOfLangCode.getDisplayName(localeOfLocalizeTo) + + // Locale.getDisplayName() falls back to the raw tag or "language (country)" form + // when it doesn't know how to render the name. + val langCodeWithCountry = "${localeOfLangCode.language} (" + val failed = + displayName.equals(ietfTag, ignoreCase = true) || + displayName.contains(langCodeWithCountry, ignoreCase = true) + + return if (failed) null else displayName +} \ No newline at end of file