Created
October 18, 2023 07:33
-
-
Save shakir915/9bb453cbdca0a6c36abaef7779b4de08 to your computer and use it in GitHub Desktop.
google sheet exported CSV to Json for String/ Text/ Locale in flutter/ apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**** | |
google sheet exported CSV to Json for String/ Text/ Locale in flutter/ apps | |
run this kotlin script file | |
UNTICK : Interactive Mode | |
TICK: Use REPL | |
edit source path (check code) | |
run in Android studio scratch file | |
* */ | |
import java.io.File | |
import java.util.regex.Pattern | |
var source = | |
File("/Users/shakir/AndroidStudioProjects/Masjid_Flutter/assets/data/Masajid Al Kuwait New - app & CMS.csv") | |
fun replace(file: File) { | |
var text = file.readText() | |
val enList = mutableListOf<String>() | |
val arList = mutableListOf<String>() | |
val varList = mutableListOf<String>() | |
text.lines().forEach { | |
val splits = it.split(Pattern.compile(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)")) | |
var en = "" | |
var ar = "" | |
var varName = "" | |
if (splits.getOrNull(1)?.isNotBlank() == true) { | |
en = splits.getOrNull(1)!! | |
} else if (splits.getOrNull(0)?.isNotBlank() == true) { | |
en = splits.getOrNull(0)!! | |
} | |
ar = splits.getOrNull(2) ?: "" | |
en = en.trim() | |
ar = ar.trim() | |
varName = en.trim().toLowerCase() | |
.replace(" ", "_") | |
.replace(":", "_") | |
.replace("-", "_") | |
.replace("(", "_") | |
.replace(")", "_") | |
.replace("\"", "_") | |
.replace("____", "_") | |
.replace("___", "_") | |
.replace("--", "_") | |
if (varName.startsWith("_")) | |
varName = varName.replaceFirst("_", "") | |
if (varName.getOrNull(0).toString().toIntOrNull() != null) { | |
varName = "a_" + varName | |
} | |
en = en.replace("\"", "") | |
ar = ar.replace("\"", "") | |
if (varName.isNotBlank() && en.isNotBlank() && ar.isNotBlank()) { | |
fun check(): Boolean { | |
if (varList.contains(varName)) { | |
val index = varList.indexOf(varName) | |
val oldEn = enList[index] | |
val oldAr = arList[index] | |
if(oldEn.toLowerCase()!=en.toLowerCase()||oldAr.replace("'","").trim()!=ar.replace("'","").trim()){ | |
println(""" | |
Conflicts $varName $en $ar $oldEn $oldAr | |
""".trimIndent()) | |
varName=varName+"_1" | |
return check() | |
}else{ | |
return false | |
} | |
} | |
return true | |
} | |
if (check()){ | |
enList.add(en) | |
arList.add(ar) | |
varList.add(varName) | |
} | |
} | |
} | |
File(source.parent, "strings_en.json").writeText("{") | |
File(source.parent, "strings_ar.json").writeText("{") | |
varList.forEachIndexed { index, s -> | |
File(source.parent, "strings_en.json").appendText( | |
""" | |
"${varList[index]}" : "${enList[index]}" ${if (index!=varList.size-1) "," else ""} | |
""" | |
) | |
File(source.parent, "strings_ar.json").appendText( | |
""" | |
"${varList[index]}" : "${arList[index]}" ${if (index!=varList.size-1) "," else ""} | |
""" | |
) | |
} | |
File(source.parent, "strings_en.json").appendText("}") | |
File(source.parent, "strings_ar.json").appendText("}") | |
} | |
replace(source) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment