Resource management library for Compose Multiplatform applications providing type-safe access to images, strings, fonts, and drawable assets across all platforms.
—
String resources provide type-safe access to localized text content with formatting support. The library automatically selects the appropriate string variant based on the current locale and environment settings.
class StringResource(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)A string resource represents a localized text entry identified by a unique key. Each resource contains multiple variants for different locales and configurations.
@Composable
fun stringResource(resource: StringResource): StringLoads a string resource within a Composable context. The function automatically uses the current environment to select the appropriate string variant.
Usage:
@Composable
fun WelcomeScreen() {
val title = stringResource(Res.string.welcome_title)
Text(text = title)
}@Composable
fun stringResource(resource: StringResource, vararg formatArgs: Any): StringLoads a formatted string resource with variable arguments. Format placeholders in the string are replaced with the provided arguments.
Parameters:
resource - The string resource containing format placeholdersformatArgs - Variable arguments to substitute into the stringUsage:
@Composable
fun UserGreeting(userName: String, messageCount: Int) {
val greeting = stringResource(
Res.string.user_greeting,
userName,
messageCount
)
Text(text = greeting)
}String Resource Example:
<string name="user_greeting">Hello %1$s, you have %2$d new messages</string>suspend fun getString(resource: StringResource): StringLoads a string resource outside of a Composable context using the system's resource environment.
Usage:
suspend fun loadWelcomeMessage(): String {
return getString(Res.string.welcome_message)
}suspend fun getString(resource: StringResource, vararg formatArgs: Any): StringLoads a formatted string resource outside of a Composable context.
Usage:
suspend fun createNotification(userName: String, count: Int): String {
return getString(Res.string.notification_message, userName, count)
}suspend fun getString(environment: ResourceEnvironment, resource: StringResource): String
suspend fun getString(
environment: ResourceEnvironment,
resource: StringResource,
vararg formatArgs: Any
): StringLoads string resources using a specific resource environment rather than the system default. This is useful for testing or when you need to load resources for a different locale/configuration.
Parameters:
environment - The resource environment specifying locale, theme, and densityresource - The string resource to loadformatArgs - Optional formatting argumentsUsage:
suspend fun getLocalizedString(locale: Locale): String {
val environment = ResourceEnvironment(
LanguageQualifier(locale.language),
RegionQualifier(locale.country),
ThemeQualifier.LIGHT,
DensityQualifier.MDPI
)
return getString(environment, Res.string.localized_content)
}The library supports standard string formatting patterns:
%1$s, %2$d, %3$fFormat Specifiers:
%s - String%d - Integer%f - Float%1$s - First argument as string%2$d - Second argument as integerString resources support full localization through resource qualifiers:
Directory Structure:
res/
├── values/strings.xml # Default (English)
├── values-es/strings.xml # Spanish
├── values-fr/strings.xml # French
├── values-zh-rCN/strings.xml # Chinese (China)
└── values-ar/strings.xml # ArabicAutomatic Selection: The library automatically selects the best matching string resource based on:
// Throws IllegalArgumentException if resource ID is not found
val text = stringResource(Res.string.missing_resource)Common Exceptions:
IllegalArgumentException - Resource ID not found in any resource fileMissingResourceException - Resource file cannot be readUse descriptive resource names:
Res.string.user_profile_title // Good
Res.string.text1 // BadProvide meaningful default text:
<string name="loading_message">Loading content...</string>Use format arguments for dynamic content:
stringResource(Res.string.items_count, itemCount)Always provide default (English) strings:
Every string resource should have a default variant in values/strings.xml
Handle pluralization with PluralStringResource: For quantity-dependent strings, use plural string resources instead of conditional logic.
Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-compose-components--components-resources