了解如何为 ComfyUI 自定义节点添加多语言支持
locales
文件夹,支持多种翻译文件类型:
your_custom_node/
├── __init__.py
├── your_node.py
└── locales/ # 国际化文件夹
├── en/ # 英语翻译(推荐作为基准)
│ ├── main.json # 通用英文翻译内容
│ ├── nodeDefs.json # 英文节点定义翻译
│ ├── settings.json # 可选:设置界面翻译
│ └── commands.json # 可选:命令翻译
├── zh/ # 中文翻译文件
│ ├── nodeDefs.json # 中文节点定义翻译
│ ├── main.json # 通用中文翻译内容
│ ├── settings.json # 中文设置界面翻译
│ └── commands.json # 中文命令翻译
├──...
class I18nTextProcessor:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {
"multiline": True,
"default": "Hello World!",
"tooltip": "The original text content to be processed"
}),
"operation": (["uppercase", "lowercase", "reverse", "add_prefix"], {
"default": "uppercase",
"tooltip": "The text processing operation to be executed"
}),
"count": ("INT", {
"default": 1,
"min": 1,
"max": 10,
"step": 1,
"tooltip": "The number of times to repeat the operation"
}),
},
"optional": {
"prefix": ("STRING", {
"default": "[I18N] ",
"multiline": False,
"tooltip": "The prefix to add to the text"
}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("processed_text",)
FUNCTION = "process_text"
CATEGORY = "I18n Demo"
DESCRIPTION = "A simple i18n demo node that demonstrates text processing with internationalization support"
def process_text(self, text, operation, count, prefix=""):
try:
result = text
for _ in range(count):
if operation == "uppercase":
result = result.upper()
elif operation == "lowercase":
result = result.lower()
elif operation == "reverse":
result = result[::-1]
elif operation == "add_prefix":
result = prefix + result
return (result,)
except Exception as e:
print(f"I18nTextProcessor error: {e}")
return (f"Error: {str(e)}",)
{
"I18nTextProcessor": {
"display_name": "国际化文本处理器",
"description": "一个简单的国际化演示节点,展示支持国际化的文本处理功能",
"inputs": {
"text": {
"name": "文本输入",
"tooltip": "待处理的原始文本内容"
},
"operation": {
"name": "操作类型",
"tooltip": "要执行的文本处理操作",
"options": {
"uppercase": "转为大写",
"lowercase": "转为小写",
"reverse": "反转文本",
"add_prefix": "添加前缀"
}
},
"count": {
"name": "重复次数",
"tooltip": "操作重复执行的次数"
},
"prefix": {
"name": "前缀文本",
"tooltip": "要添加到文本前的前缀"
}
},
"outputs": {
"0": {
"name": "处理后文本",
"tooltip": "最终处理后的文本结果"
}
}
}
}
0
, 第二个输出为 1
, 以此类推。
app.registerExtension({
name: "I18nDemo",
settings: [
{
id: "I18nDemo.EnableDebugMode",
category: ["I18nDemo","DebugMode"], // This matches the settingsCategories key in main.json
name: "Enable Debug Mode", // Will be overridden by translation
tooltip: "Show debug information in console for i18n demo nodes", // Will be overridden by translation
type: "boolean",
defaultValue: false,
experimental: true,
onChange: (value) => {
console.log("I18n Demo:", value ? "Debug mode enabled" : "Debug mode disabled");
}
},
{
id: "I18nDemo.DefaultTextOperation",
category: ["I18nDemo","DefaultTextOperation"], // This matches the settingsCategories key in main.json
name: "Default Text Operation", // Will be overridden by translation
tooltip: "Default operation for text processor node", // Will be overridden by translation
type: "combo",
options: ["uppercase", "lowercase", "reverse", "add_prefix"],
defaultValue: "uppercase",
experimental: true
}
],
})
main.json
中添加
{
"settingsCategories": {
"I18nDemo": "I18n Demo",
"DebugMode": "Debug Mode",
"DefaultTextOperation": "Default Text Operation"
}
}
settings.json
中,比如:
{
"I18nDemo_EnableDebugMode": {
"name": "启用调试模式",
"tooltip": "在控制台显示国际化演示节点的调试信息"
},
"I18nDemo_DefaultTextOperation": {
"name": "默认文本操作",
"tooltip": "文本处理节点的默认操作",
"options": {
"uppercase": "大写",
"lowercase": "小写",
"reverse": "反转",
"add_prefix": "添加前缀"
}
}
}
.
替换为 _
如:
"I18nDemo.EnableDebugMode" -> "I18nDemo_EnableDebugMode"
此页面对您有帮助吗?