mirror of
https://github.com/TermoraDev/termora.git
synced 2026-01-16 02:12:58 +08:00
chore!: migrate to version 2.x
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package app.termora
|
||||
|
||||
import org.apache.commons.lang3.LocaleUtils
|
||||
import org.apache.commons.text.StringSubstitutor
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.text.MessageFormat
|
||||
import java.util.*
|
||||
|
||||
object I18n {
|
||||
object I18n : AbstractI18n() {
|
||||
private val log = LoggerFactory.getLogger(I18n::class.java)
|
||||
private val bundle by lazy {
|
||||
private val myBundle by lazy {
|
||||
val bundle = ResourceBundle.getBundle("i18n/messages", Locale.getDefault())
|
||||
if (log.isInfoEnabled) {
|
||||
log.info("I18n: {}", bundle.baseBundleName ?: "null")
|
||||
@@ -16,7 +15,6 @@ object I18n {
|
||||
return@lazy bundle
|
||||
}
|
||||
|
||||
private val substitutor by lazy { StringSubstitutor { key -> getString(key) } }
|
||||
private val supportedLanguages = sortedMapOf(
|
||||
"en_US" to "English",
|
||||
"zh_CN" to "简体中文",
|
||||
@@ -39,24 +37,13 @@ object I18n {
|
||||
return supportedLanguages
|
||||
}
|
||||
|
||||
fun getString(key: String, vararg args: Any): String {
|
||||
val text = getString(key)
|
||||
if (args.isNotEmpty()) {
|
||||
return MessageFormat.format(text, *args)
|
||||
}
|
||||
return text
|
||||
|
||||
override fun getBundle(): ResourceBundle {
|
||||
return myBundle
|
||||
}
|
||||
|
||||
|
||||
fun getString(key: String): String {
|
||||
try {
|
||||
return substitutor.replace(bundle.getString(key))
|
||||
} catch (e: MissingResourceException) {
|
||||
if (log.isWarnEnabled) {
|
||||
log.warn(e.message, e)
|
||||
}
|
||||
return key
|
||||
}
|
||||
override fun getLogger(): Logger {
|
||||
return log
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user