diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f0139c2f6d..65ba75c06f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.20.26 +* `Resources`: + * Add opportunity to get default translation by passing `null` as `IetfLang` argument + * Add several useful extensions to get translations in `JS` target + ## 0.20.25 * `Colors`: diff --git a/resources/src/commonMain/kotlin/StringResource.kt b/resources/src/commonMain/kotlin/StringResource.kt index 4ccb0fb8fcc..d87526fc691 100644 --- a/resources/src/commonMain/kotlin/StringResource.kt +++ b/resources/src/commonMain/kotlin/StringResource.kt @@ -39,7 +39,10 @@ class StringResource( fun build() = StringResource(default, map.toMap()) } - fun translation(languageCode: IetfLang): String { + fun translation(languageCode: IetfLang?): String { + if (languageCode == null) { + return default + } map[languageCode] ?.let { return it.value } return languageCode.parentLang ?.let { diff --git a/resources/src/jsMain/kotlin/StringResourceExtensions.kt b/resources/src/jsMain/kotlin/StringResourceExtensions.kt new file mode 100644 index 00000000000..d16c1fe401c --- /dev/null +++ b/resources/src/jsMain/kotlin/StringResourceExtensions.kt @@ -0,0 +1,11 @@ +package dev.inmo.micro_utils.strings + +import dev.inmo.micro_utils.language_codes.IetfLang +import kotlinx.browser.window +import org.w3c.dom.NavigatorLanguage + +fun StringResource.translation(language: NavigatorLanguage) = translation( + language.language.unsafeCast() ?.let { IetfLang(it) } +) + +fun StringResource.translation() = translation(window.navigator)