-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16ac28c
commit b6d8940
Showing
11 changed files
with
197 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
lyricist-compose/src/commonMain/kotlin/cafe/adriel/lyricist/Lyricist.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
lyricist-core/src/commonMain/kotlin/cafe.adriel.lyricist/Lyricist.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cafe.adriel.lyricist | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
public typealias LanguageTag = String | ||
|
||
public class Lyricist<T>( | ||
private val defaultLanguageTag: LanguageTag, | ||
private val translations: Map<LanguageTag, T> | ||
) { | ||
|
||
private val mutableState: MutableStateFlow<LyricistState<T>> = | ||
MutableStateFlow(LyricistState(defaultLanguageTag, getStrings(defaultLanguageTag))) | ||
|
||
public val state: StateFlow<LyricistState<T>> = | ||
mutableState.asStateFlow() | ||
|
||
public var languageTag: LanguageTag | ||
get() = mutableState.value.languageTag | ||
set(languageTag) { | ||
mutableState.value = LyricistState(languageTag, getStrings(languageTag)) | ||
} | ||
|
||
public val strings: T | ||
get() = mutableState.value.strings | ||
|
||
private val LanguageTag.fallback: LanguageTag | ||
get() = split(FALLBACK_REGEX).first() | ||
|
||
private fun getStrings(languageTag: LanguageTag) = | ||
translations[languageTag] | ||
?: translations[languageTag.fallback] | ||
?: translations[defaultLanguageTag] | ||
?: throw LyricistException("Strings for language tag $languageTag not found") | ||
|
||
private companion object { | ||
private val FALLBACK_REGEX = Regex("[-_]") | ||
} | ||
} | ||
|
||
public data class LyricistState<T> internal constructor( | ||
val languageTag: LanguageTag, | ||
val strings: T, | ||
) | ||
|
||
public class LyricistException internal constructor( | ||
override val message: String | ||
) : RuntimeException() | ||
|
||
@Target(AnnotationTarget.PROPERTY) | ||
@Retention(AnnotationRetention.SOURCE) | ||
public annotation class LyricistStrings( | ||
val languageTag: LanguageTag, | ||
val default: Boolean = false | ||
) |
10 changes: 0 additions & 10 deletions
10
lyricist-core/src/commonMain/kotlin/cafe.adriel.lyricist/LyricistStrings.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.