Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Jun 1, 2024
1 parent 43c4095 commit c02279f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/main/kotlin/Translate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.github.brenoepics.utils.FileManager.Companion.readIni
import org.slf4j.LoggerFactory
import utils.TranslateUtils


/**
* Main function to start the translation process.
* Reads the ini file and starts the translation process.
Expand All @@ -20,6 +19,5 @@ fun main() {
val root = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger
root.level = config.loggerLevel
TranslateUtils.translateJSON(config, HashMap())

}

5 changes: 1 addition & 4 deletions src/main/kotlin/translation/Translator.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.brenoepics.translation

import java.util.concurrent.CompletableFuture

interface Translator {

fun getCache(): HashMap<String, HashMap<String, String>>
fun getLanguageCache(langCode: String): java.util.HashMap<String, String>
fun addToCache(lang: String, translations: HashMap<String, String>)
fun translate(
texts: Map<String, String>,
Expand Down
48 changes: 29 additions & 19 deletions src/main/kotlin/utils/TranslateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.github.brenoepics.utils.FileManager
import io.github.brenoepics.utils.FileManager.Companion.getJsonTexts
import io.github.brenoepics.utils.FileManager.Companion.readJsonFile
import io.github.brenoepics.utils.FileManager.Companion.writeTranslation
import org.json.JSONObject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.nio.file.Path
Expand All @@ -27,36 +26,47 @@ class TranslateUtils {
toTranslate: HashMap<String, String>,
) {
val dirFiles = FileManager.readPath(config.sourcePath, config.recursiveInput)
val filesData: HashMap<Path, JSONObject> = HashMap()
val filesData: HashMap<Path, Any?> = HashMap()
log.info("Reading files...")
dirFiles.forEach {
readFiles(dirFiles, toTranslate, filesData)
config.api?.translate(toTranslate, config.from, config.to)
config.api?.let { updateFileData(config, dirFiles, filesData, it) }
log.info("Translation finished!")
config.api?.dispose()
}

private fun readFiles(
dirFiles: List<Path>,
toTranslate: HashMap<String, String>,
filesData: HashMap<Path, Any?>
) {
for (it in dirFiles) {
val file = readJsonFile(it)
toTranslate.putAll(getJsonTexts(file))
filesData[it] = file
log.debug("Read file: {}", it)
}
config.api?.translate(toTranslate, config.from, config.to)
config.api?.let { updateFileData(config, dirFiles, filesData, it) }
log.info("Translation finished!")
config.api?.dispose()
}

private fun updateFileData(
config: TranslationConfig, dirFiles: List<Path>, filesData: HashMap<Path, JSONObject>, api: Translator
config: TranslationConfig, dirFiles: List<Path>, filesData: HashMap<Path, Any?>, api: Translator
) {
log.info("Generating translated files...")
dirFiles.forEach {
val jsonObject = filesData[it]
if (jsonObject != null) {
for ((lang, translations) in api.getCache()) translations.writeTranslation(
config.outputTemplate,
config.sourcePath,
jsonObject,
config.targetPath,
it,
lang,
config.indentationLevel
)
val json = filesData[it]
if (json != null) {
for (lang in config.to) {
val translations = api.getLanguageCache(lang)
translations.writeTranslation(
config.outputTemplate,
config.sourcePath,
json,
config.targetPath,
it,
lang,
config.indentationLevel
)
}
}
}
}
Expand Down

0 comments on commit c02279f

Please sign in to comment.