Skip to content

Commit c0ccc82

Browse files
committed
refactor: replace hardcoded text with translatable components for better localization support
1 parent f6acf95 commit c0ccc82

26 files changed

Lines changed: 303 additions & 190 deletions

File tree

common/src/main/java/net/azisaba/interchatmod/common/AbstractWebSocketChatClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void onOpen(ServerHandshake handshakedata) {
5151
openAt = System.currentTimeMillis();
5252
auth(getApiKey());
5353
trySwitch();
54-
sendMessage("ギルドチャットに接続しました。");
54+
sendMessage(Component.translatable("generic.connected"));
5555
}
5656

5757
@Override
@@ -69,7 +69,7 @@ public void onMessage(String message) {
6969
sendMessage(GsonComponentSerializer.gson().deserialize(obj.get("json").getAsString()));
7070
}
7171
if (type.equals("error_message")) {
72-
sendMessage(Component.text("サーバーからエラーが返されました: " + obj.get("message").getAsString(), NamedTextColor.RED));
72+
sendMessage(Component.translatable("generic.error_from_server", Component.text(obj.get("message").getAsString())).color(NamedTextColor.RED));
7373
}
7474
if (type.equals("guild")) {
7575
selectedGuild = obj.get("guildId").getAsLong();
@@ -79,18 +79,18 @@ public void onMessage(String message) {
7979
@Override
8080
public void onClose(int code, String reason, boolean remote) {
8181
if (System.currentTimeMillis() - openAt < 1000) {
82-
sendMessage("ギルドチャットから切断されました。APIキーを確認して、/reconnectinterchat [apikey]を実行してください。");
82+
sendMessage(Component.translatable("generic.disconnected_check_api_key"));
8383
return;
8484
}
8585
if (remote) {
86-
sendMessage("ギルドチャットから切断されました。5秒後に再接続を試みます。");
86+
sendMessage(Component.translatable("generic.disconnected"));
8787
scheduleReconnect();
8888
}
8989
}
9090

9191
@Override
9292
public void onError(Exception ex) {
93-
sendMessage("WebSocket接続でエラーが発生しました: " + ex.getMessage());
93+
sendMessage(Component.translatable("generic.error_in_connection", Component.text(ex.getMessage())).color(NamedTextColor.RED));
9494
ex.printStackTrace();
9595
}
9696

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
{
2+
"text.config.interchat-config.title": "InterChat",
3+
"text.config.interchat-config.category.general": "General",
24
"text.config.interchat-config.option.apiHost": "API Host",
35
"text.config.interchat-config.option.apiHost.tooltip": "the api host to override the default one. Leave empty unless you know what you're doing!",
46
"text.config.interchat-config.option.apiKey": "API Key",
57
"text.config.interchat-config.option.apiKey.tooltip": "the api key that you can obtain via /apikey",
68
"text.config.interchat-config.option.hideEverything": "Hide everything",
79
"text.config.interchat-config.option.hideEverything.tooltip": "Hide every in-game messages from InterChatMod",
8-
"text.config.interchat-config.option.chatWithoutCommand": "Chat without command"
10+
"text.config.interchat-config.option.chatWithoutCommand": "Chat without command",
11+
"generic.not_connected": "Not connected to InterChat.",
12+
"generic.invalid_selected_guild": "The selected guild is invalid. Select one with /cgs <guild>.",
13+
"generic.guild_info_header": "--- Guild %s Info ---",
14+
"generic.guild_members": "Members: %s/%s",
15+
"generic.guild_presence_playing": "Playing: %s",
16+
"generic.guild_role.owner": "Owner: %s",
17+
"generic.guild_role.moderator": "Moderator: %s",
18+
"generic.guild_role.member": "Member: %s",
19+
"generic.guild_open": "Public: %s",
20+
"generic.guild_chat_format": "Chat format: ",
21+
"generic.copy_on_click": "Click to copy",
22+
"generic.guild_not_found": "Guild not found: %s",
23+
"generic.guild_focus_set": "Now chatting in %s. Use /cg <message> to chat there.",
24+
"generic.boolean.true": "true",
25+
"generic.boolean.false": "false",
26+
"generic.connected": "Connected to InterChat.",
27+
"generic.disconnected": "Disconnected from InterChat. Will try to reconnect in 5 seconds.",
28+
"generic.disconnected_check_api_key": "Disconnected from InterChat. Please check your API key and do /reconnectinterchat [apikey] to reconnect.",
29+
"generic.error_from_server": "Error was returned from the server: %s",
30+
"generic.error_in_connection": "Error in WebSocket connection: %s"
931
}
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
{
2+
"text.config.interchat-config.title": "InterChat",
3+
"text.config.interchat-config.category.general": "一般",
24
"text.config.interchat-config.option.apiHost": "APIホスト",
35
"text.config.interchat-config.option.apiHost.tooltip": "APIホストを上書きします。カスタムホストを使用したい場合のみ編集してください。",
46
"text.config.interchat-config.option.apiKey": "APIキー",
57
"text.config.interchat-config.option.apiKey.tooltip": "アジ鯖内で/apikeyで入手できるAPIキー",
68
"text.config.interchat-config.option.hideEverything": "すべて非表示にする",
79
"text.config.interchat-config.option.hideEverything.tooltip": "InterChatModからのすべてのゲーム内メッセージを非表示にします",
8-
"text.config.interchat-config.option.chatWithoutCommand": "コマンドなしでチャット"
10+
"text.config.interchat-config.option.chatWithoutCommand": "コマンドなしでチャット",
11+
"generic.not_connected": "ギルドチャットに接続されていません。",
12+
"generic.invalid_selected_guild": "無効なギルドが指定されています。/cgs (ギルド)で選択してください。",
13+
"generic.guild_info_header": "--- ギルド%sの情報 ---",
14+
"generic.guild_members": "メンバー数: %s/%s",
15+
"generic.guild_presence_playing": "プレイ中: %s",
16+
"generic.guild_role.owner": "オーナー: %s",
17+
"generic.guild_role.moderator": "モデレーター: %s",
18+
"generic.guild_role.member": "メンバー: %s",
19+
"generic.guild_open": "公開: %s",
20+
"generic.guild_chat_format": "チャット形式: ",
21+
"generic.copy_on_click": "クリックでコピー",
22+
"generic.guild_not_found": "そんなぎるどないよ %s",
23+
"generic.guild_focus_set": "%s にちゃっとするようにしたよ(/cg <めっせーじ>でできるよ)",
24+
"generic.boolean.true": "はい",
25+
"generic.boolean.false": "いいえ",
26+
"generic.connected": "ギルドチャットに接続しました。",
27+
"generic.disconnected": "ギルドチャットから切断されました。5秒後に再接続を試みます。",
28+
"generic.disconnected_check_api_key": "ギルドチャットから切断されました。APIキーを確認して、/reconnectinterchat [apikey]を実行してください。",
29+
"generic.error_from_server": "サーバーからエラーが返されました: %s",
30+
"generic.error_in_connection": "WebSocket接続でエラーが発生しました: %s"
931
}

fabric-1.16/src/main/java/net/azisaba/interchatmod/fabric/Commands.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,18 @@ public static LiteralArgumentBuilder<FabricClientCommandSource> builderGuild() {
203203
private static int executeInfo(FabricClientCommandSource source) {
204204
Guild guild = Mod.GUILDS.stream().filter(g -> g.id() == Mod.client.getSelectedGuild()).findAny().orElse(null);
205205
if (guild == null) {
206-
source.sendError(Text.of("無効なギルドが指定されています。/cgs (ギルド)で選択してください。"));
206+
source.sendError(new TranslatableText("generic.invalid_selected_guild"));
207207
return 0;
208208
}
209209
Set<GuildMember> members = Mod.guildMembers.getOrDefault(guild.id(), Collections.emptySet());
210210
source.sendFeedback(
211-
new LiteralText("--- ギルド").formatted(Formatting.GOLD)
212-
.append(new LiteralText(guild.name()).formatted(Formatting.AQUA))
213-
.append(new LiteralText("の情報 ---").formatted(Formatting.GOLD))
211+
new TranslatableText("generic.guild_info_header", new LiteralText(guild.name()).formatted(Formatting.AQUA)).formatted(Formatting.GOLD)
214212
);
215213
source.sendFeedback(
216-
new LiteralText("メンバー数: ").formatted(Formatting.GOLD)
217-
.append(new LiteralText(String.valueOf(members.size())).formatted(Formatting.RED))
218-
.append(new LiteralText("/").formatted(Formatting.GOLD))
219-
.append(new LiteralText(String.valueOf(guild.capacity())).formatted(Formatting.RED))
214+
new TranslatableText("generic.guild_members",
215+
new LiteralText(String.valueOf(members.size())).formatted(Formatting.RED),
216+
new LiteralText(String.valueOf(guild.capacity())).formatted(Formatting.RED)
217+
).formatted(Formatting.GOLD)
220218
);
221219
Consumer<String> sendRole = (role) -> {
222220
List<MutableText> players =
@@ -231,15 +229,15 @@ private static int executeInfo(FabricClientCommandSource source) {
231229
.styled(style -> style.withHoverEvent(
232230
new HoverEvent(
233231
HoverEvent.Action.SHOW_TEXT,
234-
new LiteralText("プレイ中: ")
235-
.formatted(Formatting.GREEN)
236-
.append(new LiteralText(member.presence().server).formatted(Formatting.YELLOW)))));
232+
new TranslatableText("generic.guild_presence_playing",
233+
new LiteralText(member.presence().server).formatted(Formatting.YELLOW)
234+
).formatted(Formatting.GREEN))));
237235
} else {
238236
return new LiteralText(member.name()).formatted(Formatting.WHITE);
239237
}
240238
})
241239
.collect(Collectors.toList());
242-
MutableText mu = new LiteralText(role + ": ").formatted(Formatting.GOLD);
240+
MutableText mu = new TranslatableText(roleTranslationKey(role), new LiteralText("")).formatted(Formatting.GOLD);
243241
for (int i = 0; i < players.size(); i++) {
244242
mu = mu.append(players.get(i));
245243
if (i < players.size() - 1) {
@@ -253,15 +251,16 @@ private static int executeInfo(FabricClientCommandSource source) {
253251
sendRole.accept("Member");
254252
source.sendFeedback(Text.of(""));
255253
source.sendFeedback(
256-
new LiteralText("公開: ").formatted(Formatting.GOLD)
257-
.append(new LiteralText(String.valueOf(guild.open())).formatted(guild.open() ? Formatting.GREEN : Formatting.RED))
254+
new TranslatableText("generic.guild_open",
255+
new TranslatableText(booleanTranslationKey(guild.open())).formatted(guild.open() ? Formatting.GREEN : Formatting.RED)
256+
).formatted(Formatting.GOLD)
258257
);
259258
source.sendFeedback(
260-
new LiteralText("チャット形式: ").formatted(Formatting.GOLD)
259+
new TranslatableText("generic.guild_chat_format").formatted(Formatting.GOLD)
261260
.append(new LiteralText(guild.format())
262261
.formatted(Formatting.WHITE)
263262
.styled(style ->
264-
style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("クリックでコピー")))
263+
style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableText("generic.copy_on_click")))
265264
.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, guild.format()))
266265
)
267266
)
@@ -272,19 +271,19 @@ private static int executeInfo(FabricClientCommandSource source) {
272271
private static int executeFocus(FabricClientCommandSource source, String guildName) {
273272
Guild guild = Mod.GUILDS.stream().filter(g -> g.name().equalsIgnoreCase(guildName)).findAny().orElse(null);
274273
if (guild == null) {
275-
source.sendError(Text.of("そんなぎるどないよ " + guildName));
274+
source.sendError(new TranslatableText("generic.guild_not_found", guildName));
276275
return 0;
277276
}
278277
Mod.client.selectGuild(guild.id());
279-
source.sendFeedback(Text.of(guild.name() + " にちゃっとするようにしたよ(/cg <めっせーじ>でできるよ)"));
278+
source.sendFeedback(new TranslatableText("generic.guild_focus_set", guild.name()));
280279
return 1;
281280
}
282281

283282
private static int executeChat(FabricClientCommandSource source, String guildName, String message) {
284283
if (guildName != null) {
285284
Guild guild = Mod.GUILDS.stream().filter(g -> g.name().equalsIgnoreCase(guildName)).findAny().orElse(null);
286285
if (guild == null) {
287-
source.sendError(Text.of("そんなぎるどないよ " + guildName));
286+
source.sendError(new TranslatableText("generic.guild_not_found", guildName));
288287
return 0;
289288
}
290289
Mod.client.sendMessageToGuild(guild.id(), message);
@@ -293,4 +292,12 @@ private static int executeChat(FabricClientCommandSource source, String guildNam
293292
}
294293
return 1;
295294
}
295+
296+
private static String roleTranslationKey(String role) {
297+
return "generic.guild_role." + role.toLowerCase(Locale.ROOT);
298+
}
299+
300+
private static String booleanTranslationKey(boolean value) {
301+
return "generic.boolean." + value;
302+
}
296303
}

fabric-1.16/src/main/java/net/azisaba/interchatmod/fabric/ModConfigScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ModConfigScreen extends GameOptionsScreen {
2222
private ButtonListWidget list;
2323

2424
public ModConfigScreen(@Nullable Screen previous) {
25-
super(previous, MinecraftClient.getInstance().options, Text.of("InterChatMod"));
25+
super(previous, MinecraftClient.getInstance().options, new TranslatableText("text.config.interchat-config.title"));
2626
this.previous = previous;
2727
}
2828

fabric-1.16/src/main/java/net/azisaba/interchatmod/fabric/mixin/MixinClientPlayerEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.minecraft.client.MinecraftClient;
66
import net.minecraft.client.network.ClientPlayerEntity;
77
import net.minecraft.text.Text;
8+
import net.minecraft.text.TranslatableText;
89
import org.java_websocket.exceptions.WebsocketNotConnectedException;
910
import org.spongepowered.asm.mixin.Mixin;
1011
import org.spongepowered.asm.mixin.injection.At;
@@ -29,7 +30,7 @@ public void maybeCancel(String content, CallbackInfo ci) {
2930
Mod.client.sendMessageToGuild(null, content);
3031
} catch (WebsocketNotConnectedException e) {
3132
assert MinecraftClient.getInstance().player != null;
32-
MinecraftClient.getInstance().player.sendMessage(Text.of("ギルドチャットに接続されていません。"), false);
33+
MinecraftClient.getInstance().player.sendMessage(new TranslatableText("generic.not_connected"), false);
3334
Mod.reconnect();
3435
}
3536
}

0 commit comments

Comments
 (0)