com.tairitsu.ignotus.translation.provider.resource.ResourceMessageProvider.kt Maven / Gradle / Ivy
package com.tairitsu.ignotus.translation.provider.resource
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
import com.tairitsu.ignotus.translation.provider.MessageProvider
import com.tairitsu.ignotus.translation.provider.resource.dsl.LanguageLineBuilder
import com.tairitsu.ignotus.translation.provider.resource.kts.KtsRunner
import com.tairitsu.ignotus.translation.provider.resource.model.ResourceMessageNode
import com.tairitsu.ignotus.translation.provider.resource.model.StringResourceMessageLine
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.core.io.Resource
import org.springframework.core.io.ResourceLoader
import org.springframework.core.io.support.ResourcePatternUtils
import org.springframework.stereotype.Component
import java.net.JarURLConnection
import java.net.URI
import java.util.*
import javax.annotation.PostConstruct
@Component
class ResourceMessageProvider(private val resourceLoader: ResourceLoader): MessageProvider {
private val log: Logger = LoggerFactory.getLogger(this::class.java)
private val data = ResourceMessageNode()
@PostConstruct
fun init() {
val t = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath*:/ignotus/lang/**")
t.forEach { loadTemplate(it) }
}
private fun loadTemplate(resource: Resource?) {
resource ?: return
if (!resource.isReadable) {
return
}
val fileName = resource.filename ?: return
val type = if (fileName.endsWith(".yaml", ignoreCase = true) || fileName.endsWith(".yml", ignoreCase = true)) {
"yaml"
} else if (fileName.endsWith(".json", ignoreCase = true) || fileName.endsWith(".jsonc", ignoreCase = true)) {
"json"
} else if (fileName.endsWith(".properties", ignoreCase = true)) {
"properties"
} else if (fileName.endsWith(".kts", ignoreCase = true)) {
"kts"
} else {
return
}
val info = parsePrefix(resource.uri) ?: return
val prefix = info.second + fileName.split(".").subList(0, fileName.split(".").size - 1).joinToString(".")
val locale = parseLocale(info.first) ?: return
// locale.language
// locale.country
// locale.variant
val localeStr = locale.toString().lowercase()
val localeNode = if (data[localeStr] == null) {
val t = ResourceMessageNode()
data[localeStr] = t
t
} else {
data[localeStr]!!
}
val node = localeNode.walkOrCreate(prefix)
when (type) {
"yaml" -> readResourceYaml(resource, node)
"json" -> readResourceJson(resource, node)
"properties" -> readResourceProperties(resource, node)
"kts" -> readResourceKts(resource, node)
else -> return
}
}
private fun readResourceYaml(resource: Resource, node: ResourceMessageNode) {
val mapper = YAMLMapper()
val map = try {
mapper.readValue(resource.inputStream, object: TypeReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy