Skip to content

Seahorse provides a simple framework to support getting strings from various sources or fallback to the ones compiled into the app.

License

Notifications You must be signed in to change notification settings

bidrohi/seahorse

Repository files navigation

Seahorse : String resources from local or remote sources

Kotlin Alpha Kotlin License

badge-jvm badge-android badge-ios badge-watchos badge-tvos badge-mac badge-linux badge-windows

Seahorse provides a simple framework to support getting strings from remote sources or fallback to the ones compiled into the app or locally available. The goal is to provide the string as fast as possible, so the framework fetches the remote source and store locally for quick access.

Gradle setup

Kotlin DSL
implementation("com.bidyut.tech.seahorse:seahorse-core:<version>")
Version Catalogue
[versions]
seahorse = "version"

[libraries]
seahorse-core = { group = "com.bidyut.tech.seahorse", name = "seahorse-core", version.ref = "seahorse" }

Initialise Seahorse

Minimum requirement for a Seahorse set up requires the fallback source to be present, and we default to English. To get started, create an instance of Seahorse with the system fallback (where available).

For Android, we can fallback to the compiled resources:

val seahorse = Seahorse {
    fallbackSource = ResourceFallbackSource(context)
}

For iOS, we can fallback to bundled strings:

let seahorse = Seahorse { c in
    c.fallbackSource = NSLocalizedFallbackSource()
}

For platforms that don't have any such provisions we can fallback to a map or other options:

val seahorse = Seahorse {
    fallbackSource = MapFallbackSource(
        mapOf(
            "key" to "basic string",
            "parameterisedKey" to "got %s",
        )
    )
}

Accessing strings

Now you can access the string resource for the currently selected language by:

seahorse.getString("key")

or if there is some parameters we can tack along

seahorse.getString("parameterisedKey", "milk")

Setting up a remote source

Remote sources require a local store to cache the strings for quick access. The local store can be a simple map or a more complex database.

We can use a SQLite database for the local store, for example in Android:

val seahorse = Seahorse {
    fallbackSource = ResourceFallbackSource(context)
    localStore = AndroidDatabaseDriverFactory(context)
}
Library setup
[libraries]
seahorse-sqlite = { group = "com.bidyut.tech.seahorse", name = "seahorse-sqlite", version.ref = "seahorse" }

Or we can use Realm database:

val seahorse = Seahorse {
    fallbackSource = ResourceFallbackSource(context)
    localStore = RealmLocalStore()
}
Library setup
[libraries]
seahorse-realm = { group = "com.bidyut.tech.seahorse", name = "seahorse-realm", version.ref = "seahorse" }

Ktor Network Source

And we can pair that up with an Ktor network source:

val seahorse = Seahorse {
    fallbackSource = ResourceFallbackSource(context)
    localStore = AndroidDatabaseDriverFactory(context)
    remoteSource = AndroidKtorNetworkSource { languageId ->
        "https://www.bidyut.com/tech/seahorse/sample/${languageId.lowercase()}.json"
    }
}
Library setup
[libraries]
seahorse-ktor = { group = "com.bidyut.tech.seahorse", name = "seahorse-ktor", version.ref = "seahorse" }

OkHttp Network Source

There is also provide OkHttp implementation:

val seahorse = Seahorse {
    fallbackSource = ResourceFallbackSource(context)
    localStore = AndroidDatabaseDriverFactory(context)
    remoteSource = OkHttpNetworkSource(okHttpClient) { languageId ->
        "https://www.bidyut.com/tech/seahorse/sample/${languageId.lowercase()}.json"
    }
}
Library setup
[libraries]
seahorse-okhttp = { group = "com.bidyut.tech.seahorse", name = "seahorse-okhttp", version.ref = "seahorse" }

About

Seahorse provides a simple framework to support getting strings from various sources or fallback to the ones compiled into the app.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages