Skip to content

Commit

Permalink
Add fallback translations on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jan 14, 2024
1 parent 9ef1757 commit 2800d60
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.prof18.feedflow.i18n

import dev.icerock.moko.resources.StringResource
import dev.icerock.moko.resources.desc.ResourceStringDesc
import dev.icerock.moko.resources.desc.StringDesc


fun ResourceStringDesc.localizedWithFallback(): String {
return localizedStringWithFallback(stringRes)
}

private const val FALLBACK_LOCALE = "en"

private fun localizedStringWithFallback(stringRes: StringResource): String {
val bundle = StringDesc.localeType.getLocaleBundle(stringRes.bundle)
val stringInCurrentLocale = bundle.localizedStringForKey(stringRes.resourceId, null, null)
return if (stringInCurrentLocale == stringRes.resourceId) {
val stringInDefaultBundle = stringRes.bundle.localizedStringForKey(stringRes.resourceId, null, null)
if (stringInDefaultBundle == stringRes.resourceId) {
val fallbackLocale = stringRes.bundle.developmentLocalization ?: FALLBACK_LOCALE
val fallbackLocaleBundle = StringDesc.LocaleType.Custom(fallbackLocale).getLocaleBundle(bundle)
fallbackLocaleBundle.localizedStringForKey(stringRes.resourceId, null, null)
} else {
stringInDefaultBundle
}
} else {
stringInCurrentLocale
}
}
2 changes: 1 addition & 1 deletion iosApp/Source/Utils/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI

extension StringResource {
var localized: String {
return self.desc().localized()
return self.desc().localizedWithFallback()
}
}

Expand Down

0 comments on commit 2800d60

Please sign in to comment.