editor_property_name_processor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #include "editor_property_name_processor.h"
  31. #include "core/string/translation.h"
  32. #include "editor_settings.h"
  33. EditorPropertyNameProcessor *EditorPropertyNameProcessor::singleton = nullptr;
  34. EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_default_inspector_style() {
  35. if (!EditorSettings::get_singleton()) {
  36. return STYLE_CAPITALIZED;
  37. }
  38. const Style style = (Style)EDITOR_GET("interface/inspector/default_property_name_style").operator int();
  39. if (style == STYLE_LOCALIZED && !is_localization_available()) {
  40. return STYLE_CAPITALIZED;
  41. }
  42. return style;
  43. }
  44. EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_settings_style() {
  45. if (!EditorSettings::get_singleton()) {
  46. return STYLE_LOCALIZED;
  47. }
  48. const bool translate = EDITOR_GET("interface/editor/localize_settings");
  49. return translate ? STYLE_LOCALIZED : STYLE_CAPITALIZED;
  50. }
  51. EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_tooltip_style(Style p_style) {
  52. return p_style == STYLE_LOCALIZED ? STYLE_CAPITALIZED : STYLE_LOCALIZED;
  53. }
  54. bool EditorPropertyNameProcessor::is_localization_available() {
  55. if (!EditorSettings::get_singleton()) {
  56. return false;
  57. }
  58. const Vector<String> forbidden = String("en").split(",");
  59. return forbidden.find(EDITOR_GET("interface/editor/editor_language")) == -1;
  60. }
  61. String EditorPropertyNameProcessor::_capitalize_name(const String &p_name) const {
  62. HashMap<String, String>::ConstIterator cached = capitalize_string_cache.find(p_name);
  63. if (cached) {
  64. return cached->value;
  65. }
  66. Vector<String> parts = p_name.split("_", false);
  67. for (int i = 0; i < parts.size(); i++) {
  68. // Articles/conjunctions/prepositions which should only be capitalized when not at beginning and end.
  69. if (i > 0 && i + 1 < parts.size() && stop_words.find(parts[i]) != -1) {
  70. continue;
  71. }
  72. HashMap<String, String>::ConstIterator remap = capitalize_string_remaps.find(parts[i]);
  73. if (remap) {
  74. parts.write[i] = remap->value;
  75. } else {
  76. parts.write[i] = parts[i].capitalize();
  77. }
  78. }
  79. const String capitalized = String(" ").join(parts);
  80. capitalize_string_cache[p_name] = capitalized;
  81. return capitalized;
  82. }
  83. String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_style) const {
  84. switch (p_style) {
  85. case STYLE_RAW: {
  86. return p_name;
  87. } break;
  88. case STYLE_CAPITALIZED: {
  89. return _capitalize_name(p_name);
  90. } break;
  91. case STYLE_LOCALIZED: {
  92. const String capitalized = _capitalize_name(p_name);
  93. if (TranslationServer::get_singleton()) {
  94. return TranslationServer::get_singleton()->property_translate(capitalized);
  95. }
  96. return capitalized;
  97. } break;
  98. }
  99. ERR_FAIL_V_MSG(p_name, "Unexpected property name style.");
  100. }
  101. String EditorPropertyNameProcessor::translate_group_name(const String &p_name) const {
  102. if (TranslationServer::get_singleton()) {
  103. return TranslationServer::get_singleton()->property_translate(p_name);
  104. }
  105. return p_name;
  106. }
  107. EditorPropertyNameProcessor::EditorPropertyNameProcessor() {
  108. ERR_FAIL_COND(singleton != nullptr);
  109. singleton = this;
  110. // The following initialization is parsed by the l10n extraction script with a regex.
  111. // The map name and value definition format should be kept synced with the regex.
  112. // https://github.com/godotengine/godot-editor-l10n/blob/main/scripts/common.py
  113. capitalize_string_remaps["2d"] = "2D";
  114. capitalize_string_remaps["3d"] = "3D";
  115. capitalize_string_remaps["aa"] = "AA";
  116. capitalize_string_remaps["aabb"] = "AABB";
  117. capitalize_string_remaps["adb"] = "ADB";
  118. capitalize_string_remaps["ao"] = "AO";
  119. capitalize_string_remaps["api"] = "API";
  120. capitalize_string_remaps["apk"] = "APK";
  121. capitalize_string_remaps["arm32"] = "arm32";
  122. capitalize_string_remaps["arm64"] = "arm64";
  123. capitalize_string_remaps["arm64-v8a"] = "arm64-v8a";
  124. capitalize_string_remaps["armeabi-v7a"] = "armeabi-v7a";
  125. capitalize_string_remaps["arvr"] = "ARVR";
  126. capitalize_string_remaps["astc"] = "ASTC";
  127. capitalize_string_remaps["bbcode"] = "BBCode";
  128. capitalize_string_remaps["bg"] = "BG";
  129. capitalize_string_remaps["bidi"] = "BiDi";
  130. capitalize_string_remaps["bp"] = "BP";
  131. capitalize_string_remaps["bpc"] = "BPC";
  132. capitalize_string_remaps["bpm"] = "BPM";
  133. capitalize_string_remaps["bptc"] = "BPTC";
  134. capitalize_string_remaps["bvh"] = "BVH";
  135. capitalize_string_remaps["ca"] = "CA";
  136. capitalize_string_remaps["ccdik"] = "CCDIK";
  137. capitalize_string_remaps["cd"] = "CD";
  138. capitalize_string_remaps["cpu"] = "CPU";
  139. capitalize_string_remaps["csg"] = "CSG";
  140. capitalize_string_remaps["db"] = "dB";
  141. capitalize_string_remaps["dof"] = "DoF";
  142. capitalize_string_remaps["dpi"] = "DPI";
  143. capitalize_string_remaps["dtls"] = "DTLS";
  144. capitalize_string_remaps["eol"] = "EOL";
  145. capitalize_string_remaps["erp"] = "ERP";
  146. capitalize_string_remaps["etc"] = "ETC";
  147. capitalize_string_remaps["etc2"] = "ETC2";
  148. capitalize_string_remaps["fabrik"] = "FABRIK";
  149. capitalize_string_remaps["fbx"] = "FBX";
  150. capitalize_string_remaps["fbx2gltf"] = "FBX2glTF";
  151. capitalize_string_remaps["fft"] = "FFT";
  152. capitalize_string_remaps["fg"] = "FG";
  153. capitalize_string_remaps["filesystem"] = "FileSystem";
  154. capitalize_string_remaps["fov"] = "FOV";
  155. capitalize_string_remaps["fps"] = "FPS";
  156. capitalize_string_remaps["fs"] = "FS";
  157. capitalize_string_remaps["fsr"] = "FSR";
  158. capitalize_string_remaps["fxaa"] = "FXAA";
  159. capitalize_string_remaps["gdscript"] = "GDScript";
  160. capitalize_string_remaps["ggx"] = "GGX";
  161. capitalize_string_remaps["gi"] = "GI";
  162. capitalize_string_remaps["gl"] = "GL";
  163. capitalize_string_remaps["glb"] = "GLB";
  164. capitalize_string_remaps["gles2"] = "GLES2";
  165. capitalize_string_remaps["gles3"] = "GLES3";
  166. capitalize_string_remaps["gltf"] = "glTF";
  167. capitalize_string_remaps["gpu"] = "GPU";
  168. capitalize_string_remaps["gui"] = "GUI";
  169. capitalize_string_remaps["guid"] = "GUID";
  170. capitalize_string_remaps["hdr"] = "HDR";
  171. capitalize_string_remaps["hidpi"] = "hiDPI";
  172. capitalize_string_remaps["hipass"] = "High-pass";
  173. capitalize_string_remaps["hl"] = "HL";
  174. capitalize_string_remaps["hsv"] = "HSV";
  175. capitalize_string_remaps["html"] = "HTML";
  176. capitalize_string_remaps["http"] = "HTTP";
  177. capitalize_string_remaps["id"] = "ID";
  178. capitalize_string_remaps["ids"] = "IDs";
  179. capitalize_string_remaps["igd"] = "IGD";
  180. capitalize_string_remaps["ik"] = "IK";
  181. capitalize_string_remaps["image@2x"] = "Image @2x";
  182. capitalize_string_remaps["image@3x"] = "Image @3x";
  183. capitalize_string_remaps["iod"] = "IOD";
  184. capitalize_string_remaps["ios"] = "iOS";
  185. capitalize_string_remaps["ip"] = "IP";
  186. capitalize_string_remaps["ipad"] = "iPad";
  187. capitalize_string_remaps["iphone"] = "iPhone";
  188. capitalize_string_remaps["ipv6"] = "IPv6";
  189. capitalize_string_remaps["ir"] = "IR";
  190. capitalize_string_remaps["itunes"] = "iTunes";
  191. capitalize_string_remaps["jit"] = "JIT";
  192. capitalize_string_remaps["k1"] = "K1";
  193. capitalize_string_remaps["k2"] = "K2";
  194. capitalize_string_remaps["kb"] = "(KB)"; // Unit.
  195. capitalize_string_remaps["lcd"] = "LCD";
  196. capitalize_string_remaps["ldr"] = "LDR";
  197. capitalize_string_remaps["lod"] = "LOD";
  198. capitalize_string_remaps["lods"] = "LODs";
  199. capitalize_string_remaps["lowpass"] = "Low-pass";
  200. capitalize_string_remaps["macos"] = "macOS";
  201. capitalize_string_remaps["mb"] = "(MB)"; // Unit.
  202. capitalize_string_remaps["mjpeg"] = "MJPEG";
  203. capitalize_string_remaps["mms"] = "MMS";
  204. capitalize_string_remaps["ms"] = "(ms)"; // Unit
  205. capitalize_string_remaps["msaa"] = "MSAA";
  206. capitalize_string_remaps["msdf"] = "MSDF";
  207. // Not used for now as AudioEffectReverb has a `msec` property.
  208. //capitalize_string_remaps["msec"] = "(msec)"; // Unit.
  209. capitalize_string_remaps["navmesh"] = "NavMesh";
  210. capitalize_string_remaps["nfc"] = "NFC";
  211. capitalize_string_remaps["ok"] = "OK";
  212. capitalize_string_remaps["opengl"] = "OpenGL";
  213. capitalize_string_remaps["opentype"] = "OpenType";
  214. capitalize_string_remaps["openxr"] = "OpenXR";
  215. capitalize_string_remaps["osslsigncode"] = "osslsigncode";
  216. capitalize_string_remaps["pck"] = "PCK";
  217. capitalize_string_remaps["png"] = "PNG";
  218. capitalize_string_remaps["po2"] = "(Power of 2)"; // Unit.
  219. capitalize_string_remaps["ppc32"] = "ppc32";
  220. capitalize_string_remaps["ppc64"] = "ppc64";
  221. capitalize_string_remaps["pvrtc"] = "PVRTC";
  222. capitalize_string_remaps["pvs"] = "PVS";
  223. capitalize_string_remaps["rcedit"] = "rcedit";
  224. capitalize_string_remaps["rcodesign"] = "rcodesign";
  225. capitalize_string_remaps["rgb"] = "RGB";
  226. capitalize_string_remaps["rid"] = "RID";
  227. capitalize_string_remaps["rmb"] = "RMB";
  228. capitalize_string_remaps["rpc"] = "RPC";
  229. capitalize_string_remaps["rv64"] = "rv64";
  230. capitalize_string_remaps["s3tc"] = "S3TC";
  231. capitalize_string_remaps["scp"] = "SCP";
  232. capitalize_string_remaps["sdf"] = "SDF";
  233. capitalize_string_remaps["sdfgi"] = "SDFGI";
  234. capitalize_string_remaps["sdk"] = "SDK";
  235. capitalize_string_remaps["sec"] = "(sec)"; // Unit.
  236. capitalize_string_remaps["signtool"] = "signtool";
  237. capitalize_string_remaps["sms"] = "SMS";
  238. capitalize_string_remaps["srgb"] = "sRGB";
  239. capitalize_string_remaps["ssao"] = "SSAO";
  240. capitalize_string_remaps["ssh"] = "SSH";
  241. capitalize_string_remaps["ssil"] = "SSIL";
  242. capitalize_string_remaps["ssl"] = "SSL";
  243. capitalize_string_remaps["sss"] = "SSS";
  244. capitalize_string_remaps["stderr"] = "stderr";
  245. capitalize_string_remaps["stdout"] = "stdout";
  246. capitalize_string_remaps["sv"] = "SV";
  247. capitalize_string_remaps["svg"] = "SVG";
  248. capitalize_string_remaps["taa"] = "TAA";
  249. capitalize_string_remaps["tcp"] = "TCP";
  250. capitalize_string_remaps["textfile"] = "TextFile";
  251. capitalize_string_remaps["tls"] = "TLS";
  252. capitalize_string_remaps["ui"] = "UI";
  253. capitalize_string_remaps["uri"] = "URI";
  254. capitalize_string_remaps["url"] = "URL";
  255. capitalize_string_remaps["urls"] = "URLs";
  256. capitalize_string_remaps["us"] = String::utf8("(µs)"); // Unit.
  257. capitalize_string_remaps["usb"] = "USB";
  258. capitalize_string_remaps["usec"] = String::utf8("(µsec)"); // Unit.
  259. capitalize_string_remaps["uuid"] = "UUID";
  260. capitalize_string_remaps["uv"] = "UV";
  261. capitalize_string_remaps["uv1"] = "UV1";
  262. capitalize_string_remaps["uv2"] = "UV2";
  263. capitalize_string_remaps["uwp"] = "UWP";
  264. capitalize_string_remaps["vector2"] = "Vector2";
  265. capitalize_string_remaps["vpn"] = "VPN";
  266. capitalize_string_remaps["vram"] = "VRAM";
  267. capitalize_string_remaps["vrs"] = "VRS";
  268. capitalize_string_remaps["vsync"] = "V-Sync";
  269. capitalize_string_remaps["wap"] = "WAP";
  270. capitalize_string_remaps["webp"] = "WebP";
  271. capitalize_string_remaps["webrtc"] = "WebRTC";
  272. capitalize_string_remaps["websocket"] = "WebSocket";
  273. capitalize_string_remaps["wine"] = "wine";
  274. capitalize_string_remaps["wifi"] = "Wi-Fi";
  275. capitalize_string_remaps["x86"] = "x86";
  276. capitalize_string_remaps["x86_32"] = "x86_32";
  277. capitalize_string_remaps["x86_64"] = "x86_64";
  278. capitalize_string_remaps["xr"] = "XR";
  279. capitalize_string_remaps["xray"] = "X-Ray";
  280. capitalize_string_remaps["xy"] = "XY";
  281. capitalize_string_remaps["xz"] = "XZ";
  282. capitalize_string_remaps["yz"] = "YZ";
  283. // Articles, conjunctions, prepositions.
  284. // The following initialization is parsed in `editor/translations/scripts/common.py` with a regex.
  285. // The word definition format should be kept synced with the regex.
  286. stop_words = LocalVector<String>({
  287. "a",
  288. "an",
  289. "and",
  290. "as",
  291. "at",
  292. "by",
  293. "for",
  294. "in",
  295. "not",
  296. "of",
  297. "on",
  298. "or",
  299. "over",
  300. "per",
  301. "the",
  302. "then",
  303. "to",
  304. });
  305. }
  306. EditorPropertyNameProcessor::~EditorPropertyNameProcessor() {
  307. singleton = nullptr;
  308. }