Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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?
Original file line number Diff line number Diff line change
@@ -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<String>, options: dynamic) {
fun of(code: String): String?
}
Original file line number Diff line number Diff line change
@@ -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
}