Skip to content

Commit 7b9b764

Browse files
committed
feat: android jetpack demo
1 parent d38f8be commit 7b9b764

36 files changed

Lines changed: 723 additions & 4 deletions

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ plugins {
2222
// Ignore API on demo projects
2323
apiValidation {
2424
ignoredProjects.add("demo")
25-
ignoredProjects.add("exampleandroid")
2625
ignoredProjects.add("multiplatform-compose")
26+
ignoredProjects.add("exampleandroid")
27+
ignoredProjects.add("examplejetpack")
2728
}
2829

2930
dependencies {

compose/src/androidMain/kotlin/io/tolgee/ComposeTolgee.android.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ fun pluralStringResource(tolgee: Tolgee, @PluralsRes id: Int, quantity: Int, var
134134
).value
135135
}
136136

137+
@Composable
138+
fun pluralStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): String {
139+
val instance = Tolgee.instanceOrNull ?: return androidx.compose.ui.res.pluralStringResource(id, quantity, *formatArgs)
140+
141+
return pluralStringResource(instance, id, quantity, *formatArgs)
142+
}
143+
137144
@Composable
138145
fun stringArrayResource(tolgee: Tolgee, @ArrayRes id: Int): Array<String> {
139146
val context = LocalContext.current

demo/examplejetpack/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

demo/examplejetpack/.tolgeerc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "https://docs.tolgee.io/cli-schema.json",
3+
"projectId": 20161,
4+
"format": "ANDROID_XML",
5+
"push": {
6+
"filesTemplate": "./src/main/res/values-{languageTag}/strings.xml",
7+
"language": ["en", "cs", "fr", "sv"]
8+
},
9+
"pull": {
10+
"path": ".",
11+
"fileStructureTemplate": "./src/main/res/values-{languageTag}/strings.{extension}"
12+
}
13+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.android)
4+
alias(libs.plugins.compose.compiler)
5+
}
6+
7+
android {
8+
namespace = "io.tolgee.demo.examplejetpack"
9+
compileSdk = 35
10+
11+
defaultConfig {
12+
applicationId = "io.tolgee.demo.examplejetpack"
13+
minSdk = 24
14+
targetSdk = 35
15+
versionCode = 1
16+
versionName = "1.0"
17+
}
18+
19+
buildTypes {
20+
release {
21+
isMinifyEnabled = false
22+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility = JavaVersion.VERSION_11
27+
targetCompatibility = JavaVersion.VERSION_11
28+
}
29+
kotlinOptions {
30+
jvmTarget = "11"
31+
}
32+
buildFeatures {
33+
compose = true
34+
}
35+
}
36+
37+
dependencies {
38+
implementation(libs.android)
39+
implementation(libs.lifecycle.runtime.ktx)
40+
implementation(libs.activity.compose)
41+
implementation(platform(libs.compose.bom))
42+
implementation(libs.ui)
43+
implementation(libs.ui.graphics)
44+
implementation(libs.ui.tooling.preview)
45+
implementation(libs.material3)
46+
debugImplementation(libs.ui.tooling)
47+
debugImplementation(libs.ui.test.manifest)
48+
implementation(project(":compose"))
49+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application
5+
android:name=".MyApplication"
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.Composetolgee">
12+
<activity
13+
android:name=".MainActivity"
14+
android:exported="true"
15+
android:label="@string/app_name"
16+
android:theme="@style/Theme.Composetolgee">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN"/>
19+
20+
<category android:name="android.intent.category.LAUNCHER"/>
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package io.tolgee.demo.examplejetpack
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
7+
import androidx.compose.foundation.layout.Column
8+
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.material3.Scaffold
11+
import androidx.compose.material3.SegmentedButton
12+
import androidx.compose.material3.SegmentedButtonDefaults
13+
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
14+
import androidx.compose.material3.SingleChoiceSegmentedButtonRowScope
15+
import androidx.compose.material3.Text
16+
import androidx.compose.runtime.Composable
17+
import androidx.compose.runtime.collectAsState
18+
import androidx.compose.ui.Modifier
19+
import androidx.compose.ui.tooling.preview.Preview
20+
import androidx.lifecycle.lifecycleScope
21+
import io.tolgee.Tolgee
22+
import io.tolgee.demo.examplejetpack.ui.theme.ComposetolgeeTheme
23+
import kotlinx.coroutines.launch
24+
import de.comahe.i18n4k.forLocaleTag
25+
import kotlinx.coroutines.flow.mapLatest
26+
import io.tolgee.stringResource
27+
import io.tolgee.pluralStringResource
28+
29+
class MainActivity : ComponentActivity() {
30+
override fun onCreate(savedInstanceState: Bundle?) {
31+
super.onCreate(savedInstanceState)
32+
33+
lifecycleScope.launch {
34+
// Keep the Activity title updated
35+
Tolgee.instance.tFlow(this@MainActivity, R.string.app_name).collect { title = it }
36+
}
37+
38+
enableEdgeToEdge()
39+
setContent {
40+
ComposetolgeeTheme {
41+
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
42+
Column(modifier = Modifier.padding(innerPadding)) {
43+
BasicText()
44+
ParametrizedText(name = "Android")
45+
PluralText(
46+
count = 10,
47+
param = 10,
48+
)
49+
LocaleSwitcher()
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
@Composable
58+
fun BasicText(modifier: Modifier = Modifier) {
59+
Text(
60+
text = stringResource(R.string.description),
61+
modifier = modifier
62+
)
63+
}
64+
65+
@Composable
66+
fun ParametrizedText(name: String, modifier: Modifier = Modifier) {
67+
Text(
68+
text = stringResource(R.string.percentage_placeholder, name),
69+
modifier = modifier
70+
)
71+
}
72+
73+
@Composable
74+
fun PluralText(count: Int, param: Int, modifier: Modifier = Modifier) {
75+
Text(
76+
text = pluralStringResource(R.plurals.plr_test_placeholder_2, count, param, count, "Plurals"),
77+
modifier = modifier
78+
)
79+
}
80+
81+
val LOCALES = arrayOf(
82+
"en" to "English",
83+
"fr" to "Français",
84+
"cs" to "Čeština",
85+
)
86+
87+
@Composable
88+
fun LocaleSwitcher(modifier: Modifier = Modifier) {
89+
SingleChoiceSegmentedButtonRow(modifier = modifier) {
90+
LOCALES.forEachIndexed { index, (locale, name) ->
91+
ChangeLocaleButton(
92+
index = index,
93+
count = LOCALES.size,
94+
locale = locale,
95+
name = name,
96+
)
97+
}
98+
}
99+
}
100+
101+
fun Tolgee.isLocaleSelected(locale: String, fallback: Boolean): Boolean {
102+
return getLocale() == forLocaleTag(locale) || (fallback && LOCALES.none { (locale, _) ->
103+
getLocale() == forLocaleTag(locale)
104+
})
105+
}
106+
107+
@Composable
108+
fun isLocaleSelected(locale: String, fallback: Boolean): Boolean {
109+
val tolgee = Tolgee.instance
110+
return tolgee.changeFlow.mapLatest {
111+
tolgee.isLocaleSelected(locale, fallback)
112+
}.collectAsState(initial = tolgee.isLocaleSelected(locale, fallback)).value
113+
}
114+
115+
@Composable
116+
fun SingleChoiceSegmentedButtonRowScope.ChangeLocaleButton(index: Int, count: Int, locale: String, name: String) {
117+
SegmentedButton(
118+
shape = SegmentedButtonDefaults.itemShape(
119+
index = index,
120+
count = count
121+
),
122+
onClick = {
123+
Tolgee.instance.setLocale(locale)
124+
},
125+
selected = isLocaleSelected(locale, index == 0),
126+
label = { Text(text = name) }
127+
)
128+
}
129+
130+
@Preview(showBackground = true)
131+
@Composable
132+
fun ParametrizedTextPreview() {
133+
ComposetolgeeTheme {
134+
ParametrizedText("Android")
135+
}
136+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.tolgee.demo.examplejetpack
2+
3+
import android.app.Application
4+
import io.tolgee.Tolgee
5+
6+
class MyApplication : Application() {
7+
8+
override fun onCreate() {
9+
super.onCreate()
10+
11+
Tolgee.init {
12+
contentDelivery {
13+
url = "https://cdn.tolg.ee/96eacb8b07382b60c3f94b30405cc49b"
14+
}
15+
}
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.tolgee.demo.examplejetpack.ui.theme
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
val Purple80 = Color(0xFFD0BCFF)
6+
val PurpleGrey80 = Color(0xFFCCC2DC)
7+
val Pink80 = Color(0xFFEFB8C8)
8+
9+
val Purple40 = Color(0xFF6650a4)
10+
val PurpleGrey40 = Color(0xFF625b71)
11+
val Pink40 = Color(0xFF7D5260)

0 commit comments

Comments
 (0)