Skip to content

Commit 35ef3ce

Browse files
committed
feat: bit more docs within demos
1 parent 7b9b764 commit 35ef3ce

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

  • demo
    • exampleandroid/src/main/java/io/tolgee/demo/exampleandroid
    • examplejetpack/src/main/java/io/tolgee/demo/examplejetpack
    • multiplatform-compose/src/commonMain/kotlin/io/tolgee/demo/compose

demo/exampleandroid/src/main/java/io/tolgee/demo/exampleandroid/MainActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class MainActivity : ComponentActivity() {
1616
val tolgee = Tolgee.instance
1717

1818
override fun attachBaseContext(newBase: Context?) {
19+
// Wrapping base context will make sure getString calls will use tolgee
20+
// even for instances which cannot be replaced automatically by the compiler
1921
super.attachBaseContext(TolgeeContextWrapper.wrap(newBase))
2022
}
2123

@@ -24,12 +26,14 @@ class MainActivity : ComponentActivity() {
2426

2527
lifecycleScope.launch {
2628
tolgee.changeFlow.collect {
29+
// we want to reload activity after a language change
2730
recreate()
2831
}
2932
}
3033

3134
setContentView(R.layout.activity_main)
3235

36+
// Make sure the app title stays updated
3337
setTitle(R.string.app_name)
3438

3539
val name = findViewById<TextView>(R.id.app_name_text)
@@ -41,6 +45,7 @@ class MainActivity : ComponentActivity() {
4145
val buttonFr = findViewById<Button>(R.id.button_fr)
4246
val buttonCs = findViewById<Button>(R.id.button_cs)
4347

48+
// Update texts within the app with translated ones
4449
name.text = getString(R.string.app_name)
4550
basic.text = getString(R.string.description)
4651
parameter.text = getString(R.string.percentage_placeholder, "87")
@@ -63,6 +68,10 @@ class MainActivity : ComponentActivity() {
6368

6469
override fun onStart() {
6570
super.onStart()
71+
72+
// Make sure the translations are loaded
73+
// This function will initiate translations fetching in the background and
74+
// will trigger changeFlow whenever updated translations are available
6675
Tolgee.instance.preload(this)
6776
}
6877
}

demo/examplejetpack/src/main/java/io/tolgee/demo/examplejetpack/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class MainActivity : ComponentActivity() {
5656

5757
@Composable
5858
fun BasicText(modifier: Modifier = Modifier) {
59+
// Use tolgee version of stringResource composable
5960
Text(
6061
text = stringResource(R.string.description),
6162
modifier = modifier
@@ -64,6 +65,7 @@ fun BasicText(modifier: Modifier = Modifier) {
6465

6566
@Composable
6667
fun ParametrizedText(name: String, modifier: Modifier = Modifier) {
68+
// Passing parameters for the stringResource is supported
6769
Text(
6870
text = stringResource(R.string.percentage_placeholder, name),
6971
modifier = modifier
@@ -72,6 +74,7 @@ fun ParametrizedText(name: String, modifier: Modifier = Modifier) {
7274

7375
@Composable
7476
fun PluralText(count: Int, param: Int, modifier: Modifier = Modifier) {
77+
// Plurals are also supported
7578
Text(
7679
text = pluralStringResource(R.plurals.plr_test_placeholder_2, count, param, count, "Plurals"),
7780
modifier = modifier

demo/multiplatform-compose/src/commonMain/kotlin/io/tolgee/demo/compose/App.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,31 @@ fun App() {
1919

2020
MaterialTheme {
2121
Column {
22+
// Use tolgee version of stringResource composable
23+
// (or alternatively enable tolgee compiler plugin which will convert the calls automatically)
2224
Text(text = stringResource(Res.string.description))
23-
Text(text = stringResource(Res.string.percentage_placeholder))
25+
Text(text = stringResource(Res.string.percentage_placeholder, "87"))
2426
Button(
2527
onClick = {
26-
tolgee.setLocale("en-US")
28+
tolgee.setLocale("en")
2729
}
2830
) {
2931
Text(text = "English")
3032
}
33+
Button(
34+
onClick = {
35+
tolgee.setLocale("fr")
36+
}
37+
) {
38+
Text(text = "Français")
39+
}
40+
Button(
41+
onClick = {
42+
tolgee.setLocale("cs")
43+
}
44+
) {
45+
Text(text = "Čeština")
46+
}
3147
}
3248
}
3349
}

0 commit comments

Comments
 (0)