@@ -8,6 +8,8 @@ import java.io.InputStream
8
8
import java.lang.IllegalArgumentException
9
9
import javax.xml.parsers.DocumentBuilderFactory
10
10
11
+ private val regexStringArgs = Regex (" %([1-9]+)\\ \$ s" )
12
+ private val regexNumArgs = Regex (" %([1-9]+)\\ \$ d" )
11
13
data class AndroidStrings (
12
14
val locale : String ,
13
15
val strings : List <StringNode >,
@@ -127,24 +129,23 @@ fun Map<String, String>.asArbText() : String {
127
129
/* *
128
130
* very unoptimized converter
129
131
*/
130
- private fun String.toArb () : String {
132
+ private fun String.toArb (): String {
131
133
var input: String = this
132
- val regexArgs = Regex (" %([1-9]+)s" )
133
- val regexNums = Regex (" %([1-9]+)d" )
134
- replacementMap.forEach { key, value ->
135
- val findArgs: Boolean = ! regexArgs.findAll(key).toList().isNullOrEmpty()
136
- val findNums: Boolean = ! regexNums.findAll(key).toList().isNullOrEmpty()
137
- when {
138
- findArgs -> {
139
- input = input.replace(regexArgs, value)
140
- }
141
- findNums -> {
142
- input = input.replace(regexNums, value)
143
- }
144
- else -> {
145
- input = input.replace(key, value)
146
- }
147
- }
134
+
135
+ val args = regexStringArgs.findAll(input).toList()
136
+ args.forEach {
137
+ val argPosition = it.groupValues.last()
138
+ input = input.replace(it.value, " \$ {arg$argPosition }" )
139
+ }
140
+
141
+ val nums = regexNumArgs.findAll(input).toList()
142
+ nums.forEach {
143
+ val argPosition = it.groupValues.last()
144
+ input = input.replace(it.value, " \$ {num$argPosition }" )
145
+ }
146
+
147
+ replacementMap.forEach { (key, value) ->
148
+ input = input.replace(key, value)
148
149
}
149
150
return input
150
151
}
0 commit comments