editor_property_name_processor.cpp 13 KB

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