| 1 | /**************************************************************************/ |
| 2 | /* editor_property_name_processor.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "editor_property_name_processor.h" |
| 32 | |
| 33 | #include "core/string/translation_server.h" |
| 34 | #include "editor/settings/editor_settings.h" |
| 35 | |
| 36 | EditorPropertyNameProcessor *EditorPropertyNameProcessor::singleton = nullptr; |
| 37 | |
| 38 | EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_default_inspector_style() { |
| 39 | if (!EditorSettings::get_singleton()) { |
| 40 | return STYLE_CAPITALIZED; |
| 41 | } |
| 42 | const Style style = (Style)EDITOR_GET("interface/inspector/default_property_name_style").operator int(); |
| 43 | if (style == STYLE_LOCALIZED && !is_localization_available()) { |
| 44 | return STYLE_CAPITALIZED; |
| 45 | } |
| 46 | return style; |
| 47 | } |
| 48 | |
| 49 | EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_settings_style() { |
| 50 | if (!EditorSettings::get_singleton()) { |
| 51 | return STYLE_LOCALIZED; |
| 52 | } |
| 53 | const bool translate = EDITOR_GET("interface/editor/localize_settings"); |
| 54 | return translate ? STYLE_LOCALIZED : STYLE_CAPITALIZED; |
| 55 | } |
| 56 | |
| 57 | EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_tooltip_style(Style p_style) { |
| 58 | return p_style == STYLE_LOCALIZED ? STYLE_CAPITALIZED : STYLE_LOCALIZED; |
| 59 | } |
| 60 | |
| 61 | bool EditorPropertyNameProcessor::is_localization_available() { |
| 62 | return EditorSettings::get_singleton() && EditorSettings::get_singleton()->get_language() != "en"; |
| 63 | } |
| 64 | |
| 65 | String EditorPropertyNameProcessor::_capitalize_name(const String &p_name) const { |
| 66 | HashMap<String, String>::ConstIterator cached = capitalize_string_cache.find(p_name); |
| 67 | if (cached) { |
| 68 | return cached->value; |
| 69 | } |
| 70 | |
| 71 | Vector<String> parts = p_name.split("_", false); |
| 72 | for (int i = 0; i < parts.size(); i++) { |
| 73 | // Articles/conjunctions/prepositions which should only be capitalized when not at beginning and end. |
| 74 | if (i > 0 && i + 1 < parts.size() && stop_words.has(parts[i])) { |
| 75 | continue; |
| 76 | } |
| 77 | HashMap<String, String>::ConstIterator remap = capitalize_string_remaps.find(parts[i]); |
| 78 | if (remap) { |
| 79 | parts.write[i] = remap->value; |
| 80 | } else { |
| 81 | parts.write[i] = parts[i].capitalize(); |
| 82 | } |
| 83 | } |
| 84 | const String capitalized = String(" ").join(parts); |
| 85 | |
| 86 | capitalize_string_cache[p_name] = capitalized; |
| 87 | return capitalized; |
| 88 | } |
| 89 | |
| 90 | StringName EditorPropertyNameProcessor::_get_context(const String &p_name, const String &p_property, const StringName &p_class) const { |
| 91 | if (p_property.is_empty() && p_class == StringName()) { |
| 92 | return StringName(); |
| 93 | } |
| 94 | const HashMap<String, StringName> *context_map = translation_contexts.getptr(p_name); |
| 95 | if (context_map == nullptr) { |
| 96 | return StringName(); |
| 97 | } |
| 98 | // It's expected that full property path is enough to distinguish between usages. |
| 99 | // In case a class name is needed, all usages should be prefixed with the class name. |
| 100 | const StringName *context = context_map->getptr(p_property); |
| 101 | if (context == nullptr && p_class != StringName()) { |
| 102 | context = context_map->getptr(String(p_class) + "::" + p_property); |
| 103 | } |
| 104 | if (context == nullptr) { |
| 105 | return StringName(); |
| 106 | } |
| 107 | return *context; |
| 108 | } |
| 109 | |
| 110 | String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_style, const String &p_property, const StringName &p_class) const { |
| 111 | switch (p_style) { |
| 112 | case STYLE_RAW: { |
| 113 | return p_name; |
| 114 | } break; |
| 115 | |
| 116 | case STYLE_CAPITALIZED: { |
| 117 | return _capitalize_name(p_name); |
| 118 | } break; |
| 119 | |
| 120 | case STYLE_LOCALIZED: { |
| 121 | const String capitalized = _capitalize_name(p_name); |
| 122 | if (TranslationServer::get_singleton()) { |
| 123 | return TranslationServer::get_singleton()->get_property_domain()->translate(capitalized, _get_context(p_name, p_property, p_class)); |
| 124 | } |
| 125 | return capitalized; |
| 126 | } break; |
| 127 | } |
| 128 | ERR_FAIL_V_MSG(p_name, "Unexpected property name style."); |
| 129 | } |
| 130 | |
| 131 | String EditorPropertyNameProcessor::translate_group_name(const String &p_name) const { |
| 132 | if (TranslationServer::get_singleton()) { |
| 133 | return TranslationServer::get_singleton()->get_property_domain()->translate(p_name, StringName()); |
| 134 | } |
| 135 | return p_name; |
| 136 | } |
| 137 | |
| 138 | EditorPropertyNameProcessor::EditorPropertyNameProcessor() { |
| 139 | ERR_FAIL_COND(singleton != nullptr); |
| 140 | singleton = this; |
| 141 | |
| 142 | // The following initialization is parsed by the l10n extraction script with a regex. |
| 143 | // The map name and value definition format should be kept synced with the regex. |
| 144 | // https://github.com/godotengine/godot-editor-l10n/blob/main/scripts/common.py |
| 145 | capitalize_string_remaps["2d"] = "2D"; |
| 146 | capitalize_string_remaps["3d"] = "3D"; |
| 147 | capitalize_string_remaps["4d"] = "4D"; |
| 148 | capitalize_string_remaps["aa"] = "AA"; |
| 149 | capitalize_string_remaps["aabb"] = "AABB"; |
| 150 | capitalize_string_remaps["adb"] = "ADB"; |
| 151 | capitalize_string_remaps["actool"] = "actool"; |
| 152 | capitalize_string_remaps["ao"] = "AO"; |
| 153 | capitalize_string_remaps["api"] = "API"; |
| 154 | capitalize_string_remaps["apk"] = "APK"; |
| 155 | capitalize_string_remaps["arm32"] = "arm32"; |
| 156 | capitalize_string_remaps["arm64"] = "arm64"; |
| 157 | capitalize_string_remaps["arm64-v8a"] = "arm64-v8a"; |
| 158 | capitalize_string_remaps["armeabi-v7a"] = "armeabi-v7a"; |
| 159 | capitalize_string_remaps["arvr"] = "ARVR"; |
| 160 | capitalize_string_remaps["astc"] = "ASTC"; |
| 161 | capitalize_string_remaps["bbcode"] = "BBCode"; |
| 162 | capitalize_string_remaps["bg"] = "BG"; |
| 163 | capitalize_string_remaps["bidi"] = "BiDi"; |
| 164 | capitalize_string_remaps["bp"] = "BP"; |
| 165 | capitalize_string_remaps["bpc"] = "BPC"; |
| 166 | capitalize_string_remaps["bpm"] = "BPM"; |
| 167 | capitalize_string_remaps["bptc"] = "BPTC"; |
| 168 | capitalize_string_remaps["bvh"] = "BVH"; |
| 169 | capitalize_string_remaps["ca"] = "CA"; |
| 170 | capitalize_string_remaps["ccdik"] = "CCDIK"; |
| 171 | capitalize_string_remaps["cd"] = "CD"; |
| 172 | capitalize_string_remaps["cpu"] = "CPU"; |
| 173 | capitalize_string_remaps["csg"] = "CSG"; |
| 174 | capitalize_string_remaps["d3d12"] = "D3D12"; |
| 175 | capitalize_string_remaps["db"] = "dB"; |
| 176 | capitalize_string_remaps["dof"] = "DoF"; |
| 177 | capitalize_string_remaps["dpi"] = "DPI"; |
| 178 | capitalize_string_remaps["dtls"] = "DTLS"; |
| 179 | capitalize_string_remaps["eol"] = "EOL"; |
| 180 | capitalize_string_remaps["erp"] = "ERP"; |
| 181 | capitalize_string_remaps["etc2"] = "ETC2"; |
| 182 | capitalize_string_remaps["fabrik"] = "FABRIK"; |
| 183 | capitalize_string_remaps["fbx"] = "FBX"; |
| 184 | capitalize_string_remaps["fbx2gltf"] = "FBX2glTF"; |
| 185 | capitalize_string_remaps["fft"] = "FFT"; |
| 186 | capitalize_string_remaps["fg"] = "FG"; |
| 187 | capitalize_string_remaps["filesystem"] = "FileSystem"; |
| 188 | capitalize_string_remaps["fov"] = "FOV"; |
| 189 | capitalize_string_remaps["fps"] = "FPS"; |
| 190 | capitalize_string_remaps["fs"] = "FS"; |
| 191 | capitalize_string_remaps["fsr"] = "FSR"; |
| 192 | capitalize_string_remaps["fxaa"] = "FXAA"; |
| 193 | capitalize_string_remaps["gdscript"] = "GDScript"; |
| 194 | capitalize_string_remaps["ggx"] = "GGX"; |
| 195 | capitalize_string_remaps["gi"] = "GI"; |
| 196 | capitalize_string_remaps["gl"] = "GL"; |
| 197 | capitalize_string_remaps["glb"] = "GLB"; |
| 198 | capitalize_string_remaps["gles"] = "GLES"; |
| 199 | capitalize_string_remaps["gles2"] = "GLES2"; |
| 200 | capitalize_string_remaps["gles3"] = "GLES3"; |
| 201 | capitalize_string_remaps["gltf"] = "glTF"; |
| 202 | capitalize_string_remaps["gridmap"] = "GridMap"; |
| 203 | capitalize_string_remaps["gpu"] = "GPU"; |
| 204 | capitalize_string_remaps["gui"] = "GUI"; |
| 205 | capitalize_string_remaps["guid"] = "GUID"; |
| 206 | capitalize_string_remaps["hdr"] = "HDR"; |
| 207 | capitalize_string_remaps["hidpi"] = "hiDPI"; |
| 208 | capitalize_string_remaps["hipass"] = "High-pass"; |
| 209 | capitalize_string_remaps["hl"] = "HL"; |
| 210 | capitalize_string_remaps["hsv"] = "HSV"; |
| 211 | capitalize_string_remaps["html"] = "HTML"; |
| 212 | capitalize_string_remaps["http"] = "HTTP"; |
| 213 | capitalize_string_remaps["id"] = "ID"; |
| 214 | capitalize_string_remaps["ids"] = "IDs"; |
| 215 | capitalize_string_remaps["igd"] = "IGD"; |
| 216 | capitalize_string_remaps["ik"] = "IK"; |
| 217 | capitalize_string_remaps["image@2x"] = "Image @2x"; |
| 218 | capitalize_string_remaps["image@3x"] = "Image @3x"; |
| 219 | capitalize_string_remaps["ime"] = "IME"; |
| 220 | capitalize_string_remaps["iod"] = "IOD"; |
| 221 | capitalize_string_remaps["ios"] = "iOS"; |
| 222 | capitalize_string_remaps["ip"] = "IP"; |
| 223 | capitalize_string_remaps["ipad"] = "iPad"; |
| 224 | capitalize_string_remaps["iphone"] = "iPhone"; |
| 225 | capitalize_string_remaps["ipv6"] = "IPv6"; |
| 226 | capitalize_string_remaps["ir"] = "IR"; |
| 227 | capitalize_string_remaps["itunes"] = "iTunes"; |
| 228 | capitalize_string_remaps["jit"] = "JIT"; |
| 229 | capitalize_string_remaps["k1"] = "K1"; |
| 230 | capitalize_string_remaps["k2"] = "K2"; |
| 231 | capitalize_string_remaps["kb"] = "(KB)"; // Unit. |
| 232 | capitalize_string_remaps["lcd"] = "LCD"; |
| 233 | capitalize_string_remaps["ldr"] = "LDR"; |
| 234 | capitalize_string_remaps["linuxbsd"] = "Linux/*BSD"; |
| 235 | capitalize_string_remaps["lod"] = "LOD"; |
| 236 | capitalize_string_remaps["lods"] = "LODs"; |
| 237 | capitalize_string_remaps["loongarch64"] = "loongarch64"; |
| 238 | capitalize_string_remaps["lowpass"] = "Low-pass"; |
| 239 | capitalize_string_remaps["macos"] = "macOS"; |
| 240 | capitalize_string_remaps["mb"] = "(MB)"; // Unit. |
| 241 | capitalize_string_remaps["mjpeg"] = "MJPEG"; |
| 242 | capitalize_string_remaps["mms"] = "MMS"; |
| 243 | capitalize_string_remaps["ms"] = "(ms)"; // Unit |
| 244 | capitalize_string_remaps["msaa"] = "MSAA"; |
| 245 | capitalize_string_remaps["msdf"] = "MSDF"; |
| 246 | // Not used for now as AudioEffectReverb has a `msec` property. |
| 247 | //capitalize_string_remaps["msec"] = "(msec)"; // Unit. |
| 248 | capitalize_string_remaps["navmesh"] = "NavMesh"; |
| 249 | capitalize_string_remaps["nfc"] = "NFC"; |
| 250 | capitalize_string_remaps["ogv"] = "OGV"; |
| 251 | capitalize_string_remaps["oidn"] = "OIDN"; |
| 252 | capitalize_string_remaps["ok"] = "OK"; |
| 253 | capitalize_string_remaps["opengl"] = "OpenGL"; |
| 254 | capitalize_string_remaps["opengl3"] = "OpenGL 3"; |
| 255 | capitalize_string_remaps["opentype"] = "OpenType"; |
| 256 | capitalize_string_remaps["openxr"] = "OpenXR"; |
| 257 | capitalize_string_remaps["osslsigncode"] = "osslsigncode"; |
| 258 | capitalize_string_remaps["path3d"] = "Path3D"; |
| 259 | capitalize_string_remaps["pck"] = "PCK"; |
| 260 | capitalize_string_remaps["png"] = "PNG"; |
| 261 | capitalize_string_remaps["po2"] = "(Power of 2)"; // Unit. |
| 262 | capitalize_string_remaps["ppc64"] = "ppc64"; |
| 263 | capitalize_string_remaps["pvrtc"] = "PVRTC"; |
| 264 | capitalize_string_remaps["pvs"] = "PVS"; |
| 265 | capitalize_string_remaps["rcodesign"] = "rcodesign"; |
| 266 | capitalize_string_remaps["rdo"] = "RDO"; |
| 267 | capitalize_string_remaps["rgb"] = "RGB"; |
| 268 | capitalize_string_remaps["rid"] = "RID"; |
| 269 | capitalize_string_remaps["rmb"] = "RMB"; |
| 270 | capitalize_string_remaps["rpc"] = "RPC"; |
| 271 | capitalize_string_remaps["rv64"] = "rv64"; |
| 272 | capitalize_string_remaps["s3tc"] = "S3TC"; |
| 273 | capitalize_string_remaps["scp"] = "SCP"; |
| 274 | capitalize_string_remaps["scrcpy"] = "scrcpy"; |
| 275 | capitalize_string_remaps["sdf"] = "SDF"; |
| 276 | capitalize_string_remaps["sdfgi"] = "SDFGI"; |
| 277 | capitalize_string_remaps["sdk"] = "SDK"; |
| 278 | capitalize_string_remaps["sec"] = "(sec)"; // Unit. |
| 279 | capitalize_string_remaps["signtool"] = "signtool"; |
| 280 | capitalize_string_remaps["smaa"] = "SMAA"; |
| 281 | capitalize_string_remaps["sms"] = "SMS"; |
| 282 | capitalize_string_remaps["srgb"] = "sRGB"; |
| 283 | capitalize_string_remaps["ssao"] = "SSAO"; |
| 284 | capitalize_string_remaps["ssh"] = "SSH"; |
| 285 | capitalize_string_remaps["ssil"] = "SSIL"; |
| 286 | capitalize_string_remaps["ssl"] = "SSL"; |
| 287 | capitalize_string_remaps["sss"] = "SSS"; |
| 288 | capitalize_string_remaps["stderr"] = "stderr"; |
| 289 | capitalize_string_remaps["stdout"] = "stdout"; |
| 290 | capitalize_string_remaps["sv"] = "SV"; |
| 291 | capitalize_string_remaps["svg"] = "SVG"; |
| 292 | capitalize_string_remaps["taa"] = "TAA"; |
| 293 | capitalize_string_remaps["tcp"] = "TCP"; |
| 294 | capitalize_string_remaps["textfile"] = "TextFile"; |
| 295 | capitalize_string_remaps["tls"] = "TLS"; |
| 296 | capitalize_string_remaps["tv"] = "TV"; |
| 297 | capitalize_string_remaps["tvos"] = "tvOS"; |
| 298 | capitalize_string_remaps["uastc"] = "UASTC"; |
| 299 | capitalize_string_remaps["ui"] = "UI"; |
| 300 | capitalize_string_remaps["uri"] = "URI"; |
| 301 | capitalize_string_remaps["url"] = "URL"; |
| 302 | capitalize_string_remaps["urls"] = "URLs"; |
| 303 | capitalize_string_remaps["us"] = U"(µs)"; // Unit. |
| 304 | capitalize_string_remaps["usb"] = "USB"; |
| 305 | capitalize_string_remaps["usec"] = U"(µsec)"; // Unit. |
| 306 | capitalize_string_remaps["uid"] = "UID"; |
| 307 | capitalize_string_remaps["uuid"] = "UUID"; |
| 308 | capitalize_string_remaps["uv"] = "UV"; |
| 309 | capitalize_string_remaps["uv1"] = "UV1"; |
| 310 | capitalize_string_remaps["uv2"] = "UV2"; |
| 311 | capitalize_string_remaps["vector2"] = "Vector2"; |
| 312 | capitalize_string_remaps["visionos"] = "visionOS"; |
| 313 | capitalize_string_remaps["vpn"] = "VPN"; |
| 314 | capitalize_string_remaps["vram"] = "VRAM"; |
| 315 | capitalize_string_remaps["vrs"] = "VRS"; |
| 316 | capitalize_string_remaps["vsync"] = "V-Sync"; |
| 317 | capitalize_string_remaps["wap"] = "WAP"; |
| 318 | capitalize_string_remaps["webp"] = "WebP"; |
| 319 | capitalize_string_remaps["webrtc"] = "WebRTC"; |
| 320 | capitalize_string_remaps["websocket"] = "WebSocket"; |
| 321 | capitalize_string_remaps["wintab"] = "WinTab"; |
| 322 | capitalize_string_remaps["winink"] = "Windows Ink"; |
| 323 | capitalize_string_remaps["wifi"] = "Wi-Fi"; |
| 324 | capitalize_string_remaps["x86"] = "x86"; |
| 325 | capitalize_string_remaps["x86_32"] = "x86_32"; |
| 326 | capitalize_string_remaps["x86_64"] = "x86_64"; |
| 327 | capitalize_string_remaps["xr"] = "XR"; |
| 328 | capitalize_string_remaps["xray"] = "X-Ray"; |
| 329 | capitalize_string_remaps["xy"] = "XY"; |
| 330 | capitalize_string_remaps["xz"] = "XZ"; |
| 331 | capitalize_string_remaps["yz"] = "YZ"; |
| 332 | |
| 333 | // Articles, conjunctions, prepositions. |
| 334 | // The following initialization is parsed in https://github.com/godotengine/godot-editor-l10n/blob/main/scripts/common.py |
| 335 | // with a regex. The word definition format should be kept synced with the regex. |
| 336 | stop_words = LocalVector<String>({ |
| 337 | "a", |
| 338 | "an", |
| 339 | "and", |
| 340 | "as", |
| 341 | "at", |
| 342 | "by", |
| 343 | "for", |
| 344 | "in", |
| 345 | "not", |
| 346 | "of", |
| 347 | "on", |
| 348 | "or", |
| 349 | "over", |
| 350 | "per", |
| 351 | "the", |
| 352 | "then", |
| 353 | "to", |
| 354 | "with", |
| 355 | }); |
| 356 | |
| 357 | // Translation context associated with a name. |
| 358 | // The second key is either: |
| 359 | // - `full/property/path` |
| 360 | // - `Class::full/property/path` |
| 361 | // In case a class name is needed to distinguish between usages, all usages should use the second format. |
| 362 | // |
| 363 | // The following initialization is parsed in https://github.com/godotengine/godot-editor-l10n/blob/main/scripts/common.py |
| 364 | // with a regex. The map name and value definition format should be kept synced with the regex. |
| 365 | translation_contexts["force"]["constant_force"] = "Physics"; |
| 366 | translation_contexts["force"]["force/8_bit"] = "Enforce"; |
| 367 | translation_contexts["force"]["force/mono"] = "Enforce"; |
| 368 | translation_contexts["force"]["force/max_rate"] = "Enforce"; |
| 369 | translation_contexts["force"]["force/max_rate_hz"] = "Enforce"; |
| 370 | translation_contexts["normal"]["theme_override_styles/normal"] = "Ordinary"; |
| 371 | translation_contexts["normal"]["TextureButton::texture_normal"] = "Ordinary"; |
| 372 | translation_contexts["normal"]["Decal::texture_normal"] = "Geometry"; |
| 373 | translation_contexts["normal"]["detail_normal"] = "Geometry"; |
| 374 | translation_contexts["normal"]["normal"] = "Geometry"; |
| 375 | } |
| 376 | |
| 377 | EditorPropertyNameProcessor::~EditorPropertyNameProcessor() { |
| 378 | singleton = nullptr; |
| 379 | } |