|
| 1 | +--- |
| 2 | +title: "ComfyUI Custom Nodes i18n Support" |
| 3 | +description: "Learn how to add multi-language support for ComfyUI custom nodes" |
| 4 | +sidebarTitle: "i18n Support" |
| 5 | +--- |
| 6 | + |
| 7 | +import SupportedLanguages from '/snippets/interface/supported-languages.mdx' |
| 8 | + |
| 9 | +If you want to add support for multiple languages, you can refer to this document to learn how to implement multi-language support. |
| 10 | + |
| 11 | +<SupportedLanguages /> |
| 12 | + |
| 13 | + |
| 14 | +Custom node i18n demo: [comfyui-wiki/ComfyUI-i18n-demo](https://github.com/comfyui-wiki/ComfyUI-i18n-demo) |
| 15 | + |
| 16 | +## Directory Structure |
| 17 | + |
| 18 | +Create a `locales` folder under your custom node, which supports multiple types of translation files: |
| 19 | + |
| 20 | +```bash |
| 21 | +your_custom_node/ |
| 22 | +├── __init__.py |
| 23 | +├── your_node.py |
| 24 | +└── locales/ # i18n support folder |
| 25 | + ├── en/ # English translations (recommended as the base) |
| 26 | + │ ├── main.json # General English translation content |
| 27 | + │ ├── nodeDefs.json # English node definition translations |
| 28 | + │ ├── settings.json # Optional: settings interface translations |
| 29 | + │ └── commands.json # Optional: command translations |
| 30 | + ├── zh/ # Chinese translation files |
| 31 | + │ ├── nodeDefs.json # Chinese node definition translations |
| 32 | + │ ├── main.json # General Chinese translation content |
| 33 | + │ ├── settings.json # Chinese settings interface translations |
| 34 | + │ └── commands.json # Chinese command translations |
| 35 | + ├──... |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +## nodeDefs.json - Node Definition Translation |
| 40 | + |
| 41 | +For example, here is a Python definition example of an [i18n-demo](https://github.com/comfyui-wiki/ComfyUI-i18n-demo) node: |
| 42 | + |
| 43 | +```python |
| 44 | +class I18nTextProcessor: |
| 45 | + @classmethod |
| 46 | + def INPUT_TYPES(cls): |
| 47 | + return { |
| 48 | + "required": { |
| 49 | + "text": ("STRING", { |
| 50 | + "multiline": True, |
| 51 | + "default": "Hello World!", |
| 52 | + "tooltip": "The original text content to be processed" |
| 53 | + }), |
| 54 | + "operation": (["uppercase", "lowercase", "reverse", "add_prefix"], { |
| 55 | + "default": "uppercase", |
| 56 | + "tooltip": "The text processing operation to be executed" |
| 57 | + }), |
| 58 | + "count": ("INT", { |
| 59 | + "default": 1, |
| 60 | + "min": 1, |
| 61 | + "max": 10, |
| 62 | + "step": 1, |
| 63 | + "tooltip": "The number of times to repeat the operation" |
| 64 | + }), |
| 65 | + }, |
| 66 | + "optional": { |
| 67 | + "prefix": ("STRING", { |
| 68 | + "default": "[I18N] ", |
| 69 | + "multiline": False, |
| 70 | + "tooltip": "The prefix to add to the text" |
| 71 | + }), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + RETURN_TYPES = ("STRING",) |
| 76 | + RETURN_NAMES = ("processed_text",) |
| 77 | + FUNCTION = "process_text" |
| 78 | + CATEGORY = "I18n Demo" |
| 79 | + DESCRIPTION = "A simple i18n demo node that demonstrates text processing with internationalization support" |
| 80 | + |
| 81 | + def process_text(self, text, operation, count, prefix=""): |
| 82 | + try: |
| 83 | + result = text |
| 84 | + |
| 85 | + for _ in range(count): |
| 86 | + if operation == "uppercase": |
| 87 | + result = result.upper() |
| 88 | + elif operation == "lowercase": |
| 89 | + result = result.lower() |
| 90 | + elif operation == "reverse": |
| 91 | + result = result[::-1] |
| 92 | + elif operation == "add_prefix": |
| 93 | + result = prefix + result |
| 94 | + |
| 95 | + return (result,) |
| 96 | + except Exception as e: |
| 97 | + print(f"I18nTextProcessor error: {e}") |
| 98 | + return (f"Error: {str(e)}",) |
| 99 | +``` |
| 100 | + |
| 101 | +Then the corresponding localized support nodeDefs.json file content should include: |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "I18nTextProcessor": { |
| 106 | + "display_name": "I18n Text Processor", |
| 107 | + "description": "A simple i18n demo node that demonstrates text processing with internationalization support", |
| 108 | + "inputs": { |
| 109 | + "text": { |
| 110 | + "name": "Text Input", |
| 111 | + "tooltip": "The original text content to be processed" |
| 112 | + }, |
| 113 | + "operation": { |
| 114 | + "name": "Operation Type", |
| 115 | + "tooltip": "The text processing operation to be executed", |
| 116 | + "options": { |
| 117 | + "uppercase": "To Uppercase", |
| 118 | + "lowercase": "To Lowercase", |
| 119 | + "reverse": "Reverse Text", |
| 120 | + "add_prefix": "Add Prefix" |
| 121 | + } |
| 122 | + }, |
| 123 | + "count": { |
| 124 | + "name": "Repeat Count", |
| 125 | + "tooltip": "The number of times to repeat the operation" |
| 126 | + }, |
| 127 | + "prefix": { |
| 128 | + "name": "Prefix Text", |
| 129 | + "tooltip": "The prefix to add to the text" |
| 130 | + } |
| 131 | + }, |
| 132 | + "outputs": { |
| 133 | + "0": { |
| 134 | + "name": "Processed Text", |
| 135 | + "tooltip": "The final processed text result" |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +For the node output part, the corresponding output index is used instead of the output name, for example, the first output should be `0`, the second output should be `1`, and so on. |
| 143 | + |
| 144 | +## Menu Settings |
| 145 | + |
| 146 | +For example, in the i18n-demo custom node, we registered the following two menu settings: |
| 147 | +```javascript |
| 148 | +app.registerExtension({ |
| 149 | + name: "I18nDemo", |
| 150 | + settings: [ |
| 151 | + { |
| 152 | + id: "I18nDemo.EnableDebugMode", |
| 153 | + category: ["I18nDemo","DebugMode"], // This matches the settingsCategories key in main.json |
| 154 | + name: "Enable Debug Mode", // Will be overridden by translation |
| 155 | + tooltip: "Show debug information in console for i18n demo nodes", // Will be overridden by translation |
| 156 | + type: "boolean", |
| 157 | + defaultValue: false, |
| 158 | + experimental: true, |
| 159 | + onChange: (value) => { |
| 160 | + console.log("I18n Demo:", value ? "Debug mode enabled" : "Debug mode disabled"); |
| 161 | + } |
| 162 | + }, |
| 163 | + { |
| 164 | + id: "I18nDemo.DefaultTextOperation", |
| 165 | + category: ["I18nDemo","DefaultTextOperation"], // This matches the settingsCategories key in main.json |
| 166 | + name: "Default Text Operation", // Will be overridden by translation |
| 167 | + tooltip: "Default operation for text processor node", // Will be overridden by translation |
| 168 | + type: "combo", |
| 169 | + options: ["uppercase", "lowercase", "reverse", "add_prefix"], |
| 170 | + defaultValue: "uppercase", |
| 171 | + experimental: true |
| 172 | + } |
| 173 | + ], |
| 174 | + }) |
| 175 | +``` |
| 176 | + |
| 177 | +If you need to add corresponding internationalization support, for the menu category, you need to add it in the `main.json` file: |
| 178 | + |
| 179 | +```json |
| 180 | +{ |
| 181 | + "settingsCategories": { |
| 182 | + "I18nDemo": "I18n Demo", |
| 183 | + "DebugMode": "Debug Mode", |
| 184 | + "DefaultTextOperation": "Default Text Operation" |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +For the translation of the corresponding setting items, it is recommended to update them separately in the `settings.json` file, for example: |
| 190 | + |
| 191 | +```json |
| 192 | +{ |
| 193 | + "I18nDemo_EnableDebugMode": { |
| 194 | + "name": "Enable Debug Mode", |
| 195 | + "tooltip": "Show debug information in console for i18n demo nodes" |
| 196 | + }, |
| 197 | + "I18nDemo_DefaultTextOperation": { |
| 198 | + "name": "Default Text Operation", |
| 199 | + "tooltip": "Default operation for text processor node", |
| 200 | + "options": { |
| 201 | + "uppercase": "Uppercase", |
| 202 | + "lowercase": "Lowercase", |
| 203 | + "reverse": "Reverse", |
| 204 | + "add_prefix": "Add Prefix" |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +Note that the name of the corresponding translation key should replace the `.` in the original id with `_`, for example: |
| 211 | +``` |
| 212 | +"I18nDemo.EnableDebugMode" -> "I18nDemo_EnableDebugMode" |
| 213 | +``` |
| 214 | + |
| 215 | +## Custom Frontend Component Localization Support |
| 216 | + |
| 217 | +[To be updated] |
0 commit comments