From 8b4167d847a668ddb748ef88b2bc192c1b48c02b Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 13 May 2025 15:45:43 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A8=80=E8=AA=9E=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=81=8C=E8=AA=AD=E3=81=BF=E8=BE=BC=E3=82=81?= =?UTF-8?q?=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MessageSourceが正常に構成されず、言語ファイルが読み込めずに表示される問題を修正 --- .../core/config/MessageSourceConfig.kt | 16 +---- .../MergedPropertiesMessageSource.kt | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MergedPropertiesMessageSource.kt diff --git a/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/MessageSourceConfig.kt b/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/MessageSourceConfig.kt index 73e2311c..efe82579 100644 --- a/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/MessageSourceConfig.kt +++ b/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/MessageSourceConfig.kt @@ -16,27 +16,17 @@ package dev.usbharu.hideout.core.config -import org.springframework.boot.autoconfigure.context.MessageSourceProperties -import org.springframework.boot.context.properties.ConfigurationProperties +import dev.usbharu.hideout.core.infrastructure.springframework.MergedPropertiesMessageSource import org.springframework.context.MessageSource import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Profile -import org.springframework.context.support.ReloadableResourceBundleMessageSource @Configuration @Profile("dev") class MessageSourceConfig { @Bean - fun messageSource(messageSourceProperties: MessageSourceProperties): MessageSource { - val reloadableResourceBundleMessageSource = ReloadableResourceBundleMessageSource() - reloadableResourceBundleMessageSource.setBasename("classpath:" + messageSourceProperties.basename) - reloadableResourceBundleMessageSource.setCacheSeconds(0) - return reloadableResourceBundleMessageSource + fun messageSource(): MessageSource { + return MergedPropertiesMessageSource() } - - @Bean - @Profile("dev") - @ConfigurationProperties(prefix = "spring.messages") - fun messageSourceProperties(): MessageSourceProperties = MessageSourceProperties() } diff --git a/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MergedPropertiesMessageSource.kt b/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MergedPropertiesMessageSource.kt new file mode 100644 index 00000000..d23a328d --- /dev/null +++ b/hideout/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/MergedPropertiesMessageSource.kt @@ -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 = 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) + } +} \ No newline at end of file