-
Notifications
You must be signed in to change notification settings - Fork 121
/
ApplePluralsGenerator.kt
100 lines (83 loc) · 3.11 KB
/
ApplePluralsGenerator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.icerock.gradle.generator.apple
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import dev.icerock.gradle.generator.PluralMap
import dev.icerock.gradle.generator.PluralsGenerator
import dev.icerock.gradle.generator.KeyType
import dev.icerock.gradle.generator.ObjectBodyExtendable
import org.gradle.api.file.FileTree
import java.io.File
class ApplePluralsGenerator(
pluralsFileTree: FileTree,
strictLineBreaks: Boolean,
private val baseLocalizationRegion: String
) : PluralsGenerator(pluralsFileTree, strictLineBreaks),
ObjectBodyExtendable by AppleGeneratorHelper() {
override fun getClassModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)
override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)
override fun getPropertyInitializer(key: String) = CodeBlock.of(
"PluralsResource(resourceId = %S, bundle = ${AppleMRGenerator.BUNDLE_PROPERTY_NAME})",
key
)
private fun writeStringsFile(file: File, strings: Map<KeyType, PluralMap>) {
val head = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
"""
val content = strings.map { (key, pluralMap) ->
val start = """<key>$key</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@pluraled@</string>
<key>pluraled</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
"""
val items = pluralMap.map { (quantity, value) ->
val processedValue = value.escapeFormatArguments()
""" <key>$quantity</key>
<string>$processedValue</string>"""
}.joinToString(separator = "\n")
val end = """
</dict>
</dict>"""
start + items + end
}.joinToString("\n")
val footer = """
</dict>
</plist>"""
file.writeText(head)
file.appendText(content)
file.appendText(footer)
}
override fun generateResources(
resourcesGenerationDir: File,
language: String?,
strings: Map<KeyType, PluralMap>
) {
val resDirName = when (language) {
null -> "Base.lproj"
else -> "$language.lproj"
}
val resDir = File(resourcesGenerationDir, resDirName)
val localizableFile = File(resDir, "Localizable.stringsdict")
resDir.mkdirs()
writeStringsFile(localizableFile, strings)
if (language == null) {
val regionDir = File(resourcesGenerationDir, "$baseLocalizationRegion.lproj")
regionDir.mkdirs()
val regionFile = File(regionDir, "Localizable.stringsdict")
writeStringsFile(regionFile, strings)
}
}
private fun String.escapeFormatArguments(): String {
return this.replace(Regex("%(((?:\\.|\\d|\\$)*)[abcdefs])"), "%%$1")
}
}