@@ -6,10 +6,13 @@ package dev.icerock.moko.resources.desc
66
77import dev.icerock.moko.resources.PluralsResource
88import dev.icerock.moko.resources.StringResource
9+ import dev.icerock.moko.resources.objc.pluralizedString
10+ import platform.Foundation.NSString
11+ import platform.Foundation.stringWithFormat
912
1013actual sealed class StringDesc {
1114 actual data class Resource actual constructor(val stringRes : StringResource ) : StringDesc() {
12- override fun toLocalizedString ( formatter : Formatter ): String {
15+ override fun localized ( ): String {
1316 return stringRes.bundle.localizedStringForKey(stringRes.resourceId, null , null )
1417 }
1518 }
@@ -18,21 +21,26 @@ actual sealed class StringDesc {
1821 val stringRes : StringResource ,
1922 val args : List <Any >
2023 ) : StringDesc() {
21- override fun toLocalizedString (formatter : Formatter ): String {
22- val string = stringRes.bundle.localizedStringForKey(stringRes.resourceId, null , null )
23- return formatter.formatString(string, args.toTypedArray())
24- }
25-
2624 actual constructor (stringRes: StringResource , vararg args: Any ) : this (
2725 stringRes,
2826 args.toList()
2927 )
28+
29+ override fun localized (): String {
30+ val string = stringRes.bundle.localizedStringForKey(stringRes.resourceId, null , null )
31+ return stringWithFormat(string, args.toTypedArray())
32+ }
3033 }
3134
3235 actual data class Plural actual constructor(val pluralsRes : PluralsResource , val number : Int ) :
3336 StringDesc () {
34- override fun toLocalizedString (formatter : Formatter ): String {
35- return formatter.plural(pluralsRes, number)
37+
38+ override fun localized (): String {
39+ return pluralizedString(
40+ bundle = pluralsRes.bundle,
41+ resourceId = pluralsRes.resourceId,
42+ number = number
43+ )!!
3644 }
3745 }
3846
@@ -41,41 +49,96 @@ actual sealed class StringDesc {
4149 val number : Int ,
4250 val args : List <Any >
4351 ) : StringDesc() {
44- override fun toLocalizedString (formatter : Formatter ): String {
45- return formatter.formatPlural(pluralsRes, number, args.toTypedArray())
46- }
4752
4853 actual constructor (pluralsRes: PluralsResource , number: Int , vararg args: Any ) : this (
4954 pluralsRes,
5055 number,
5156 args.toList()
5257 )
58+
59+ override fun localized (): String {
60+ val pluralized = pluralizedString(
61+ bundle = pluralsRes.bundle,
62+ resourceId = pluralsRes.resourceId,
63+ number = number
64+ )!!
65+ return stringWithFormat(pluralized, args.toTypedArray())
66+ }
5367 }
5468
5569 actual data class Raw actual constructor(val string : String ) : StringDesc() {
56- override fun toLocalizedString (formatter : Formatter ): String {
70+
71+ override fun localized (): String {
5772 return string
5873 }
5974 }
6075
61- actual data class Composition actual constructor(val args : List <StringDesc >, val separator : String? ) : StringDesc() {
62- override fun toLocalizedString (formatter : Formatter ): String {
63- return StringBuilder ().apply {
64- args.forEachIndexed { index, stringDesc ->
65- if (index != 0 && separator != null ) {
66- append(separator)
67- }
68- append(stringDesc.toLocalizedString(formatter))
69- }
70- }.toString()
76+ actual data class Composition actual constructor(
77+ val args : List <StringDesc >,
78+ val separator : String?
79+ ) : StringDesc() {
80+ override fun localized (): String {
81+ return args.joinToString(separator = separator ? : " " ) { it.localized() }
7182 }
7283 }
7384
74- abstract fun toLocalizedString ( formatter : Formatter ): String
85+ abstract fun localized ( ): String
7586
76- interface Formatter {
77- fun formatString (string : String , args : Array <out Any >): String
78- fun plural (resource : PluralsResource , number : Int ): String
79- fun formatPlural (resource : PluralsResource , number : Int , args : Array <out Any >): String
87+ protected fun stringWithFormat (format : String , args : Array <out Any >): String {
88+ // NSString format works with NSObjects via %@, we should change standard format to %@
89+ val objcFormat = format.replace(Regex (" %[\\ .|\\ d]*[a|b|c|d|e|f|s]" ), " %@" )
90+ // bad but objc interop limited :(
91+ // When calling variadic C functions spread operator is supported only for *arrayOf(...)
92+ return when (args.size) {
93+ 0 -> NSString .stringWithFormat(objcFormat)
94+ 1 -> NSString .stringWithFormat(objcFormat, args[0 ])
95+ 2 -> NSString .stringWithFormat(objcFormat, args[0 ], args[1 ])
96+ 3 -> NSString .stringWithFormat(objcFormat, args[0 ], args[1 ], args[2 ])
97+ 4 -> NSString .stringWithFormat(objcFormat, args[0 ], args[1 ], args[2 ], args[3 ])
98+ 5 -> NSString .stringWithFormat(objcFormat, args[0 ], args[1 ], args[2 ], args[3 ], args[4 ])
99+ 6 -> NSString .stringWithFormat(
100+ objcFormat,
101+ args[0 ],
102+ args[1 ],
103+ args[2 ],
104+ args[3 ],
105+ args[4 ],
106+ args[5 ]
107+ )
108+ 7 -> NSString .stringWithFormat(
109+ objcFormat,
110+ args[0 ],
111+ args[1 ],
112+ args[2 ],
113+ args[3 ],
114+ args[4 ],
115+ args[5 ],
116+ args[6 ]
117+ )
118+ 8 -> NSString .stringWithFormat(
119+ objcFormat,
120+ args[0 ],
121+ args[1 ],
122+ args[2 ],
123+ args[3 ],
124+ args[4 ],
125+ args[5 ],
126+ args[6 ],
127+ args[7 ]
128+ )
129+ 9 -> NSString .stringWithFormat(
130+ objcFormat,
131+ args[0 ],
132+ args[1 ],
133+ args[2 ],
134+ args[3 ],
135+ args[4 ],
136+ args[5 ],
137+ args[6 ],
138+ args[7 ],
139+ args[8 ]
140+ )
141+ else -> throw IllegalArgumentException (" can't handle more then 9 arguments now" )
142+ }
80143 }
81144}
0 commit comments