Android SDK
The Kotlin library reads the strings you publish with Delivery (CDN) into an Android app, so a translation fix reaches users without a new release. getString never blocks on the network: it answers from the downloaded strings, then your bundled ones, then your own resource. Requires minSdk 24 and Kotlin 2.2 or newer.
Before you start
Create a distribution under Project Settings → Delivery (CDN) and publish a release, as described in Delivery (CDN). The library needs the distribution's public ID, the 22-character part of its manifest URL. In the app, Copy SDK snippet on the distribution card has an Android entry that fills it in.
Install
dependencies {
implementation("com.translatize:ota:0.1.0")
// only if you use Jetpack Compose
implementation("com.translatize:ota-compose:0.1.0")
}The Maven coordinates are com.translatize:ota and com.translatize:ota-compose. The only runtime dependency of ota is kotlinx-coroutines; HTTP goes through HttpURLConnection and JSON through the platform's org.json. Compose lives in its own artifact, so apps without Compose never pull it in; ota-compose needs Compose 1.7 or newer. The library adds the INTERNET permission to your manifest.
Set it up
import com.translatize.ota.TranslatizeOta
class App : Application() {
override fun onCreate() {
super.onCreate()
TranslatizeOta.init(
context = this,
distributionId = "YOUR_DISTRIBUTION_ID",
cdnUrl = "https://cdn.translatize.com", // the default
bundled = mapOf("en" to mapOf("home.title" to "Welcome")),
)
}
}init does no network or disk work, so it is safe in Application.onCreate. Later calls to init are ignored. Optional arguments: minRefreshMs (default 300000) and timeoutMs (default 3000). A blank distributionId logs a warning in Logcat; bundled strings and resources keep working and the CDN is never called.
Read strings
val title = TranslatizeOta.getString("home.title", "en", R.string.home_title)getString returns the first of these that has the key:
- the downloaded strings in the cache,
- your
bundledstrings, - the
@StringResfallback (or the key itself if that resource is missing).
When the cache is older than minRefreshMs, it refreshes in the background. The first call for a language reads its cache file, so the first frame already has downloaded strings. Call load from a coroutine if you would rather do that read off the main thread.
language is a BCP 47 tag such as de, pt-BR or zh-Hant. A more specific tag falls back to a shorter one, so Locale.getDefault().toLanguageTag() works: de-DE finds de. Nested files read by dotted keys: {"home":{"title":"Hi"}} is home.title. A value that isn't a string is skipped, and the bundled or resource value is used instead.
| Call | What it does |
|---|---|
getString(key, language, @StringRes fallback) | As above. Returns a String. |
getString(key, language) | The same with no resource: null when neither the cache nor bundled has the key. |
suspend load(language) | The whole language as a flat map: cached, else bundled, else one fetch attempt, else empty. |
suspend refresh(language?) | Checks now. true when new strings came in. Without a language it covers every language used so far, or the device's language if none was. |
updates | A Flow<OtaUpdate> that emits language and translations each time new strings are stored, and never for a check that found nothing new. |
stringsFromJson(json) | Dotted keys from a nested or flat JSON file, for bundled. |
Bundled strings
To bundle a file exported from Translatize:
val en = assets.open("en.json").bufferedReader().use { it.readText() }
TranslatizeOta.init(this, "YOUR_DISTRIBUTION_ID", bundled = mapOf("en" to TranslatizeOta.stringsFromJson(en)))Bundle your default language. Before the first download it is the only text besides your resources.
Refresh and updates
Reads start at most one check per language per minRefreshMs, failed checks included. A check loads the manifest and downloads a language file only when its hash changed. Collect updates to react when new strings arrive:
lifecycleScope.launch {
TranslatizeOta.updates.collect { recreate() }
}refresh() checks right away and ignores minRefreshMs.
getString on a Context and @string/ in layouts still read your APK. Call the library where you want downloaded text.Jetpack Compose
import com.translatize.ota.compose.otaString
Text(otaString("home.title"))
Text(otaString("home.title", fallback = R.string.home_title))
Text(otaString("home.title", language = "de"))otaString uses the locale of the current configuration unless you pass language, and recomposes when new strings arrive.
Placeholders stay as stored
Values are returned exactly as they were published. Nothing is parsed, escaped or replaced, so your strings keep whatever format you wrote them in: %1$s, %@, {name}, ICU. There is no formatting engine. Format the result as you would a resource string:
val text = TranslatizeOta.getString("cart.count", "en", R.string.cart_count).format(count)What's cached, and where
One file per distribution and language in filesDir/translatize-ota/, holding the hash, the strings, the fetch time and the manifest's ETag. Files are replaced atomically, and a corrupt file is ignored. They hold published strings only, nothing about the user. The folder is included in Android Auto Backup unless your backup rules exclude it. That does no harm, since the content is public and is checked again on the next refresh.
When the CDN says no, or isn't there
Nothing throws into your app. On every failure below, the cached or bundled strings stay in place.
| Case | What happens |
|---|---|
| Offline, timeout, server error | The attempt ends. At most one attempt per language per minRefreshMs starts from reads. |
| Hash mismatch | The file is rejected and never stored. |
| 429 | No calls to the CDN until Retry-After has passed (an hour if it is missing, a day at most). |
| 404 | The distribution is disabled or unknown. No more calls until the next app launch. |
| Blank distributionId | A warning in Logcat. Bundled strings and resources keep working, and the CDN is never called. |
Every request the CDN serves counts towards your delivery volume, including checks that find nothing new. Refused requests (429) don't. See Requests and blocks and When the limit is reached.
Using iOS or Flutter? See the iOS SDK and the Flutter SDK.
