Skip to content

Commit 9cf0eac

Browse files
committed
✨ preliminary implement i18n
1 parent 8ae96cb commit 9cf0eac

File tree

4 files changed

+139
-12
lines changed

4 files changed

+139
-12
lines changed

src/main/kotlin/xyz/xasmc/hashbook/HashBook.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import xyz.xasmc.hashbook.listener.BookshelfListener
99
import xyz.xasmc.hashbook.listener.OpenBookListener
1010
import xyz.xasmc.hashbook.service.ItemDataServices
1111
import xyz.xasmc.hashbook.service.StorageServices
12+
import xyz.xasmc.hashbook.util.I18nUtil
1213
import xyz.xasmc.hashbook.util.MarkUtil
14+
import java.io.File
1315

1416
class HashBook : JavaPlugin() {
1517
override fun onEnable() {
@@ -26,6 +28,12 @@ class HashBook : JavaPlugin() {
2628
}
2729

2830
fun load() {
31+
val langDir = File(dataFolder, "lang")
32+
if (!langDir.exists()) {
33+
langDir.mkdir()
34+
saveResource("lang/zh_cn.yml", true)
35+
}
36+
I18nUtil.loadTranslate(File(dataFolder, "lang/zh_cn.yml"))
2937
HashBook.config = ConfigLoader.loadConfig()
3038
ItemDataServices.load(HashBook.config)
3139
StorageServices.load(HashBook.config)

src/main/kotlin/xyz/xasmc/hashbook/listener/BookshelfListener.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import org.bukkit.event.Listener
77
import org.bukkit.event.player.PlayerMoveEvent
88
import org.bukkit.event.player.PlayerQuitEvent
99
import org.bukkit.inventory.meta.BookMeta
10-
import org.bukkit.inventory.meta.BookMeta.Generation.*
10+
import org.bukkit.inventory.meta.EnchantmentStorageMeta
11+
import xyz.xasmc.hashbook.util.I18nUtil
1112
import xyz.xasmc.hashbook.util.MarkUtil
1213

1314

@@ -44,18 +45,23 @@ class BookshelfListener : Listener {
4445
}
4546
val normalizedEyeDirection = player.eyeLocation.direction.clone().normalize()
4647
val markLocation = hitPosition.clone().subtract(normalizedEyeDirection.multiply(0.1))
47-
val nameSb = StringBuilder(item.type.name)
48-
if (item.type == Material.WRITTEN_BOOK) {
49-
val meta = item.itemMeta as BookMeta
50-
nameSb.append("\n<aqua>${meta.title}\n<gray>${meta.author}")
51-
val generation = when (meta.generation) {
52-
ORIGINAL -> "原稿"
53-
COPY_OF_ORIGINAL -> "原稿的副本"
54-
COPY_OF_COPY -> "副本的副本"
55-
TATTERED -> "破烂不堪"
56-
null -> "原稿"
48+
val nameSb = StringBuilder(I18nUtil.translate(item.type))
49+
when (item.type) {
50+
Material.WRITTEN_BOOK -> {
51+
val meta = item.itemMeta as BookMeta
52+
nameSb.append("\n").append(meta.title)
53+
nameSb.append("\n<gray>").append(I18nUtil.getTranslate("book.byAuthor").format(meta.author))
54+
nameSb.append("\n<gray>").append(I18nUtil.translate(meta.generation))
5755
}
58-
nameSb.append("\n<gray>$generation")
56+
57+
Material.ENCHANTED_BOOK -> {
58+
val meta = item.itemMeta as EnchantmentStorageMeta
59+
meta.storedEnchants.forEach {
60+
nameSb.append("\n<gray>").append(I18nUtil.translate(it.key, it.value))
61+
}
62+
}
63+
64+
else -> {}
5965
}
6066
MarkUtil.updateMark(player, markLocation.toLocation(world), nameSb.toString())
6167
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package xyz.xasmc.hashbook.util
2+
3+
import org.bukkit.Material
4+
import org.bukkit.configuration.file.YamlConfiguration
5+
import org.bukkit.enchantments.Enchantment
6+
import org.bukkit.inventory.meta.BookMeta
7+
import org.bukkit.inventory.meta.BookMeta.Generation.*
8+
import java.io.File
9+
10+
object I18nUtil {
11+
private lateinit var config: YamlConfiguration
12+
13+
fun loadTranslate(path: File) {
14+
config = YamlConfiguration.loadConfiguration(path)
15+
}
16+
17+
fun translate(generation: BookMeta.Generation?): String {
18+
return when (generation) {
19+
ORIGINAL -> getTranslate("book.generation.0")
20+
COPY_OF_ORIGINAL -> getTranslate("book.generation.1")
21+
COPY_OF_COPY -> getTranslate("book.generation.2")
22+
TATTERED -> getTranslate("book.generation.3")
23+
null -> getTranslate("book.generation.0")
24+
}
25+
}
26+
27+
fun translate(type: Material): String {
28+
val key = type.itemTranslationKey
29+
return if (key != null) getTranslate(key) else type.name.lowercase()
30+
}
31+
32+
fun translate(enchantment: Enchantment, level: Int): String {
33+
val name = getTranslate(enchantment.translationKey())
34+
val levelStr = if (level <= 10) getTranslate("enchantment.level.$level") else level.toString()
35+
return "$name $levelStr"
36+
}
37+
38+
fun getTranslate(key: String): String {
39+
return config.getString(key) ?: key
40+
}
41+
}

src/main/resources/lang/zh_cn.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
item:
2+
minecraft:
3+
book: ""
4+
enchanted_book: "<yellow>附魔书"
5+
knowledge_book: "<light_purple>知识之书"
6+
written_book: "成书"
7+
writable_book: "书与笔"
8+
9+
book:
10+
byAuthor: "%s 著"
11+
generation:
12+
"0": "原稿"
13+
"1": "原稿的副本"
14+
"2": "副本的副本"
15+
"3": "破烂不堪"
16+
17+
enchantment:
18+
level:
19+
"1": "I"
20+
"2": "II"
21+
"3": "III"
22+
"4": "IV"
23+
"5": "V"
24+
"6": "VI"
25+
"7": "VII"
26+
"8": "VIII"
27+
"9": "IX"
28+
"10": "X"
29+
minecraft:
30+
aqua_affinity: "水下速掘"
31+
bane_of_arthropods: "节肢杀手"
32+
binding_curse: "绑定诅咒"
33+
blast_protection: "爆炸保护"
34+
breach: "破甲"
35+
channeling: "引雷"
36+
density: "致密"
37+
depth_strider: "深海探索者"
38+
efficiency: "效率"
39+
feather_falling: "摔落缓冲"
40+
fire_aspect: "火焰附加"
41+
fire_protection: "火焰保护"
42+
flame: "火矢"
43+
fortune: "时运"
44+
frost_walker: "冰霜行者"
45+
impaling: "穿刺"
46+
infinity: "无限"
47+
knockback: "击退"
48+
looting: "抢夺"
49+
loyalty: "忠诚"
50+
luck_of_the_sea: "海之眷顾"
51+
lure: "饵钓"
52+
mending: "经验修补"
53+
multishot: "多重射击"
54+
piercing: "穿透"
55+
power: "力量"
56+
projectile_protection: "弹射物保护"
57+
protection: "保护"
58+
punch: "冲击"
59+
quick_charge: "快速装填"
60+
respiration: "水下呼吸"
61+
riptide: "激流"
62+
sharpness: "锋利"
63+
silk_touch: "精准采集"
64+
smite: "亡灵杀手"
65+
soul_speed: "灵魂疾行"
66+
sweeping: "横扫之刃"
67+
sweeping_edge: "横扫之刃"
68+
swift_sneak: "迅捷潜行"
69+
thorns: "荆棘"
70+
unbreaking: "耐久"
71+
vanishing_curse: "消失诅咒"
72+
wind_burst: "风爆"

0 commit comments

Comments
 (0)