-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_script.py
More file actions
981 lines (848 loc) · 35.2 KB
/
export_script.py
File metadata and controls
981 lines (848 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
#!/usr/bin/env python3
"""
RPG Maker MV/MZ Text Exporter
Exports translatable text from RPG Maker game data to JSON format for translation
Supports:
- Map events (Map*.json) - Dialogues and event commands
- Common events (CommonEvents.json)
- System data (System.json) - Terms, menu texts
- Actors, Items, Skills, Enemies, etc.
Author: GalTransl-RPGmaker Project
"""
import json
import argparse
import re
from pathlib import Path
from dataclasses import dataclass, asdict, field
from typing import List, Dict, Any, Optional
# RPG Maker event command codes for text
TEXT_CODES = {
101: "show_text_options", # Show Text (顯示對話設定)
102: "show_choices", # Show Choices (顯示選項)
401: "show_text_content", # Text content (對話內容)
402: "choice_branch", # When [Choice]
405: "scroll_text_content", # Scroll Text content
108: "comment", # Comment
408: "comment_content", # Comment content
355: "script", # Script
655: "script_content", # Script content
320: "change_name", # Change Name
324: "change_nickname", # Change Nickname
231: "show_picture", # Show Picture (圖片名稱可能需要翻譯)
356: "plugin_command", # Plugin Command
}
@dataclass
class TextEntry:
"""Represents a translatable text entry"""
index: int
source_file: str
location: str # e.g., "Map001/Event1/Page1/Command5"
original: str
translated: str = ""
context: str = "" # dialog, choice, name, description, etc.
speaker: str = "" # Speaker name if available
code: int = 0 # Event command code
@dataclass
class ExportInfo:
"""Export metadata"""
game_title: str = ""
version: str = "1.0"
total_strings: int = 0
source_files: List[str] = field(default_factory=list)
@dataclass
class ContextTypeInfo:
"""Defines a context type for translation"""
description: str
priority: str # 高, 中, 低, 最低
style: str
note: str = ""
# Centralized context type definitions
CONTEXT_TYPES: Dict[str, ContextTypeInfo] = {
"dialog": ContextTypeInfo("對話文本", "高", "自然口語化"),
"choice": ContextTypeInfo("選項文本", "高", "簡潔明確", "可能包含 if(s[xxx]) 條件需保留"),
"scroll_text": ContextTypeInfo("滾動文字", "高", "文學性較強"),
"picture_name": ContextTypeInfo("圖片名稱", "低", "通常不需翻譯"),
"map_name": ContextTypeInfo("地圖名稱", "中", "簡短地點名稱"),
"event_name": ContextTypeInfo("事件名稱", "低", "開發者參考用"),
"common_event_name": ContextTypeInfo("公共事件名稱", "低", "開發者參考用"),
"game_title": ContextTypeInfo("遊戲標題", "高", "精心翻譯"),
"term": ContextTypeInfo("術語", "中", "業界通用翻譯"),
"command": ContextTypeInfo("選單指令", "高", "簡潔動詞/名詞"),
"param": ContextTypeInfo("參數名", "中", "業界通用翻譯"),
"message": ContextTypeInfo("系統訊息", "中", "保留 %1 %2 佔位符"),
"actor": ContextTypeInfo("角色資料", "高", "名稱可音譯或意譯"),
"skill": ContextTypeInfo("技能資料", "中", "有遊戲感"),
"item": ContextTypeInfo("道具資料", "中", "有遊戲感"),
"weapon": ContextTypeInfo("武器資料", "中", "有遊戲感"),
"armor": ContextTypeInfo("防具資料", "中", "有遊戲感"),
"enemy": ContextTypeInfo("敵人資料", "中", "有威脅感"),
"state": ContextTypeInfo("狀態資料", "中", "簡潔明確"),
"type_name": ContextTypeInfo("類型名稱", "低", "通用類型名稱"),
"debug_name": ContextTypeInfo("除錯名稱", "最低", "可跳過不翻譯"),
"class": ContextTypeInfo("職業資料", "中", "符合遊戲風格"),
"plugin_command": ContextTypeInfo("插件指令", "低", "通常不需翻譯"),
"currency": ContextTypeInfo("貨幣單位", "中", "簡短名稱"),
}
# Source file descriptions
SOURCE_FILES: Dict[str, str] = {
"System.json": "系統設定、術語、選單文字",
"CommonEvents.json": "公共事件(對話、劇情)",
"Map*.json": "地圖事件",
"Actors.json": "角色資料",
"Skills.json": "技能資料",
"Items.json": "道具資料",
"Weapons.json": "武器資料",
"Armors.json": "防具資料",
"Enemies.json": "敵人資料",
"States.json": "狀態資料",
"Classes.json": "職業資料",
}
# RPG Maker control codes
CONTROL_CODES: Dict[str, str] = {
"\\n": "換行符號,多行對話會用此分隔",
"\\\\N[n]": "遊戲中會顯示第 n 號角色的名稱",
"\\\\V[n]": "遊戲中會顯示第 n 號變數的值",
"\\\\C[n]": "文字顏色代碼",
"\\\\G": "貨幣單位",
"\\\\I[n]": "顯示第 n 號圖標",
"\\\\{": "放大文字",
"\\\\}": "縮小文字",
"\\\\!": "等待按鍵",
"\\\\.": "等待 1/4 秒",
"\\\\|": "等待 1 秒",
}
# Event command code descriptions
EVENT_COMMAND_CODES: Dict[int, str] = {
0: "無特定代碼(資料庫條目)",
101: "顯示文字指令(對話開始)",
102: "顯示選項指令",
231: "顯示圖片指令",
356: "插件指令",
401: "文字內容(對話正文)",
405: "滾動文字內容",
}
class FormatSpecificationBuilder:
"""Builder class for generating format specification document"""
def __init__(self, version: str = "1.0"):
self.version = version
def _build_file_structure(self) -> Dict[str, Any]:
"""Build file structure section"""
return {
"info": {
"description": "翻譯檔案的元數據資訊",
"fields": {
"game_title": {"type": "string", "description": "遊戲原始標題"},
"version": {"type": "string", "description": "翻譯檔案版本"},
"string_count": {"type": "integer", "description": "總字串數量"}
}
},
"strings": {
"description": "可翻譯字串陣列,每個元素代表一個需要翻譯的文本條目",
"type": "array"
}
}
def _build_field_spec(self, name: str, field_type: str, description: str,
extra: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""Build a single field specification"""
spec = {
"type": field_type,
"description": description,
}
if extra:
spec.update(extra)
return spec
def _build_strings_fields(self) -> Dict[str, Any]:
"""Build strings array fields specification"""
return {
"index": self._build_field_spec(
"index", "integer",
"字串在陣列中的唯一索引編號,從 0 開始遞增",
{"translation_note": "此欄位僅供定位用途,翻譯時不需修改"}
),
"source_file": self._build_field_spec(
"source_file", "string",
"此文本來源的 RPG Maker 資料檔案名稱",
{
"possible_values": [f"{k} - {v}" for k, v in SOURCE_FILES.items()],
"translation_note": "此欄位僅供參考,翻譯時不需修改"
}
),
"location": self._build_field_spec(
"location", "string",
"文本在來源檔案中的精確位置路徑",
{
"format_examples": {
"event_dialog": "Event1/Page0/Cmd5 - 地圖事件1/頁面0/指令5",
"common_event_dialog": "CE100/Cmd258 - 公共事件100/指令258",
"choice": "CE84/Cmd18/Choice0 - 公共事件84/指令18/選項0",
"event_name": "Event1/name - 事件名稱",
"system_term": "terms/messages/actionFailure - 系統術語",
"database_entry": "ID1/name - 資料庫項目1的名稱欄位"
},
"translation_note": "此欄位僅供定位用途,翻譯時不需修改"
}
),
"original": self._build_field_spec(
"original", "string",
"原始日文文本,這是需要被翻譯的內容",
{
"special_characters": CONTROL_CODES,
"translation_note": "翻譯時必須保留所有特殊控制碼,僅翻譯可見文字部分"
}
),
"translated": self._build_field_spec(
"translated", "string",
"翻譯後的文本,AI 翻譯代理需要填入此欄位",
{
"default": "",
"translation_rules": [
"將 original 欄位的日文翻譯成目標語言",
"保留所有 \\\\N[n]、\\\\V[n]、\\\\C[n] 等控制碼不變",
"保留 \\n 換行符號的位置(可適當調整以配合翻譯)",
"對於選項類型(choice),如包含條件判斷 if(s[xxx]),需保留條件部分",
"注意翻譯長度,過長可能導致遊戲顯示問題",
"空字串表示尚未翻譯"
]
}
),
"context": self._build_field_spec(
"context", "string",
"文本的類型分類,用於判斷翻譯策略和風格",
{"possible_values": self._build_context_values()}
),
"speaker": self._build_field_spec(
"speaker", "string",
"說話者名稱(如果有的話)",
{
"translation_note": "此欄位可幫助理解對話語境和說話風格",
"usage": ["判斷說話者性格以調整翻譯語氣", "保持同一角色的用語一致性"]
}
),
"code": self._build_field_spec(
"code", "integer",
"RPG Maker 事件指令代碼",
{
"common_values": {str(k): v for k, v in EVENT_COMMAND_CODES.items()},
"translation_note": "此欄位僅供參考,翻譯時不需修改"
}
),
}
def _build_context_values(self) -> Dict[str, Dict[str, str]]:
"""Build context possible values from CONTEXT_TYPES"""
result = {}
for ctx_name, ctx_info in CONTEXT_TYPES.items():
value = {
"description": ctx_info.description,
"priority": ctx_info.priority,
"style": ctx_info.style,
}
if ctx_info.note:
value["note"] = ctx_info.note
result[ctx_name] = value
return result
def _build_translation_examples(self) -> List[Dict[str, Any]]:
"""Build translation examples"""
examples = [
("基本對話翻譯",
"目を覚ますと、見慣れない白い天井。",
"醒來後,映入眼簾的是陌生的白色天花板。"),
("多行對話(保留換行)",
"こんにちは。\n元気ですか?",
"你好。\n你還好嗎?"),
("帶條件的選項(保留條件)",
"1日目 if(s[211])",
"第一天 if(s[211])"),
("系統訊息(保留佔位符)",
"%1は %2 のダメージを受けた!",
"%1 受到了 %2 點傷害!"),
("控制碼(保留控制碼)",
"\\C[2]重要\\C[0]な情報です。",
"這是\\C[2]重要\\C[0]的情報。"),
]
return [
{
"description": desc,
"before": {"original": orig, "translated": ""},
"after": {"original": orig, "translated": trans}
}
for desc, orig, trans in examples
]
def _build_priority_order(self) -> List[str]:
"""Build priority order based on CONTEXT_TYPES"""
priority_groups = {
"高": [],
"中": [],
"低": [],
"最低": [],
}
for ctx_name, ctx_info in CONTEXT_TYPES.items():
priority_groups[ctx_info.priority].append(ctx_name)
order = []
priority_labels = {
"高": "最優先",
"中": "",
"低": "可選翻譯",
"最低": "可跳過",
}
idx = 1
for priority in ["高", "中", "低", "最低"]:
if priority_groups[priority]:
items = " / ".join(priority_groups[priority])
label = priority_labels[priority]
suffix = f"({label})" if label else ""
order.append(f"{idx}. {items}{suffix}")
idx += 1
return order
def _build_ai_guidelines(self) -> Dict[str, Any]:
"""Build AI agent guidelines"""
return {
"workflow": [
"1. 讀取翻譯 JSON 檔案",
"2. 遍歷 strings 陣列中的每個條目",
"3. 根據 context 欄位判斷翻譯策略",
"4. 翻譯 original 欄位的內容",
"5. 將翻譯結果寫入 translated 欄位",
"6. 保存修改後的 JSON 檔案"
],
"priority_order": self._build_priority_order(),
"quality_checks": [
"確保所有控制碼(\\\\N[n], \\\\V[n] 等)被保留",
"確保佔位符(%1, %2 等)被保留",
"確保條件判斷(if(s[xxx]))被保留",
"檢查翻譯長度是否合理",
"保持同一角色用語風格一致"
],
"skip_conditions": [
f"context 為 '{ctx}' 的條目可跳過"
for ctx, info in CONTEXT_TYPES.items()
if info.priority == "最低"
] + [
"context 為 'plugin_command' 且不含日文顯示文字的可跳過",
"context 為 'picture_name' 且為純檔名的可跳過"
]
}
def build(self) -> Dict[str, Any]:
"""Build the complete format specification"""
return {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "GalTransl-RPGmaker Translation File Format",
"description": "RPG Maker MV/MZ 遊戲翻譯檔案格式說明,供 AI 翻譯代理參考",
"version": self.version,
"file_structure": self._build_file_structure(),
"strings_array_fields": self._build_strings_fields(),
"translation_examples": self._build_translation_examples(),
"ai_agent_guidelines": self._build_ai_guidelines(),
}
def generate_format_specification(version: str = "1.0") -> Dict[str, Any]:
"""
Generate JSON format specification document for AI translation agents.
Uses FormatSpecificationBuilder to create a modular, maintainable specification
based on centralized definitions (CONTEXT_TYPES, SOURCE_FILES, etc.)
Args:
version: Version string for the specification document
Returns:
Complete format specification dictionary
"""
builder = FormatSpecificationBuilder(version=version)
return builder.build()
def is_translatable_text(text: str) -> bool:
"""
Check if a string contains translatable content.
Filter out control-only strings, empty strings, etc.
"""
if not text or not isinstance(text, str):
return False
# Remove leading/trailing whitespace
text = text.strip()
if not text:
return False
# Skip if it's just a file path or resource name
if text.endswith(('.png', '.jpg', '.ogg', '.m4a', '.wav', '.mp3')):
return False
# Skip if it's just numbers
if text.isdigit():
return False
# Skip if it's a plugin command that doesn't need translation
plugin_skip_patterns = [
r'^[A-Z_]+\s+\d+', # PLUGIN_NAME 123
r'^P_CALL_CE',
r'^MSGSE\s+',
r'^Choicelist_offset',
r'^createFilter',
r'^setFilter',
r'^eraseAllFilter',
]
for pattern in plugin_skip_patterns:
if re.match(pattern, text):
return False
# Check if contains Japanese/Chinese/Korean characters (likely needs translation)
if re.search(r'[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF\uAC00-\uD7AF]', text):
return True
# Check if contains meaningful alphabetic text (at least 2 letters)
if re.search(r'[a-zA-Z]{2,}', text):
# Skip common non-translatable patterns
skip_patterns = [
r'^[A-Z]+\d*$', # ALL_CAPS_VAR
r'^if\(s\[', # if(s[xxx])
r'^\d+[a-z]$', # 1a, 2b, etc.
]
for pattern in skip_patterns:
if re.match(pattern, text):
return False
return True
return False
def extract_event_commands(commands: List[Dict],
source_file: str,
location_prefix: str) -> List[TextEntry]:
"""
Extract translatable text from event command list.
"""
entries = []
index = 0
current_speaker = ""
i = 0
while i < len(commands):
cmd = commands[i]
code = cmd.get("code", 0)
params = cmd.get("parameters", [])
# Show Text Options (code 101)
# parameters: [face_name, face_index, background, position]
# Next commands with code 401 are the actual text
if code == 101:
if len(params) >= 1:
current_speaker = params[0] if params[0] else ""
# Collect all following 401 commands
text_parts = []
j = i + 1
while j < len(commands) and commands[j].get("code") == 401:
text_params = commands[j].get("parameters", [])
if text_params and isinstance(text_params[0], str):
text_parts.append(text_params[0])
j += 1
full_text = "\n".join(text_parts)
if is_translatable_text(full_text):
entries.append(TextEntry(
index=index,
source_file=source_file,
location=f"{location_prefix}/Cmd{i}",
original=full_text,
context="dialog",
speaker=current_speaker,
code=code
))
index += 1
i = j
continue
# Show Choices (code 102)
# parameters: [choices_array, cancel_type, default_type, position_type, background]
elif code == 102:
if params and isinstance(params[0], list):
for choice_idx, choice in enumerate(params[0]):
# Filter out conditional choices like "朝 if(s[240])"
clean_choice = re.sub(r'\s*if\(s\[\d+\]\)', '', str(choice)).strip()
if is_translatable_text(clean_choice):
entries.append(TextEntry(
index=index,
source_file=source_file,
location=f"{location_prefix}/Cmd{i}/Choice{choice_idx}",
original=choice,
context="choice",
code=code
))
index += 1
# Scroll Text content (code 405)
elif code == 405:
if params and isinstance(params[0], str) and is_translatable_text(params[0]):
entries.append(TextEntry(
index=index,
source_file=source_file,
location=f"{location_prefix}/Cmd{i}",
original=params[0],
context="scroll_text",
code=code
))
index += 1
# Show Picture (code 231) - picture name might contain text
elif code == 231:
if params and len(params) >= 2 and isinstance(params[1], str):
pic_name = params[1]
# Only include if it contains Japanese text (likely needs translation)
if re.search(r'[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF]', pic_name):
entries.append(TextEntry(
index=index,
source_file=source_file,
location=f"{location_prefix}/Cmd{i}",
original=pic_name,
context="picture_name",
code=code
))
index += 1
# Plugin Command (code 356)
elif code == 356:
if params and isinstance(params[0], str):
plugin_text = params[0]
# Check if contains translatable text within plugin command
if is_translatable_text(plugin_text) and re.search(r'[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FFF]', plugin_text):
entries.append(TextEntry(
index=index,
source_file=source_file,
location=f"{location_prefix}/Cmd{i}",
original=plugin_text,
context="plugin_command",
code=code
))
index += 1
i += 1
return entries
def extract_map_data(map_file: Path) -> List[TextEntry]:
"""
Extract translatable text from a map file.
"""
entries = []
with open(map_file, 'r', encoding='utf-8') as f:
data = json.load(f)
source_file = map_file.name
# Extract displayName if present
if data.get("displayName") and is_translatable_text(data["displayName"]):
entries.append(TextEntry(
index=0,
source_file=source_file,
location="displayName",
original=data["displayName"],
context="map_name"
))
# Extract from events
events = data.get("events", [])
for event in events:
if event is None:
continue
event_id = event.get("id", 0)
event_name = event.get("name", "")
# Extract event name if translatable
if is_translatable_text(event_name):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"Event{event_id}/name",
original=event_name,
context="event_name"
))
# Extract from event pages
pages = event.get("pages", [])
for page_idx, page in enumerate(pages):
commands = page.get("list", [])
location_prefix = f"Event{event_id}/Page{page_idx}"
page_entries = extract_event_commands(commands, source_file, location_prefix)
for entry in page_entries:
entry.index = len(entries)
entries.append(entry)
return entries
def extract_common_events(ce_file: Path) -> List[TextEntry]:
"""
Extract translatable text from CommonEvents.json.
"""
entries = []
with open(ce_file, 'r', encoding='utf-8') as f:
data = json.load(f)
source_file = ce_file.name
for event in data:
if event is None:
continue
event_id = event.get("id", 0)
event_name = event.get("name", "")
# Extract event name if translatable
if is_translatable_text(event_name):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"CE{event_id}/name",
original=event_name,
context="common_event_name"
))
# Extract from command list
commands = event.get("list", [])
location_prefix = f"CE{event_id}"
cmd_entries = extract_event_commands(commands, source_file, location_prefix)
for entry in cmd_entries:
entry.index = len(entries)
entries.append(entry)
return entries
def extract_system_data(system_file: Path) -> List[TextEntry]:
"""
Extract translatable text from System.json.
"""
entries = []
with open(system_file, 'r', encoding='utf-8') as f:
data = json.load(f)
source_file = system_file.name
# Game title
if data.get("gameTitle") and is_translatable_text(data["gameTitle"]):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location="gameTitle",
original=data["gameTitle"],
context="game_title"
))
# Currency unit
if data.get("currencyUnit") and is_translatable_text(data["currencyUnit"]):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location="currencyUnit",
original=data["currencyUnit"],
context="currency"
))
# Type names (armor types, skill types, etc.)
type_arrays = ["armorTypes", "equipTypes", "skillTypes", "weaponTypes", "elements"]
for type_name in type_arrays:
if type_name in data:
for idx, value in enumerate(data[type_name]):
if is_translatable_text(value):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"{type_name}/{idx}",
original=value,
context="type_name"
))
# Switches and Variables names
for var_type in ["switches", "variables"]:
if var_type in data:
for idx, value in enumerate(data[var_type]):
if is_translatable_text(value):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"{var_type}/{idx}",
original=value,
context="debug_name"
))
# Terms
if "terms" in data:
terms = data["terms"]
# Basic terms
if "basic" in terms:
for idx, term in enumerate(terms["basic"]):
if is_translatable_text(term):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"terms/basic/{idx}",
original=term,
context="term"
))
# Command terms
if "commands" in terms:
for idx, term in enumerate(terms["commands"]):
if term and is_translatable_text(term):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"terms/commands/{idx}",
original=term,
context="command"
))
# Params
if "params" in terms:
for idx, term in enumerate(terms["params"]):
if is_translatable_text(term):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"terms/params/{idx}",
original=term,
context="param"
))
# Messages
if "messages" in terms:
for key, msg in terms["messages"].items():
if is_translatable_text(msg):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"terms/messages/{key}",
original=msg,
context="message"
))
return entries
def extract_database_entries(db_file: Path, fields: List[str], context: str) -> List[TextEntry]:
"""
Extract translatable text from database files (Actors, Items, Skills, etc.)
"""
entries = []
with open(db_file, 'r', encoding='utf-8') as f:
data = json.load(f)
source_file = db_file.name
for item in data:
if item is None:
continue
item_id = item.get("id", 0)
for field_name in fields:
if field_name in item and is_translatable_text(item[field_name]):
entries.append(TextEntry(
index=len(entries),
source_file=source_file,
location=f"ID{item_id}/{field_name}",
original=item[field_name],
context=context
))
return entries
def export_all(game_dir: Path, output_dir: Path, split_by_file: bool = False):
"""
Export all translatable text from the game data directory.
"""
data_dir = game_dir / "www" / "data"
if not data_dir.exists():
# Try alternative structure (direct www folder)
data_dir = game_dir / "data"
if not data_dir.exists():
raise FileNotFoundError(f"Data directory not found in {game_dir}")
all_entries = []
source_files = []
game_title = ""
# Extract System.json first to get game title
system_file = data_dir / "System.json"
if system_file.exists():
with open(system_file, 'r', encoding='utf-8') as f:
system_data = json.load(f)
game_title = system_data.get("gameTitle", "Unknown")
print(f"Processing: {system_file.name}")
entries = extract_system_data(system_file)
if entries:
all_entries.extend(entries)
source_files.append(system_file.name)
# Extract CommonEvents.json
ce_file = data_dir / "CommonEvents.json"
if ce_file.exists():
print(f"Processing: {ce_file.name}")
entries = extract_common_events(ce_file)
if entries:
all_entries.extend(entries)
source_files.append(ce_file.name)
# Extract Map files
map_files = sorted(data_dir.glob("Map*.json"))
for map_file in map_files:
if map_file.name == "MapInfos.json":
continue
print(f"Processing: {map_file.name}")
entries = extract_map_data(map_file)
if entries:
all_entries.extend(entries)
source_files.append(map_file.name)
# Extract database files
db_configs = [
("Actors.json", ["name", "nickname", "profile"], "actor"),
("Classes.json", ["name"], "class"),
("Skills.json", ["name", "description", "message1", "message2"], "skill"),
("Items.json", ["name", "description"], "item"),
("Weapons.json", ["name", "description"], "weapon"),
("Armors.json", ["name", "description"], "armor"),
("Enemies.json", ["name"], "enemy"),
("States.json", ["name", "message1", "message2", "message3", "message4"], "state"),
]
for filename, fields, context in db_configs:
db_file = data_dir / filename
if db_file.exists():
print(f"Processing: {filename}")
entries = extract_database_entries(db_file, fields, context)
if entries:
all_entries.extend(entries)
source_files.append(filename)
# Reassign indices
for idx, entry in enumerate(all_entries):
entry.index = idx
# Create output directory
output_dir.mkdir(parents=True, exist_ok=True)
# Prepare output data
info = {
"game_title": game_title,
"version": "1.0",
"string_count": len(all_entries)
}
if split_by_file:
# Split by source file
by_file = {}
for entry in all_entries:
if entry.source_file not in by_file:
by_file[entry.source_file] = []
by_file[entry.source_file].append(entry)
for source_file, entries in by_file.items():
# Reassign indices within each file
for idx, entry in enumerate(entries):
entry.index = idx
output_data = {
"info": {
"game_title": game_title,
"source_file": source_file,
"string_count": len(entries)
},
"strings": [asdict(e) for e in entries]
}
# Use source file name as output name
output_name = source_file.replace('.json', '_text.json')
output_file = output_dir / output_name
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(output_data, f, ensure_ascii=False, indent=2)
print(f"Exported: {output_file} ({len(entries)} strings)")
else:
# Single output file
output_data = {
"info": info,
"strings": [asdict(e) for e in all_entries]
}
output_file = output_dir / "script.json"
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(output_data, f, ensure_ascii=False, indent=2)
print(f"\nExported: {output_file}")
# Generate and save format specification
format_spec = generate_format_specification()
format_spec_file = output_dir / "format_specification.json"
with open(format_spec_file, 'w', encoding='utf-8') as f:
json.dump(format_spec, f, ensure_ascii=False, indent=2)
print(f"Generated: {format_spec_file}")
print("\n=== Export Summary ===")
print(f"Game Title: {game_title}")
print(f"Total strings: {len(all_entries)}")
print(f"Source files: {len(source_files)}")
# Print breakdown by context
context_counts = {}
for entry in all_entries:
context_counts[entry.context] = context_counts.get(entry.context, 0) + 1
print("\nBy context:")
for ctx, count in sorted(context_counts.items(), key=lambda x: -x[1]):
print(f" {ctx}: {count}")
def main():
parser = argparse.ArgumentParser(
description="RPG Maker MV/MZ Text Exporter - Export game text for translation"
)
parser.add_argument(
"game_dir",
type=Path,
help="Path to the game directory (containing www/data folder)"
)
parser.add_argument(
"-o", "--output",
type=Path,
default=Path("exported"),
help="Output directory for JSON files (default: exported)"
)
parser.add_argument(
"-s", "--split",
action="store_true",
help="Split output by source file"
)
args = parser.parse_args()
if not args.game_dir.exists():
print(f"Error: Game directory not found: {args.game_dir}")
return 1
try:
export_all(args.game_dir, args.output, args.split)
return 0
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
exit(main())