forked from fbiego/DT78-App-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslations.kt
128 lines (115 loc) · 4.35 KB
/
translations.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.File
const val SEPARATOR = "\t"
const val license = "<!--\n" +
" ~\n" +
" ~ MIT License\n" +
" ~\n" +
" ~ Copyright (c) 2021 Felix Biego\n" +
" ~\n" +
" ~ Permission is hereby granted, free of charge, to any person obtaining a copy\n" +
" ~ of this software and associated documentation files (the \"Software\"), to deal\n" +
" ~ in the Software without restriction, including without limitation the rights\n" +
" ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n" +
" ~ copies of the Software, and to permit persons to whom the Software is\n" +
" ~ furnished to do so, subject to the following conditions:\n" +
" ~\n" +
" ~ The above copyright notice and this permission notice shall be included in all\n" +
" ~ copies or substantial portions of the Software.\n" +
" ~\n" +
" ~ THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n" +
" ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" +
" ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n" +
" ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n" +
" ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" +
" ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" +
" ~ SOFTWARE.\n" +
" -->"
fun main(args: Array<String>) {
if (args.size > 0){
val data = File(args[0]).readLines()
if (data.size > 0){
generate(data)
println("-----Done-------")
} else {
println("Could not read from ${args[0]}")
}
} else {
println("Specify the file name")
}
}
fun generate(strings: List<String>){
val lang = strings[0].split(SEPARATOR).count() - 1
val filled = arrayOf("bat_charging", "bat_unplugged", "bat_phone", "quiet_active")
val alarm = arrayOf("alarm_once", "alarm_everyday", "alarm_mon_fri", "custom_alarm")
var alarms = arrayListOf("", "", "", "")
println("Languages: $lang")
val dir = File("app/src/main/res")
if (!dir.exists())
dir.mkdirs()
println("Created output folder")
for (x in 1..lang){
var output = "$license\n\n"
output += "<resources>\n"
var name = ""
for (s in strings){
var word = s.split(SEPARATOR)
val text = word[x].replace("\'", "\\'").replace("&", "&").replace("\"", "")
if (word[0] == "id"){
name = if (word[x] == "en"){
"values"
} else {
"values-${word[x]}"
}
} else {
output += "\t<string name=\"${word[0]}\">${if (filled.contains(word[0])) padded(text) else text}</string>\n"
if (alarm.contains(word[0])){
val i = alarm.indexOf(word[0])
alarms[i] = text
}
}
}
output += "\t<string-array name=\"alarm_options\">\n\t\t<item>${alarms[0]}</item>\n\t\t<item>${alarms[1]}</item>\n\t\t<item>${alarms[2]}</item>\n\t\t<item>${alarms[3]}</item>\n\t</string-array>"
output += "\n\t<string-array name=\"sedentaryInterval\">\n\t\t<item>45 mins</item>\n\t\t<item>1 hour</item>\n\t</string-array>\n"
output += "</resources>"
println("Generated: ${name}/strings.xml")
if (!dir.exists())
dir.mkdirs()
val res = File(dir, name)
if (!res.exists())
res.mkdirs()
val file = File(res, "strings.xml")
file.createNewFile()
file.writeText(output)
}
}
fun padded(w: String): String{
val input = w.replace("[", "").replace("]", "")
if (input.length > 25){
return input.substring(0, 25)
} else if (input.length == 25){
return input
} else {
val rem = 25 - input.length
var start = if (rem/2 != 0){
"_".repeat(rem/2)
} else {
""
}
if (rem % 2 != 0){
start += "_"
}
var end = if (rem/2 != 0){
"_".repeat(rem/2)
} else {
""
}
var pad = start + input + end
if (pad[0] == '_'){
pad = "[" + pad.substring(1,25)
}
if (pad[24] == '_'){
pad = pad.substring(0,24) + "]"
}
return pad
}
}