mirror of https://github.com/usbharu/Hideout.git
fix: 言語ファイルが読み込めない問題を修正
MessageSourceが正常に構成されず、言語ファイルが読み込めずに表示される問題を修正
This commit is contained in:
parent
98385c6b82
commit
8b4167d847
|
@ -16,27 +16,17 @@
|
||||||
|
|
||||||
package dev.usbharu.hideout.core.config
|
package dev.usbharu.hideout.core.config
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.context.MessageSourceProperties
|
import dev.usbharu.hideout.core.infrastructure.springframework.MergedPropertiesMessageSource
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
|
||||||
import org.springframework.context.MessageSource
|
import org.springframework.context.MessageSource
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
import org.springframework.context.annotation.Profile
|
import org.springframework.context.annotation.Profile
|
||||||
import org.springframework.context.support.ReloadableResourceBundleMessageSource
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Profile("dev")
|
@Profile("dev")
|
||||||
class MessageSourceConfig {
|
class MessageSourceConfig {
|
||||||
@Bean
|
@Bean
|
||||||
fun messageSource(messageSourceProperties: MessageSourceProperties): MessageSource {
|
fun messageSource(): MessageSource {
|
||||||
val reloadableResourceBundleMessageSource = ReloadableResourceBundleMessageSource()
|
return MergedPropertiesMessageSource()
|
||||||
reloadableResourceBundleMessageSource.setBasename("classpath:" + messageSourceProperties.basename)
|
|
||||||
reloadableResourceBundleMessageSource.setCacheSeconds(0)
|
|
||||||
return reloadableResourceBundleMessageSource
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Profile("dev")
|
|
||||||
@ConfigurationProperties(prefix = "spring.messages")
|
|
||||||
fun messageSourceProperties(): MessageSourceProperties = MessageSourceProperties()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
package dev.usbharu.hideout.core.infrastructure.springframework
|
||||||
|
|
||||||
|
import org.springframework.context.support.AbstractMessageSource
|
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
import java.nio.charset.StandardCharsets
|
||||||
|
import java.text.MessageFormat
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
|
class MergedPropertiesMessageSource : AbstractMessageSource() {
|
||||||
|
|
||||||
|
private val messages: MutableMap<Locale, Properties> = ConcurrentHashMap()
|
||||||
|
|
||||||
|
init {
|
||||||
|
loadAllLocaleProperties("classpath*:/messages/hideout-web-messages*.properties")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadAllLocaleProperties(locationPattern: String) {
|
||||||
|
val resolver = PathMatchingResourcePatternResolver()
|
||||||
|
val resources = resolver.getResources(locationPattern)
|
||||||
|
|
||||||
|
for (resource in resources) {
|
||||||
|
val filename = resource.filename ?: continue
|
||||||
|
|
||||||
|
val localeSuffix = filename
|
||||||
|
.removePrefix("hideout-web-messages")
|
||||||
|
.removeSuffix(".properties")
|
||||||
|
.takeIf { it.isNotBlank() }
|
||||||
|
?: "default"
|
||||||
|
|
||||||
|
val locale = if (localeSuffix == "default") {
|
||||||
|
Locale.ROOT
|
||||||
|
} else {
|
||||||
|
Locale.forLanguageTag(localeSuffix.replace('_', '-'))
|
||||||
|
}
|
||||||
|
|
||||||
|
val props = messages.getOrPut(locale) { Properties() }
|
||||||
|
|
||||||
|
resource.inputStream.use { stream ->
|
||||||
|
InputStreamReader(stream, StandardCharsets.UTF_8).use { reader ->
|
||||||
|
val newProps = Properties()
|
||||||
|
newProps.load(reader)
|
||||||
|
props.putAll(newProps) // 上書きあり
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun resolveCode(code: String, locale: Locale): MessageFormat? {
|
||||||
|
val props = messages[locale] ?: messages[Locale.ROOT] ?: return null
|
||||||
|
val msg = props.getProperty(code) ?: return null
|
||||||
|
return MessageFormat(msg, locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun resolveCodeWithoutArguments(code: String, locale: Locale): String? {
|
||||||
|
val props = messages[locale] ?: messages[Locale.ROOT] ?: return null
|
||||||
|
return props.getProperty(code)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue