### Manage Locales with Tolgee Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Provides examples for setting the current locale, retrieving the current locale, and observing changes in locale or available translations using a Flow. ```kotlin // Set locale tolgee.setLocale("en") // Get current locale val locale = tolgee.getLocale() // Listen for changes tolgee.changeFlow.collect { // Locale or available translations changed, update UI if needed } ``` -------------------------------- ### Initialize Tolgee for Other Platforms Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Initialize Tolgee for non-Android platforms. This basic setup configures content delivery URL and suggests creating a custom storage provider for caching. ```kotlin fun initTolgee() { Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" // Create a custom storage provider for caching the latest translations from CDN if needed } } } ``` -------------------------------- ### Get Translations using Tolgee Instance Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Demonstrates how to retrieve translations from the Tolgee instance. Includes fetching a simple string, a string with parameters, and a string as a Flow for reactive updates. ```kotlin // Get the Tolgee instance val tolgee = Tolgee.instance // Get a translation (returns null if not loaded yet) val text: String? = tolgee.t("key") // Get a translation with parameters val textWithParams: String? = tolgee.t("key_with_param", mapOf("param" to "value")) // Get a translation as a Flow (updates automatically when translations change) val textFlow: Flow = tolgee.tFlow("key") textFlow.collect { // Use the text (e.g., update UI) } ``` -------------------------------- ### Android-Specific Translation Retrieval Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Shows how to get translations in Android, with options to fall back to Android string resources and include parameters. Also demonstrates using a Flow for reactive updates. ```kotlin // Get a translation with fallback to Android resources val text = tolgee.t(context, R.string.string_key) // Get a translation with fallback to Android resources with parameters val textWithParams = tolgee.t(context, R.string.string_with_params, "param1", "param2") // Get a translation with fallback to Android resources as a Flow val textFlow = tolgee.tFlow(context, R.string.string_key) ``` -------------------------------- ### Initialize Tolgee Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/compose/README.md Set up the Tolgee instance in your application entry point. ```kotlin class MyApplication : Application() { override fun onCreate() { super.onCreate() Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" storage = TolgeeStorageProviderAndroid(this@MyApplication, BuildConfig.VERSION_CODE) } } } } ``` ```kotlin fun initTolgee() { Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" // Create a custom storage provider for caching the latest translations from CDN if needed } } } ``` -------------------------------- ### Build Core Module Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/AGENTS.md Builds the core module of the Tolgee Mobile Kotlin SDK. Ensure you are in the project root directory. ```bash ./gradlew :core:build ``` -------------------------------- ### Compose String Transformations: Before Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Demonstrates standard Jetpack Compose and Compose Multiplatform string resource calls before they are transformed by the Tolgee Gradle plugin. ```kotlin // Standard Compose stringResource @Composable fun WelcomeText() { Text(text = stringResource(R.string.welcome_message)) } // With formatting @Composable fun UserWelcome(username: String) { Text(text = stringResource(R.string.welcome_user, username)) } // Plurals @Composable fun ItemCount(count: Int) { Text(text = pluralStringResource(R.plurals.item_count, count, count)) ``` -------------------------------- ### Android String Transformations: Before Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Illustrates standard Android string resource access methods before transformation by the Tolgee Gradle plugin. ```kotlin // Standard Android getString val text = context.getString(R.string.welcome_message) // Standard Android getString with formatting val formattedText = context.getString(R.string.welcome_user, username) // Standard Android plural string val pluralText = context.getQuantityString(R.plurals.item_count, count, count) ``` -------------------------------- ### Build Compose Module Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/AGENTS.md Builds the compose module of the Tolgee Mobile Kotlin SDK. Ensure you are in the project root directory. ```bash ./gradlew :compose:build ``` -------------------------------- ### Configure Tolgee Network and Content Delivery Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Initializes the Tolgee SDK with custom network settings, including HTTP client configuration and content delivery paths. ```kotlin import io.tolgee.Tolgee import io.ktor.client.* import io.ktor.client.engine.okhttp.* import io.ktor.client.plugins.cache.* import kotlinx.coroutines.Dispatchers Tolgee.init { // Custom network configuration network { // Use custom HTTP client client(OkHttp) { followRedirects = true install(HttpCache) } // Or provide pre-configured client client(myExistingHttpClient) // Custom coroutine context for network operations context(Dispatchers.IO) } contentDelivery { url = "https://cdn.tolg.ee/your-prefix" // Custom path pattern for translation files path { language -> "$language.json" } // Custom manifest path manifestPath("manifest.json") } } ``` -------------------------------- ### Initialize Tolgee in Application class Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Configure the Tolgee instance within the Android Application class, including CDN settings, storage providers, and locale defaults. ```kotlin import android.app.Application import io.tolgee.Tolgee import io.tolgee.storage.TolgeeStorageProviderAndroid class MyApplication : Application() { override fun onCreate() { super.onCreate() Tolgee.init { // Configure content delivery from Tolgee CDN contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" storage = TolgeeStorageProviderAndroid(this@MyApplication, BuildConfig.VERSION_CODE) // Optional: Specify available locales to skip manifest fetch availableLocaleTags("en", "fr", "cs", "de") // Optional: Configure formatter (default is Sprintf) formatter(Tolgee.Formatter.Sprintf) // formatter(Tolgee.Formatter.ICU) // For ICU MessageFormat // Optional: Cache multiple locales in memory (default is 1) maxLocalesInMemory(3) } // Optional: Set default fallback language defaultLanguage("en") // Optional: Set initial locale (defaults to system locale) locale("en") } } } ``` -------------------------------- ### Initialize Tolgee SDK in Android Application Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/README.md Initialize the Tolgee SDK in your Android application's onCreate method. Ensure you provide the correct CDN URL and storage implementation. ```kotlin class MyApplication : Application() { override fun onCreate() { super.onCreate() Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" storage = TolgeeStorageProviderAndroid(this@MyApplication, BuildConfig.VERSION_CODE) } } } } ``` -------------------------------- ### Run Gradle Plugin Tests Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/AGENTS.md Executes all tests for the gradle-plugin module. Ensure you are in the project root directory. ```bash ./gradlew :gradle-plugin:test ``` -------------------------------- ### Compose String Transformations: After Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Illustrates how standard Compose string resource calls are transformed to use Tolgee's equivalents after the Gradle plugin is applied. ```kotlin // Transformed to use Tolgee @Composable fun WelcomeText() { Text(text = io.tolgee.stringResource(R.string.welcome_message)) } // With formatting @Composable fun UserWelcome(username: String) { Text(text = io.tolgee.stringResource(R.string.welcome_user, username)) } // Plurals @Composable fun ItemCount(count: Int) { Text(text = io.tolgee.pluralStringResource(R.plurals.item_count, count, count)) ``` -------------------------------- ### Build Gradle Plugin Module Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/AGENTS.md Builds the gradle-plugin module of the Tolgee Mobile Kotlin SDK. Ensure you are in the project root directory. ```bash ./gradlew :gradle-plugin:build ``` -------------------------------- ### Android String Transformations: After Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Shows how standard Android string resource access methods are transformed to use Tolgee equivalents after the plugin is applied. ```kotlin // Transformed to use Tolgee val text = Tolgee.instance.t(context, R.string.welcome_message) // Transformed with formatting val formattedText = Tolgee.instance.t(context, R.string.welcome_user, username) // Transformed plural val pluralText = Tolgee.instance.t(context, R.plurals.item_count, count, count) ``` -------------------------------- ### Apply Tolgee Plugin using Version Catalog Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Add the Tolgee plugin to your project by referencing it in your Gradle version catalog. ```toml # gradle/libs.versions.toml [plugins] tolgee = { id = "io.tolgee.mobile-kotlin-sdk", version.ref = "tolgee" } ``` -------------------------------- ### Manage Multiple Tolgee Instances Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Create independent Tolgee instances for different configurations or use the singleton instance. Explicit instances are required for specific translation sources or when using Jetpack Compose. ```kotlin import io.tolgee.Tolgee // Create additional instances (not singleton) val mainTolgee = Tolgee.new { contentDelivery { url = "https://cdn.tolg.ee/main-app" } defaultLanguage("en") } val helpTolgee = Tolgee.new { contentDelivery { url = "https://cdn.tolg.ee/help-content" } defaultLanguage("en") } // Use specific instance val mainText = mainTolgee.t("welcome") val helpText = helpTolgee.t("getting_started") // In Compose, pass explicit instance @Composable fun HelpScreen() { val helpTolgee = remember { /* get your instance */ } Text(text = stringResource(helpTolgee, R.string.help_intro)) } // Initialize singleton separately Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/main-app" } } // Access singleton val singleton = Tolgee.instance val singletonOrNull = Tolgee.instanceOrNull // Returns null if not initialized ``` -------------------------------- ### Add Tolgee Dependency using Version Catalog Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Add the Tolgee dependency to your project's Gradle build file using the Version Catalog for consistent version management. ```toml # gradle/libs.versions.toml [libraries] tolgee = { group = "io.tolgee.mobile-kotlin-sdk", name = "core", version.ref = "tolgee" } ``` -------------------------------- ### Implement Locale Switching Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/compose/README.md Create a UI component to change the active locale and observe changes. ```kotlin @Composable fun LocaleSwitcher() { val tolgee = Tolgee.instance val currentLocale = tolgee.changeFlow.mapLatest { tolgee.getLocale() }.collectAsState(initial = tolgee.getLocale()) Row { Text(text = stringResource(tolgee, R.string.selected_locale, currentLocale.toTag("-"))) Button(onClick = { tolgee.setLocale("en") }) { Text("English") } Button(onClick = { tolgee.setLocale("fr") }) { Text("Français") } Button(onClick = { tolgee.setLocale("cs") }) { Text("Čeština") } } } ``` -------------------------------- ### Configure Network Security for Tolgee CDN Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Create a network security config file to allow connections to Tolgee CDN domains. This is required for over-the-air translation updates. ```xml tolgee.io tolg.ee ``` -------------------------------- ### Configure Tolgee Compiler Plugin for Android Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Configure the Tolgee compiler plugin for Android-specific transformations, such as replacing Context.getString and Context.getQuantityString calls. ```kotlin // build.gradle.kts tolgee { // Configure the compiler plugin compilerPlugin { // Android-specific configuration android { // Replace Context.getString calls with Tolgee equivalents replaceGetString.set(true) // default: true // Replace Context.getQuantityString calls with Tolgee equivalents replacePluralString.set(true) // default: true } compose { // Replace androidx.compose.ui.res.stringResource and // org.jetbrains.compose.resources.stringResource calls with Tolgee equivalents stringResource.set(true) // default: true // Replace androidx.compose.ui.res.pluralStringResource and // org.jetbrains.compose.resources.pluralStringResource calls with Tolgee equivalents pluralStringResource.set(true) // default: true } } } ``` -------------------------------- ### Compose Multiplatform Integration with Tolgee Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Integrate Tolgee into your Compose Multiplatform application to manage string resources. Ensure Tolgee is initialized before use. ```kotlin import androidx.compose.material3.Text import androidx.compose.runtime.Composable import io.tolgee.Tolgee import io.tolgee.stringResource import io.tolgee.pluralStringResource import io.tolgee.stringArrayResource import myapp.generated.resources.Res import myapp.generated.resources.welcome_message import myapp.generated.resources.greeting_user import myapp.generated.resources.item_count import myapp.generated.resources.categories @Composable fun MultiplatformApp() { val tolgee = Tolgee.instance // Simple string resource using Compose Multiplatform Res Text(text = stringResource(Res.string.welcome_message)) // String with parameters Text(text = stringResource(Res.string.greeting_user, "Alice")) // Plural strings Text(text = pluralStringResource(Res.plurals.item_count, 3, 3)) // String arrays val items = stringArrayResource(Res.array.categories) items.forEach { Text(text = it) } // Explicit instance usage Text(text = stringResource(tolgee, Res.string.welcome_message)) } // Initialize Tolgee for multiplatform (in common code) fun initTolgee() { Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-prefix" // Custom storage provider for your platform if needed } defaultLanguage("en") } } ``` -------------------------------- ### Configure Formatters for Translations Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Configure the translation parsing formatters within the Tolgee initialization. Supports Sprintf (default) and ICU formatting. ```kotlin Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" // Configure formatters for parsing translations from CDN format(Tolgee.Formatter.Sprintf) // Android SDK formatting (default) // format(Tolgee.Formatter.ICU) // Tolgee Native Flat JSON formatting } } ``` -------------------------------- ### Implement Custom Storage Provider in Kotlin Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Define a custom storage provider by implementing TolgeeStorageProvider to handle translation caching. Use the provider during Tolgee initialization or utilize the built-in Android file-based storage. ```kotlin import io.tolgee.storage.TolgeeStorageProvider // Custom storage provider implementation class CustomStorageProvider : TolgeeStorageProvider { override fun put(name: String, data: ByteArray) { // Store translation data with the given name // name is typically the locale tag (e.g., "en.json") myDatabase.store(name, data) } override fun get(name: String): ByteArray? { // Retrieve cached translation data // Return null if not found return myDatabase.retrieve(name) } } // Use custom storage in initialization Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-prefix" storage = CustomStorageProvider() } } // Android: Use built-in file-based storage import io.tolgee.storage.TolgeeStorageProviderAndroid Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-prefix" storage = TolgeeStorageProviderAndroid( context = applicationContext, versionCode = BuildConfig.VERSION_CODE, // Invalidates cache on app update path = "tolgee/translations" // Optional custom path ) } } ``` -------------------------------- ### Apply Tolgee Plugin in build.gradle.kts Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/gradle-plugin/README.md Apply the Tolgee plugin in your project's build.gradle.kts file using the alias from the version catalog. ```kotlin // build.gradle.kts plugins { alias(libs.plugins.tolgee) } ``` -------------------------------- ### Run Specific Gradle Plugin Tests Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/AGENTS.md Executes specific tests within the gradle-plugin module, filtering by the 'TolgeeTest' class. Ensure you are in the project root directory. ```bash ./gradlew :gradle-plugin:test --tests "TolgeeTest" ``` -------------------------------- ### Automate Translation with TolgeeContextWrapper Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Wrap the Activity context to intercept getString calls and enable automatic re-translation. ```kotlin import android.content.Context import android.os.Bundle import androidx.activity.ComponentActivity import io.tolgee.Tolgee import io.tolgee.TolgeeContextWrapper class MainActivity : ComponentActivity() { val tolgee = Tolgee.instance // Wrap the base context to automatically translate getString calls override fun attachBaseContext(newBase: Context?) { super.attachBaseContext(TolgeeContextWrapper.wrap(newBase)) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // These calls are automatically translated via the wrapped context title = getString(R.string.app_name) // Listen for translation changes lifecycleScope.launch { tolgee.changeFlow.collect { // Re-translate views without recreating Activity tolgee.retranslate(this@MainActivity) // Update title manually setTitle(R.string.app_name) } } } override fun onStart() { super.onStart() // Preload translations for current locale tolgee.preload(this) } } // Alternative: Wrap with explicit Tolgee instance class CustomActivity : ComponentActivity() { override fun attachBaseContext(newBase: Context?) { val customTolgee = Tolgee.new { contentDelivery { url = "https://cdn.tolg.ee/custom-prefix" } } super.attachBaseContext(TolgeeContextWrapper.wrap(newBase, customTolgee)) } } ``` -------------------------------- ### Use Android Extension Functions Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Access translation strings, plurals, and arrays directly from Android Context or Resources using Tolgee extension functions. ```kotlin import android.content.Context import android.content.res.Resources import io.tolgee.Tolgee import io.tolgee.common.getStringT import io.tolgee.common.getQuantityStringT import io.tolgee.common.getTextT import io.tolgee.common.getStringArrayT // Context extensions val text: String = context.getStringT(R.string.welcome) val formatted: String = context.getStringT(R.string.greeting, "John") val styled: CharSequence = context.getTextT(tolgee, R.string.styled_text) // Resources extensions val pluralText: String = resources.getQuantityStringT(R.plurals.items, 5) val pluralFormatted: String = resources.getQuantityStringT(R.plurals.items, 5, 5, "items") val array: Array = resources.getStringArrayT(tolgee, R.array.options) val styledArray: Array = resources.getTextArrayT(tolgee, R.array.styled_options) // With explicit Tolgee instance val customText: String = context.getStringT(customTolgee, R.string.welcome) ``` -------------------------------- ### Preloading Translations with Tolgee Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Preload translations to ensure they are available before they are accessed, improving performance and enabling offline support. Use coroutines or Android lifecycle owners for management. ```kotlin import io.tolgee.Tolgee import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch val tolgee = Tolgee.instance // Preload current locale (suspend function) lifecycleScope.launch { tolgee.preload() // Translations now available via t() method val text = tolgee.t("welcome_message") } // Android convenience method with LifecycleOwner override fun onStart() { super.onStart() tolgee.preload(this) // Launches coroutine automatically } // Preload ALL available locales (for offline support) lifecycleScope.launch { tolgee.preloadAll() // All locales cached according to maxLocalesInMemory setting } // Android convenience method for preloading all locales tolgee.preloadAll(this) ``` -------------------------------- ### Generate API Dump Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/AGENTS.md Generates API dump files for the project, which are used to track binary compatibility. This command should be run after any changes to public APIs. Ensure you are in the project root directory. ```bash ./gradlew apiDump ``` -------------------------------- ### Gradle Plugin for Automatic Code Transformation Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Configure the Tolgee Gradle plugin in your `build.gradle.kts` or `settings.gradle.kts` to automatically transform string resource calls at compile time. This reduces manual code changes. ```kotlin // settings.gradle.kts pluginManagement { repositories { mavenCentral() gradlePluginPortal() } } // build.gradle.kts plugins { id("io.tolgee.mobile-kotlin-sdk") version "1.0.0-alpha04" } // Configure the plugin tolgee { compilerPlugin { android { // Transform Context.getString() calls replaceGetString.set(true) // default: true // Transform Context.getQuantityString() calls replacePluralString.set(true) // default: true } compose { // Transform stringResource() calls stringResource.set(true) // default: true // Transform pluralStringResource() calls pluralStringResource.set(true) // default: true } } } // Original code (automatically transformed at compile time): // val text = context.getString(R.string.welcome) // Becomes: // val text = Tolgee.instance.t(context, R.string.welcome) // Original Compose code: // Text(text = stringResource(R.string.welcome)) // Becomes: // Text(text = io.tolgee.stringResource(R.string.welcome)) ``` -------------------------------- ### Perform basic translations with t() Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Retrieve immediate translations using the t() method. Requires calling preload() before use to ensure translations are cached. ```kotlin import io.tolgee.Tolgee import io.tolgee.model.TolgeeMessageParams // Get the singleton Tolgee instance val tolgee = Tolgee.instance // Simple translation by key val welcomeText: String? = tolgee.t("welcome_message") // Returns: "Welcome to our app!" // Translation with indexed parameters (Sprintf style) val greeting: String? = tolgee.t( key = "greeting_user", parameters = TolgeeMessageParams.Indexed("John", 5) ) // For key "Hello %s, you have %d messages" returns: "Hello John, you have 5 messages" // Translation with named parameters (ICU style) val orderInfo: String? = tolgee.t( key = "order_status", parameters = TolgeeMessageParams.Mapped( mapOf("orderId" to "12345", "status" to "shipped") ) ) // For key "Order {orderId} is {status}" returns: "Order 12345 is shipped" // Get string array val menuItems: List = tolgee.tArray("menu_items") // Returns: ["Home", "Settings", "Profile", "Logout"] ``` -------------------------------- ### Translate Android Resources with Tolgee Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Use Tolgee instance methods to translate string, plural, and array resources, or observe changes via Flow. ```kotlin import android.content.Context import io.tolgee.Tolgee import io.tolgee.TolgeeAndroid import kotlinx.coroutines.flow.collect val tolgee = Tolgee.instance as TolgeeAndroid // Immediate translation with Android resource ID val appName: String = tolgee.t(context, R.string.app_name) // Translation with format arguments val welcomeUser: String = tolgee.t( context, R.string.welcome_user, "John" // Format argument ) // For resource "Welcome, %s!" returns: "Welcome, John!" // Plural strings val itemCount: String = tolgee.tPlural( resources, R.plurals.item_count, quantity = 5, 5 // Format argument for the count ) // Returns appropriate plural form: "5 items" // String arrays val menuItems: List = tolgee.tArray(resources, R.array.menu_items) // Flow-based translation with resource ID lifecycleScope.launch { tolgee.tFlow(context, R.string.description).collect { text -> descriptionTextView.text = text } } // Styled text (preserves CharSequence formatting on fallback) val styledText: CharSequence = tolgee.tStyled(context, R.string.styled_message) ``` -------------------------------- ### Locale Management with Tolgee Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Manage locale switching and access locale information using the Tolgee SDK. Supports BCP 47 tags and provides listeners for changes. ```kotlin import io.tolgee.Tolgee import de.comahe.i18n4k.Locale import de.comahe.i18n4k.forLocaleTag import kotlinx.coroutines.flow.collect val tolgee = Tolgee.instance // Set locale using string tag tolgee.setLocale("fr") tolgee.setLocale("zh-Hans-CN") // Set locale using Locale object tolgee.setLocale(forLocaleTag("de")) // Get current locale val currentLocale: Locale = tolgee.getLocale() val localeTag: String = currentLocale.toTag("-") // "en-US" // Get system locale val systemLocale: Locale = Tolgee.systemLocale // Get available locales (after preload) val availableLocales: List? = tolgee.availableLocales val availableTags: List? = tolgee.availableLocaleTags // Returns: ["en", "fr", "de", "cs"] // Listen for locale/translation changes lifecycleScope.launch { tolgee.changeFlow.collect { // Locale or translations changed val newLocale = tolgee.getLocale() updateUI() } } // For Java interoperability: use ChangeListener tolgee.addChangeListener(object : Tolgee.ChangeListener { override fun onTranslationsChanged() { // Update UI on main thread runOnUiThread { updateUI() } } }) ``` -------------------------------- ### Wrap Activity Context with TolgeeContextWrapper Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Wraps the Activity's base context with TolgeeContextWrapper to enable Tolgee to override getString-like methods, ensuring translations are handled correctly. ```kotlin // Wrap the context of your Activity to override what is returned by getString-like methods class MyActivity : Activity() { override fun attachBaseContext(newBase: Context?) { super.attachBaseContext(TolgeeContextWrapper.wrap(newBase)) } } ``` -------------------------------- ### Integrate Tolgee with Jetpack Compose Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Use Tolgee-provided composable functions to automatically update UI elements when translations change. ```kotlin import androidx.compose.material3.Text import androidx.compose.runtime.Composable import io.tolgee.stringResource import io.tolgee.pluralStringResource import io.tolgee.stringArrayResource @Composable fun WelcomeScreen() { // Simple string resource - automatically updates on locale change Text(text = stringResource(R.string.welcome_message)) } @Composable fun UserGreeting(username: String) { // String with format arguments Text(text = stringResource(R.string.greeting_user, username)) // For resource "Hello, %s!" displays: "Hello, John!" } @Composable fun ItemCounter(count: Int) { // Plural string resource Text( text = pluralStringResource( R.plurals.item_count, quantity = count, count // Format argument ) ) // Displays: "1 item" or "5 items" based on count } @Composable fun CategoryList() { // String array resource val categories = stringArrayResource(R.array.categories) categories.forEach { category -> Text(text = category) } } @Composable fun ExplicitTolgeeUsage() { // Use explicit Tolgee instance (useful for dependency injection) val tolgee = Tolgee.instance Text(text = stringResource(tolgee, R.string.app_name)) } ``` -------------------------------- ### Include Tolgee Dependency in build.gradle.kts Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Include the Tolgee core library in your project's dependencies within the build.gradle.kts file. ```kotlin // build.gradle.kts dependencies { implementation(libs.tolgee) } ``` -------------------------------- ### Configure Default Language Fallback Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Sets the fallback language to be used when the user's specific locale is unavailable in the project. ```kotlin Tolgee.init { contentDelivery { url = "https://cdn.tolg.ee/your-cdn-url-prefix" } defaultLanguage("en") // Fallback to English when user's locale is unavailable } ``` -------------------------------- ### Use Translations in Compose Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/compose/README.md Display translated strings, parameters, and plurals in your UI components. ```kotlin @Composable fun SimpleText() { // Use the stringResource extension function Text(text = stringResource(R.string.welcome_message)) } @Composable fun TextWithParameters(name: String) { // Pass parameters to your translations Text(text = stringResource(R.string.welcome_user, name)) } @Composable fun PluralText(count: Int) { // Handle plural forms Text(text = pluralStringResource(R.plurals.item_count, count, count)) } ``` ```kotlin @Composable fun SimpleText() { // Use the stringResource extension function with Res Text(text = stringResource(Res.string.welcome_message)) } @Composable fun TextWithParameters(name: String) { // Pass parameters to your translations Text(text = stringResource(Res.string.welcome_user, name)) } @Composable fun PluralText(count: Int) { // Handle plural forms Text(text = pluralStringResource(Res.plurals.item_count, count, count)) } ``` -------------------------------- ### Add Compose Module Dependency (Jetpack Compose) Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/README.md Add the compose module dependency to your build.gradle.kts file for Jetpack Compose or Compose Multiplatform projects. This module extends the core functionality for Compose UI. ```toml # gradle/libs.versions.toml [libraries] tolgee = { group = "io.tolgee.mobile-kotlin-sdk", name = "compose", version.ref = "tolgee" } ``` ```kotlin // build.gradle.kts dependencies { implementation(libs.tolgee) } ``` -------------------------------- ### Use Explicit Tolgee Instance Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/compose/README.md Pass a specific Tolgee instance to translation functions instead of using the singleton. ```kotlin @Composable fun ExplicitInstance() { val tolgee = remember { /* your custom Tolgee instance */ } Text(text = stringResource(tolgee, Res.string.welcome_message)) } ``` -------------------------------- ### Observe Locale Changes Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/compose/README.md Trigger recomposition when the application locale changes. ```kotlin @Composable fun LocaleAwareComponent() { val tolgee = Tolgee.instance // This will cause recomposition when the locale changes val currentLocale = tolgee.changeFlow.mapLatest { tolgee.getLocale() }.collectAsState(initial = tolgee.getLocale()) // Your UI that depends on the current locale Text(text = currentLocale.toTag("-")) } ``` -------------------------------- ### Add Network Security Config to AndroidManifest.xml Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Reference the network security config file in your AndroidManifest.xml to enable the defined network security settings. ```xml <!-- Add this line to your existing application tag --> ``` -------------------------------- ### Preload Translations Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/core/README.md Preloads translations for the current locale within an Activity lifecycle method to ensure availability. ```kotlin // Preload translations for the current locale from Activity override fun onStart() { super.onStart() tolgee.preload(this) } ``` -------------------------------- ### Add Core Module Dependency (Traditional Android) Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/README.md Add the core module dependency to your build.gradle.kts file for traditional Android development. This module provides base functionality for fetching and querying translations. ```toml # gradle/libs.versions.toml [libraries] tolgee = { group = "io.tolgee.mobile-kotlin-sdk", name = "core", version.ref = "tolgee" } ``` ```kotlin // build.gradle.kts dependencies { implementation(libs.tolgee) } ``` -------------------------------- ### Implement reactive translations with tFlow() Source: https://context7.com/tolgee/tolgee-mobile-kotlin-sdk/llms.txt Use tFlow() to observe translation changes as a Kotlin Flow, enabling automatic UI updates when locales or translations change. ```kotlin import io.tolgee.Tolgee import io.tolgee.model.TolgeeMessageParams import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch val tolgee = Tolgee.instance // Collect translation updates in a coroutine lifecycleScope.launch { tolgee.tFlow("welcome_message").collect { text -> // UI automatically updates when translations load or locale changes welcomeTextView.text = text } } // Flow with parameters lifecycleScope.launch { tolgee.tFlow( key = "items_count", parameters = TolgeeMessageParams.Indexed(42) ).collect { text -> countTextView.text = text } } // String array as Flow lifecycleScope.launch { tolgee.tArrayFlow("categories").collect { items -> categoryAdapter.submitList(items) } } ``` -------------------------------- ### Configure Android Network Security Source: https://github.com/tolgee/tolgee-mobile-kotlin-sdk/blob/master/compose/README.md Allow network access to Tolgee CDN domains in your Android application. ```xml tolgee.io tolg.ee ``` ```xml ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.