From 9cf0eace8702cd9d9fcfa9f4e9d6dd9a0e4b90cc Mon Sep 17 00:00:00 2001 From: XiYang6666 <1782356858@qq.com> Date: Thu, 8 Aug 2024 17:45:01 +0800 Subject: [PATCH] :sparkles: preliminary implement i18n --- .../kotlin/xyz/xasmc/hashbook/HashBook.kt | 8 +++ .../hashbook/listener/BookshelfListener.kt | 30 ++++---- .../xyz/xasmc/hashbook/util/I18nUtil.kt | 41 +++++++++++ src/main/resources/lang/zh_cn.yml | 72 +++++++++++++++++++ 4 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 src/main/kotlin/xyz/xasmc/hashbook/util/I18nUtil.kt create mode 100644 src/main/resources/lang/zh_cn.yml diff --git a/src/main/kotlin/xyz/xasmc/hashbook/HashBook.kt b/src/main/kotlin/xyz/xasmc/hashbook/HashBook.kt index 4c24b9e..da7d103 100644 --- a/src/main/kotlin/xyz/xasmc/hashbook/HashBook.kt +++ b/src/main/kotlin/xyz/xasmc/hashbook/HashBook.kt @@ -9,7 +9,9 @@ import xyz.xasmc.hashbook.listener.BookshelfListener import xyz.xasmc.hashbook.listener.OpenBookListener import xyz.xasmc.hashbook.service.ItemDataServices import xyz.xasmc.hashbook.service.StorageServices +import xyz.xasmc.hashbook.util.I18nUtil import xyz.xasmc.hashbook.util.MarkUtil +import java.io.File class HashBook : JavaPlugin() { override fun onEnable() { @@ -26,6 +28,12 @@ class HashBook : JavaPlugin() { } fun load() { + val langDir = File(dataFolder, "lang") + if (!langDir.exists()) { + langDir.mkdir() + saveResource("lang/zh_cn.yml", true) + } + I18nUtil.loadTranslate(File(dataFolder, "lang/zh_cn.yml")) HashBook.config = ConfigLoader.loadConfig() ItemDataServices.load(HashBook.config) StorageServices.load(HashBook.config) diff --git a/src/main/kotlin/xyz/xasmc/hashbook/listener/BookshelfListener.kt b/src/main/kotlin/xyz/xasmc/hashbook/listener/BookshelfListener.kt index 64dfab1..f22d28c 100644 --- a/src/main/kotlin/xyz/xasmc/hashbook/listener/BookshelfListener.kt +++ b/src/main/kotlin/xyz/xasmc/hashbook/listener/BookshelfListener.kt @@ -7,7 +7,8 @@ import org.bukkit.event.Listener import org.bukkit.event.player.PlayerMoveEvent import org.bukkit.event.player.PlayerQuitEvent import org.bukkit.inventory.meta.BookMeta -import org.bukkit.inventory.meta.BookMeta.Generation.* +import org.bukkit.inventory.meta.EnchantmentStorageMeta +import xyz.xasmc.hashbook.util.I18nUtil import xyz.xasmc.hashbook.util.MarkUtil @@ -44,18 +45,23 @@ class BookshelfListener : Listener { } val normalizedEyeDirection = player.eyeLocation.direction.clone().normalize() val markLocation = hitPosition.clone().subtract(normalizedEyeDirection.multiply(0.1)) - val nameSb = StringBuilder(item.type.name) - if (item.type == Material.WRITTEN_BOOK) { - val meta = item.itemMeta as BookMeta - nameSb.append("\n${meta.title}\n${meta.author} 著") - val generation = when (meta.generation) { - ORIGINAL -> "原稿" - COPY_OF_ORIGINAL -> "原稿的副本" - COPY_OF_COPY -> "副本的副本" - TATTERED -> "破烂不堪" - null -> "原稿" + val nameSb = StringBuilder(I18nUtil.translate(item.type)) + when (item.type) { + Material.WRITTEN_BOOK -> { + val meta = item.itemMeta as BookMeta + nameSb.append("\n").append(meta.title) + nameSb.append("\n").append(I18nUtil.getTranslate("book.byAuthor").format(meta.author)) + nameSb.append("\n").append(I18nUtil.translate(meta.generation)) } - nameSb.append("\n$generation") + + Material.ENCHANTED_BOOK -> { + val meta = item.itemMeta as EnchantmentStorageMeta + meta.storedEnchants.forEach { + nameSb.append("\n").append(I18nUtil.translate(it.key, it.value)) + } + } + + else -> {} } MarkUtil.updateMark(player, markLocation.toLocation(world), nameSb.toString()) } diff --git a/src/main/kotlin/xyz/xasmc/hashbook/util/I18nUtil.kt b/src/main/kotlin/xyz/xasmc/hashbook/util/I18nUtil.kt new file mode 100644 index 0000000..90327e5 --- /dev/null +++ b/src/main/kotlin/xyz/xasmc/hashbook/util/I18nUtil.kt @@ -0,0 +1,41 @@ +package xyz.xasmc.hashbook.util + +import org.bukkit.Material +import org.bukkit.configuration.file.YamlConfiguration +import org.bukkit.enchantments.Enchantment +import org.bukkit.inventory.meta.BookMeta +import org.bukkit.inventory.meta.BookMeta.Generation.* +import java.io.File + +object I18nUtil { + private lateinit var config: YamlConfiguration + + fun loadTranslate(path: File) { + config = YamlConfiguration.loadConfiguration(path) + } + + fun translate(generation: BookMeta.Generation?): String { + return when (generation) { + ORIGINAL -> getTranslate("book.generation.0") + COPY_OF_ORIGINAL -> getTranslate("book.generation.1") + COPY_OF_COPY -> getTranslate("book.generation.2") + TATTERED -> getTranslate("book.generation.3") + null -> getTranslate("book.generation.0") + } + } + + fun translate(type: Material): String { + val key = type.itemTranslationKey + return if (key != null) getTranslate(key) else type.name.lowercase() + } + + fun translate(enchantment: Enchantment, level: Int): String { + val name = getTranslate(enchantment.translationKey()) + val levelStr = if (level <= 10) getTranslate("enchantment.level.$level") else level.toString() + return "$name $levelStr" + } + + fun getTranslate(key: String): String { + return config.getString(key) ?: key + } +} \ No newline at end of file diff --git a/src/main/resources/lang/zh_cn.yml b/src/main/resources/lang/zh_cn.yml new file mode 100644 index 0000000..bda0200 --- /dev/null +++ b/src/main/resources/lang/zh_cn.yml @@ -0,0 +1,72 @@ +item: + minecraft: + book: "书" + enchanted_book: "附魔书" + knowledge_book: "知识之书" + written_book: "成书" + writable_book: "书与笔" + +book: + byAuthor: "%s 著" + generation: + "0": "原稿" + "1": "原稿的副本" + "2": "副本的副本" + "3": "破烂不堪" + +enchantment: + level: + "1": "I" + "2": "II" + "3": "III" + "4": "IV" + "5": "V" + "6": "VI" + "7": "VII" + "8": "VIII" + "9": "IX" + "10": "X" + minecraft: + aqua_affinity: "水下速掘" + bane_of_arthropods: "节肢杀手" + binding_curse: "绑定诅咒" + blast_protection: "爆炸保护" + breach: "破甲" + channeling: "引雷" + density: "致密" + depth_strider: "深海探索者" + efficiency: "效率" + feather_falling: "摔落缓冲" + fire_aspect: "火焰附加" + fire_protection: "火焰保护" + flame: "火矢" + fortune: "时运" + frost_walker: "冰霜行者" + impaling: "穿刺" + infinity: "无限" + knockback: "击退" + looting: "抢夺" + loyalty: "忠诚" + luck_of_the_sea: "海之眷顾" + lure: "饵钓" + mending: "经验修补" + multishot: "多重射击" + piercing: "穿透" + power: "力量" + projectile_protection: "弹射物保护" + protection: "保护" + punch: "冲击" + quick_charge: "快速装填" + respiration: "水下呼吸" + riptide: "激流" + sharpness: "锋利" + silk_touch: "精准采集" + smite: "亡灵杀手" + soul_speed: "灵魂疾行" + sweeping: "横扫之刃" + sweeping_edge: "横扫之刃" + swift_sneak: "迅捷潜行" + thorns: "荆棘" + unbreaking: "耐久" + vanishing_curse: "消失诅咒" + wind_burst: "风爆" \ No newline at end of file