Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 6e7934f

Browse files
authored
Add Int.getQuantityString (#62)
* Add Int.getQuantityString * Use resources of app * Use english locale * Fix format
1 parent 5c92a5a commit 6e7934f

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

common/src/main/kotlin/io/goooler/demoapp/common/CommonApplication.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import com.scwang.smart.refresh.layout.SmartRefreshLayout
77
import io.goooler.demoapp.base.core.BaseApplication
88
import io.goooler.demoapp.common.util.CrashHandler
99
import io.goooler.demoapp.common.util.ImageLoader
10+
import java.util.Locale
1011

1112
abstract class CommonApplication : BaseApplication() {
1213

1314
override fun initImmediately() {
1415
super.initImmediately()
1516
app = this
17+
resources.configuration.setLocale(Locale.ENGLISH)
1618
CrashHandler.init()
1719
initImageLoader()
1820
initWebView()

common/src/main/kotlin/io/goooler/demoapp/common/util/CommonExtensions.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.annotation.ColorRes
1414
import androidx.annotation.Dimension
1515
import androidx.annotation.DrawableRes
1616
import androidx.annotation.MainThread
17+
import androidx.annotation.PluralsRes
1718
import androidx.annotation.Px
1819
import androidx.annotation.StringRes
1920
import androidx.databinding.ViewDataBinding
@@ -222,6 +223,13 @@ fun @receiver:StringRes Int.getString(): String? = try {
222223
null
223224
}
224225

226+
fun @receiver:PluralsRes Int.getQuantityString(num: Int): String? = try {
227+
CommonApplication.app.resources.getQuantityString(this, num, num)
228+
} catch (e: Exception) {
229+
e.printStackTrace()
230+
null
231+
}
232+
225233
fun @receiver:StringRes Int.formatString(vararg args: Any): String =
226234
String.format(getString().orEmpty(), args)
227235

detail/src/main/kotlin/io/goooler/demoapp/detail/ui/DetailPage.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import androidx.compose.ui.tooling.preview.Preview
2929
import androidx.compose.ui.unit.dp
3030
import com.google.accompanist.swiperefresh.SwipeRefresh
3131
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
32+
import io.goooler.demoapp.common.util.getQuantityString
3233
import io.goooler.demoapp.common.util.showToast
34+
import io.goooler.demoapp.detail.R
3335
import io.goooler.demoapp.detail.model.RepoDetailModel
3436

3537
@Composable
@@ -73,7 +75,7 @@ fun DetailPage(model: RepoDetailModel, onForkClick: () -> Unit) {
7375
Spacer(modifier = Modifier.height(5.dp))
7476
Row {
7577
Button(onClick = {
76-
"All ${model.starsCount} stars".showToast()
78+
R.plurals.detail_star_count_tip.getQuantityString(model.starsCount)?.showToast()
7779
}) {
7880
Icon(
7981
Icons.Filled.Star,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<plurals name="detail_star_count_tip">
5+
<item quantity="one">All %d star.</item>
6+
<item quantity="other">All %d stars.</item>
7+
</plurals>
8+
9+
</resources>

0 commit comments

Comments
 (0)