def detect_encoding(file_path): """检测文件编码""" encodings = ['utf-8', 'utf-16', 'utf-16le', 'utf-16be', 'gb18030', 'gbk'] for encoding in encodings: try: with codecs.open(file_path, 'r', encoding=encoding) as f: f.read() return encoding except UnicodeDecodeError: continue return None
def load_translations(txt_file): """加载翻译文件到字典""" translations = {} encoding = detect_encoding(txt_file) if not encoding: raise ValueError("无法检测文件编码")
with codecs.open(txt_file, 'r', encoding=encoding) as f: for line in f: line = line.strip() if line and '\t' in line: key, value = line.split('\t', 1) translations[key] = value return translations
def replace_variables(data, translations): """递归替换JSON中的变量""" if isinstance(data, dict): for key in list(data.keys()): data[key] = replace_variables(data[key], translations) elif isinstance(data, list): for i in range(len(data)): data[i] = replace_variables(data[i], translations) elif isinstance(data, str) and data.startswith('): return translations.get(data, data) return data
24 comments
import json
import codecs
def detect_encoding(file_path):
"""检测文件编码"""
encodings = ['utf-8', 'utf-16', 'utf-16le', 'utf-16be', 'gb18030', 'gbk']
for encoding in encodings:
try:
with codecs.open(file_path, 'r', encoding=encoding) as f:
f.read()
return encoding
except UnicodeDecodeError:
continue
return None
def load_translations(txt_file):
"""加载翻译文件到字典"""
translations = {}
encoding = detect_encoding(txt_file)
if not encoding:
raise ValueError("无法检测文件编码")
with codecs.open(txt_file, 'r', encoding=encoding) as f:
for line in f:
line = line.strip()
if line and '\t' in line:
key, value = line.split('\t', 1)
translations[key] = value
return translations
def replace_variables(data, translations):
"""递归替换JSON中的变量"""
if isinstance(data, dict):
for key in list(data.keys()):
data[key] = replace_variables(data[key], translations)
elif isinstance(data, list):
for i in range(len(data)):
data[i] = replace_variables(data[i], translations)
elif isinstance(data, str) and data.startswith('):
return translations.get(data, data)
return data
def main():
# 从用户输入获取文件名
json_input = input("请输入源JSON文件名: ")
txt_file = input("请输入翻译文本文件名: ")
json_output = input("请输入输出JSON文件名: ")
try:
# 加载翻译文件
translations = load_translations(txt_file)
# 读取原始JSON
with open(json_input, 'r', encoding='utf-8') as f:
config = json.load(f)
# 执行替换
replaced_config = replace_variables(config, translations)
# 写入新JSON
with open(json_output, 'w', encoding='utf-8', newline='\n') as f:
json.dump(replaced_config, f, ensure_ascii=False, indent=4, sort_keys=False)
print("转换完成!")
except FileNotFoundError as e:
print(f"错误:找不到文件 - {e.filename}")
except ValueError as e:
print(f"错误:{str(e)}")
except Exception as e:
print(f"发生错误:{str(e)}")
if __name__ == '__main__':
main()
https://www.nexusmods.com/fallout4/mods/49070
https://www.nexusmods.com/fallout4/mods/56922
https://www.nexusmods.com/fallout4/mods/49997
https://www.nexusmods.com/fallout4/mods/27479
不知道是不是可以请作者分析一下原因,谢谢。
我看了一下, 他们全部好像都是MCMminversion 1或者2的, 而且config文件夹都有一个setting.ini,
我也不太懂这个版本的translations应该在哪里, 关键字应该是什么格式, 试了好几种都不行, 看共同特征也看不出啥, photomode里甚至还有自带的翻译文件, 不过在bs2打包里, 但是只要更改了关键字就不行, 关键字又是PM_MCM这种缩写, 也找不到在哪里定义了只能是这个关键字, 无能为力嘞, 可能是旧版mcm的某种实现方法吧.🤦
每次mod更新对比MCM的变化真的要疯了,还没用,先点赞感谢。
话说为什么辐射4的MCM变成.json了,技术上来说,对比老滚的.pex是进步还是退步?
所以原理是查找并替换原json文件中待翻译条目然后生成新json文件和与新json文件中条目相对应的翻译文件?
测试了几个不提供翻译文件的配置文件,感觉挺方便的,多谢大佬~
Ps.有些配置文件的“options”里会有需要翻译的词(比如从MCM更改默认涂装/皮肤/种族,然后在options里列出来对应的项),但是好像用这个工具是无法把这些给提取出来的,是这个工具只查找text和help,还是我没搞清楚怎么弄?
顺带问一下,上面那个mod因为没给翻译文件,我汉化直接翻了json里的条目,其他人下载应该没问题吧?
我把本地化和选择生成_en 还是_cn或者_dr类似的选项也加上
我之前测试只用一个mod测试的, l网有个朋友发现会重复菜单, 我才知道mcm不会用 modname_langcode.txt 这个文件的名字分辨mod, 只靠$keyword
必须更新新版给关键字加mod名字的前缀, 不然问题很大