diff --git a/lyricist-processor-compose/src/main/java/cafe/adriel/lyricist/processor/internal/LyricistSymbolProcessor.kt b/lyricist-processor-compose/src/main/java/cafe/adriel/lyricist/processor/internal/LyricistSymbolProcessor.kt index f61d390..2cd757e 100644 --- a/lyricist-processor-compose/src/main/java/cafe/adriel/lyricist/processor/internal/LyricistSymbolProcessor.kt +++ b/lyricist-processor-compose/src/main/java/cafe/adriel/lyricist/processor/internal/LyricistSymbolProcessor.kt @@ -124,6 +124,10 @@ internal class LyricistSymbolProcessor( |) { | ProvideStrings(lyricist, Local$fileName, content) |} + | + |$visibility fun getLocale$fileName(locale: Locale = Locale.current): $stringsClassOutput { + | return $stringsName[locale.toLanguageTag()] ?: $defaultStringsOutput + |} """.trimMargin().toByteArray() ) } diff --git a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt index 5cff7d1..d185559 100644 --- a/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt +++ b/lyricist-processor-xml/src/main/java/cafe/adriel/lyricist/processor/xml/internal/LyricistXmlSymbolProcessor.kt @@ -168,6 +168,14 @@ internal class LyricistXmlSymbolProcessor( """.trimMargin().toByteArray() ) } + stream.write( + """ + | + |public fun getLocale$fileName(locale: Locale = Locale.current): $fileName { + | return $stringsName[locale.toLanguageTag()] ?: $defaultStringsOutput + |} + """.trimMargin().toByteArray() + ) } } diff --git a/sample/src/main/java/cafe/adriel/lyricist/sample/MainActivity.kt b/sample/src/main/java/cafe/adriel/lyricist/sample/MainActivity.kt index 5b8fb20..9990adc 100644 --- a/sample/src/main/java/cafe/adriel/lyricist/sample/MainActivity.kt +++ b/sample/src/main/java/cafe/adriel/lyricist/sample/MainActivity.kt @@ -1,6 +1,7 @@ package cafe.adriel.lyricist.sample import android.os.Bundle +import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Arrangement @@ -27,6 +28,7 @@ import cafe.adriel.lyricist.XmlStrings import cafe.adriel.lyricist.custompackage.LocalMultiModuleStrings import cafe.adriel.lyricist.custompackage.ProvideMultiModuleStrings import cafe.adriel.lyricist.custompackage.rememberMultiModuleStrings +import cafe.adriel.lyricist.getLocaleStrings import cafe.adriel.lyricist.rememberStrings import cafe.adriel.lyricist.rememberXmlStrings import cafe.adriel.lyricist.sample.multimodule.strings.MultiModuleStrings @@ -70,7 +72,14 @@ class MainActivity : ComponentActivity() { SampleXmlStrings() } } + + item { + Row(modifier = Modifier.padding(vertical = 16.dp)) { + ShowToastButton() + } + } } + Row( horizontalArrangement = Arrangement.SpaceEvenly, modifier = Modifier @@ -160,6 +169,23 @@ class MainActivity : ComponentActivity() { } } + @Composable + fun ShowToastButton( + modifier: Modifier = Modifier + ) { + Button( + onClick = { + showToast() + }, modifier = modifier + ) { + Text(text = "Show Toast") + } + } + + private fun showToast() { + Toast.makeText(this, getLocaleStrings().nonComposeAlert, Toast.LENGTH_SHORT).show() + } + @Composable fun SwitchLocaleButton( languageTag: String, diff --git a/sample/src/main/java/cafe/adriel/lyricist/sample/strings/EnStrings.kt b/sample/src/main/java/cafe/adriel/lyricist/sample/strings/EnStrings.kt index b2a0979..54f2629 100644 --- a/sample/src/main/java/cafe/adriel/lyricist/sample/strings/EnStrings.kt +++ b/sample/src/main/java/cafe/adriel/lyricist/sample/strings/EnStrings.kt @@ -31,5 +31,7 @@ internal val EnStrings = Strings( "I have $value apples" }, - list = listOf("Avocado", "Pineapple", "Plum") + list = listOf("Avocado", "Pineapple", "Plum"), + + nonComposeAlert = "This is a sample toast", ) diff --git a/sample/src/main/java/cafe/adriel/lyricist/sample/strings/PtStrings.kt b/sample/src/main/java/cafe/adriel/lyricist/sample/strings/PtStrings.kt index d56ef6a..d447746 100644 --- a/sample/src/main/java/cafe/adriel/lyricist/sample/strings/PtStrings.kt +++ b/sample/src/main/java/cafe/adriel/lyricist/sample/strings/PtStrings.kt @@ -31,5 +31,7 @@ internal val PtStrings = Strings( "Eu $value maças" }, - list = listOf("Abacate", "Abacaxi", "Ameixa") + list = listOf("Abacate", "Abacaxi", "Ameixa"), + + nonComposeAlert = "Este é um exemplo de brinde", ) diff --git a/sample/src/main/java/cafe/adriel/lyricist/sample/strings/Strings.kt b/sample/src/main/java/cafe/adriel/lyricist/sample/strings/Strings.kt index 3181d0d..93138b8 100644 --- a/sample/src/main/java/cafe/adriel/lyricist/sample/strings/Strings.kt +++ b/sample/src/main/java/cafe/adriel/lyricist/sample/strings/Strings.kt @@ -7,5 +7,6 @@ internal data class Strings( val annotated: AnnotatedString, val parameter: (locale: String) -> String, val plural: (count: Int) -> String, - val list: List + val list: List, + val nonComposeAlert:String )