editor_property_name_processor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_server.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.has(EDITOR_GET("interface/editor/editor_language"));
  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.has(parts[i])) {
  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. StringName EditorPropertyNameProcessor::_get_context(const String &p_name, const String &p_property, const StringName &p_class) const {
  84. if (p_property.is_empty() && p_class == StringName()) {
  85. return StringName();
  86. }
  87. const HashMap<String, StringName> *context_map = translation_contexts.getptr(p_name);
  88. if (context_map == nullptr) {
  89. return StringName();
  90. }
  91. // It's expected that full property path is enough to distinguish between usages.
  92. // In case a class name is needed, all usages should be prefixed with the class name.
  93. const StringName *context = context_map->getptr(p_property);
  94. if (context == nullptr && p_class != StringName()) {
  95. context = context_map->getptr(String(p_class) + "::" + p_property);
  96. }
  97. if (context == nullptr) {
  98. return StringName();
  99. }
  100. return *context;
  101. }
  102. String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_style, const String &p_property, const StringName &p_class) const {
  103. switch (p_style) {
  104. case STYLE_RAW: {
  105. return p_name;
  106. } break;
  107. case STYLE_CAPITALIZED: {
  108. return _capitalize_name(p_name);
  109. } break;
  110. case STYLE_LOCALIZED: {
  111. const String capitalized = _capitalize_name(p_name);
  112. if (TranslationServer::get_singleton()) {
  113. return TranslationServer::get_singleton()->property_translate(capitalized, _get_context(p_name, p_property, p_class));
  114. }
  115. return capitalized;
  116. } break;
  117. }
  118. ERR_FAIL_V_MSG(p_name, "Unexpected property name style.");
  119. }
  120. String EditorPropertyNameProcessor::translate_group_name(const String &p_name) const {
  121. if (TranslationServer::get_singleton()) {
  122. return TranslationServer::get_singleton()->property_translate(p_name);
  123. }
  124. return p_name;
  125. }
  126. EditorPropertyNameProcessor::EditorPropertyNameProcessor() {
  127. ERR_FAIL_COND(singleton != nullptr);
  128. singleton = this;
  129. // The following initialization is parsed by the l10n extraction script with a regex.
  130. // The map name and value definition format should be kept synced with the regex.
  131. // https://github.com/godotengine/godot-editor-l10n/blob/main/scripts/common.py
  132. capitalize_string_remaps["2d"] = "2D";
  133. capitalize_string_remaps["3d"] = "3D";
  134. capitalize_string_remaps["4d"] = "4D";
  135. capitalize_string_remaps["aa"] = "AA";
  136. capitalize_string_remaps["aabb"] = "AABB";
  137. capitalize_string_remaps["adb"] = "ADB";
  138. capitalize_string_remaps["ao"] = "AO";
  139. capitalize_string_remaps["api"] = "API";
  140. capitalize_string_remaps["apk"] = "APK";
  141. capitalize_string_remaps["arm32"] = "arm32";
  142. capitalize_string_remaps["arm64"] = "arm64";
  143. capitalize_string_remaps["arm64-v8a"] = "arm64-v8a";
  144. capitalize_string_remaps["armeabi-v7a"] = "armeabi-v7a";
  145. capitalize_string_remaps["arvr"] = "ARVR";
  146. capitalize_string_remaps["astc"] = "ASTC";
  147. capitalize_string_remaps["bbcode"] = "BBCode";
  148. capitalize_string_remaps["bg"] = "BG";
  149. capitalize_string_remaps["bidi"] = "BiDi";
  150. capitalize_string_remaps["bp"] = "BP";
  151. capitalize_string_remaps["bpc"] = "BPC";
  152. capitalize_string_remaps["bpm"] = "BPM";
  153. capitalize_string_remaps["bptc"] = "BPTC";
  154. capitalize_string_remaps["bvh"] = "BVH";
  155. capitalize_string_remaps["ca"] = "CA";
  156. capitalize_string_remaps["ccdik"] = "CCDIK";
  157. capitalize_string_remaps["cd"] = "CD";
  158. capitalize_string_remaps["cpu"] = "CPU";
  159. capitalize_string_remaps["csg"] = "CSG";
  160. capitalize_string_remaps["d3d12"] = "D3D12";
  161. capitalize_string_remaps["db"] = "dB";
  162. capitalize_string_remaps["dof"] = "DoF";
  163. capitalize_string_remaps["dpi"] = "DPI";
  164. capitalize_string_remaps["dtls"] = "DTLS";
  165. capitalize_string_remaps["eol"] = "EOL";
  166. capitalize_string_remaps["erp"] = "ERP";
  167. capitalize_string_remaps["etc2"] = "ETC2";
  168. capitalize_string_remaps["fabrik"] = "FABRIK";
  169. capitalize_string_remaps["fbx"] = "FBX";
  170. capitalize_string_remaps["fbx2gltf"] = "FBX2glTF";
  171. capitalize_string_remaps["fft"] = "FFT";
  172. capitalize_string_remaps["fg"] = "FG";
  173. capitalize_string_remaps["filesystem"] = "FileSystem";
  174. capitalize_string_remaps["fov"] = "FOV";
  175. capitalize_string_remaps["fps"] = "FPS";
  176. capitalize_string_remaps["fs"] = "FS";
  177. capitalize_string_remaps["fsr"] = "FSR";
  178. capitalize_string_remaps["fxaa"] = "FXAA";
  179. capitalize_string_remaps["gdscript"] = "GDScript";
  180. capitalize_string_remaps["ggx"] = "GGX";
  181. capitalize_string_remaps["gi"] = "GI";
  182. capitalize_string_remaps["gl"] = "GL";
  183. capitalize_string_remaps["glb"] = "GLB";
  184. capitalize_string_remaps["gles"] = "GLES";
  185. capitalize_string_remaps["gles2"] = "GLES2";
  186. capitalize_string_remaps["gles3"] = "GLES3";
  187. capitalize_string_remaps["gltf"] = "glTF";
  188. capitalize_string_remaps["gpu"] = "GPU";
  189. capitalize_string_remaps["gui"] = "GUI";
  190. capitalize_string_remaps["guid"] = "GUID";
  191. capitalize_string_remaps["hdr"] = "HDR";
  192. capitalize_string_remaps["hidpi"] = "hiDPI";
  193. capitalize_string_remaps["hipass"] = "High-pass";
  194. capitalize_string_remaps["hl"] = "HL";
  195. capitalize_string_remaps["hsv"] = "HSV";
  196. capitalize_string_remaps["html"] = "HTML";
  197. capitalize_string_remaps["http"] = "HTTP";
  198. capitalize_string_remaps["id"] = "ID";
  199. capitalize_string_remaps["ids"] = "IDs";
  200. capitalize_string_remaps["igd"] = "IGD";
  201. capitalize_string_remaps["ik"] = "IK";
  202. capitalize_string_remaps["image@2x"] = "Image @2x";
  203. capitalize_string_remaps["image@3x"] = "Image @3x";
  204. capitalize_string_remaps["iod"] = "IOD";
  205. capitalize_string_remaps["ios"] = "iOS";
  206. capitalize_string_remaps["ip"] = "IP";
  207. capitalize_string_remaps["ipad"] = "iPad";
  208. capitalize_string_remaps["iphone"] = "iPhone";
  209. capitalize_string_remaps["ipv6"] = "IPv6";
  210. capitalize_string_remaps["ir"] = "IR";
  211. capitalize_string_remaps["itunes"] = "iTunes";
  212. capitalize_string_remaps["jit"] = "JIT";
  213. capitalize_string_remaps["k1"] = "K1";
  214. capitalize_string_remaps["k2"] = "K2";
  215. capitalize_string_remaps["kb"] = "(KB)"; // Unit.
  216. capitalize_string_remaps["lcd"] = "LCD";
  217. capitalize_string_remaps["ldr"] = "LDR";
  218. capitalize_string_remaps["linuxbsd"] = "Linux/*BSD";
  219. capitalize_string_remaps["lod"] = "LOD";
  220. capitalize_string_remaps["lods"] = "LODs";
  221. capitalize_string_remaps["lowpass"] = "Low-pass";
  222. capitalize_string_remaps["macos"] = "macOS";
  223. capitalize_string_remaps["mb"] = "(MB)"; // Unit.
  224. capitalize_string_remaps["mjpeg"] = "MJPEG";
  225. capitalize_string_remaps["mms"] = "MMS";
  226. capitalize_string_remaps["ms"] = "(ms)"; // Unit
  227. capitalize_string_remaps["msaa"] = "MSAA";
  228. capitalize_string_remaps["msdf"] = "MSDF";
  229. // Not used for now as AudioEffectReverb has a `msec` property.
  230. //capitalize_string_remaps["msec"] = "(msec)"; // Unit.
  231. capitalize_string_remaps["navmesh"] = "NavMesh";
  232. capitalize_string_remaps["nfc"] = "NFC";
  233. capitalize_string_remaps["oidn"] = "OIDN";
  234. capitalize_string_remaps["ok"] = "OK";
  235. capitalize_string_remaps["opengl"] = "OpenGL";
  236. capitalize_string_remaps["opengl3"] = "OpenGL 3";
  237. capitalize_string_remaps["opentype"] = "OpenType";
  238. capitalize_string_remaps["openxr"] = "OpenXR";
  239. capitalize_string_remaps["osslsigncode"] = "osslsigncode";
  240. capitalize_string_remaps["pck"] = "PCK";
  241. capitalize_string_remaps["png"] = "PNG";
  242. capitalize_string_remaps["po2"] = "(Power of 2)"; // Unit.
  243. capitalize_string_remaps["ppc32"] = "ppc32";
  244. capitalize_string_remaps["ppc64"] = "ppc64";
  245. capitalize_string_remaps["pvrtc"] = "PVRTC";
  246. capitalize_string_remaps["pvs"] = "PVS";
  247. capitalize_string_remaps["rcedit"] = "rcedit";
  248. capitalize_string_remaps["rcodesign"] = "rcodesign";
  249. capitalize_string_remaps["rgb"] = "RGB";
  250. capitalize_string_remaps["rid"] = "RID";
  251. capitalize_string_remaps["rmb"] = "RMB";
  252. capitalize_string_remaps["rpc"] = "RPC";
  253. capitalize_string_remaps["rv64"] = "rv64";
  254. capitalize_string_remaps["s3tc"] = "S3TC";
  255. capitalize_string_remaps["scp"] = "SCP";
  256. capitalize_string_remaps["sdf"] = "SDF";
  257. capitalize_string_remaps["sdfgi"] = "SDFGI";
  258. capitalize_string_remaps["sdk"] = "SDK";
  259. capitalize_string_remaps["sec"] = "(sec)"; // Unit.
  260. capitalize_string_remaps["signtool"] = "signtool";
  261. capitalize_string_remaps["sms"] = "SMS";
  262. capitalize_string_remaps["srgb"] = "sRGB";
  263. capitalize_string_remaps["ssao"] = "SSAO";
  264. capitalize_string_remaps["ssh"] = "SSH";
  265. capitalize_string_remaps["ssil"] = "SSIL";
  266. capitalize_string_remaps["ssl"] = "SSL";
  267. capitalize_string_remaps["sss"] = "SSS";
  268. capitalize_string_remaps["stderr"] = "stderr";
  269. capitalize_string_remaps["stdout"] = "stdout";
  270. capitalize_string_remaps["sv"] = "SV";
  271. capitalize_string_remaps["svg"] = "SVG";
  272. capitalize_string_remaps["taa"] = "TAA";
  273. capitalize_string_remaps["tcp"] = "TCP";
  274. capitalize_string_remaps["textfile"] = "TextFile";
  275. capitalize_string_remaps["tls"] = "TLS";
  276. capitalize_string_remaps["tv"] = "TV";
  277. capitalize_string_remaps["ui"] = "UI";
  278. capitalize_string_remaps["uri"] = "URI";
  279. capitalize_string_remaps["url"] = "URL";
  280. capitalize_string_remaps["urls"] = "URLs";
  281. capitalize_string_remaps["us"] = U"(µs)"; // Unit.
  282. capitalize_string_remaps["usb"] = "USB";
  283. capitalize_string_remaps["usec"] = U"(µsec)"; // Unit.
  284. capitalize_string_remaps["uuid"] = "UUID";
  285. capitalize_string_remaps["uv"] = "UV";
  286. capitalize_string_remaps["uv1"] = "UV1";
  287. capitalize_string_remaps["uv2"] = "UV2";
  288. capitalize_string_remaps["vector2"] = "Vector2";
  289. capitalize_string_remaps["vpn"] = "VPN";
  290. capitalize_string_remaps["vram"] = "VRAM";
  291. capitalize_string_remaps["vrs"] = "VRS";
  292. capitalize_string_remaps["vsync"] = "V-Sync";
  293. capitalize_string_remaps["wap"] = "WAP";
  294. capitalize_string_remaps["webp"] = "WebP";
  295. capitalize_string_remaps["webrtc"] = "WebRTC";
  296. capitalize_string_remaps["websocket"] = "WebSocket";
  297. capitalize_string_remaps["wine"] = "wine";
  298. capitalize_string_remaps["wifi"] = "Wi-Fi";
  299. capitalize_string_remaps["x86"] = "x86";
  300. capitalize_string_remaps["x86_32"] = "x86_32";
  301. capitalize_string_remaps["x86_64"] = "x86_64";
  302. capitalize_string_remaps["xr"] = "XR";
  303. capitalize_string_remaps["xray"] = "X-Ray";
  304. capitalize_string_remaps["xy"] = "XY";
  305. capitalize_string_remaps["xz"] = "XZ";
  306. capitalize_string_remaps["yz"] = "YZ";
  307. // Articles, conjunctions, prepositions.
  308. // The following initialization is parsed in `editor/translations/scripts/common.py` with a regex.
  309. // The word definition format should be kept synced with the regex.
  310. stop_words = LocalVector<String>({
  311. "a",
  312. "an",
  313. "and",
  314. "as",
  315. "at",
  316. "by",
  317. "for",
  318. "in",
  319. "not",
  320. "of",
  321. "on",
  322. "or",
  323. "over",
  324. "per",
  325. "the",
  326. "then",
  327. "to",
  328. });
  329. // Translation context associated with a name.
  330. // The second key is either:
  331. // - `full/property/path`
  332. // - `Class::full/property/path`
  333. // In case a class name is needed to distinguish between usages, all usages should use the second format.
  334. //
  335. // The following initialization is parsed in `editor/translations/scripts/common.py` with a regex.
  336. // The map name and value definition format should be kept synced with the regex.
  337. translation_contexts["force"]["constant_force"] = "Physics";
  338. translation_contexts["force"]["force/8_bit"] = "Enforce";
  339. translation_contexts["force"]["force/mono"] = "Enforce";
  340. translation_contexts["force"]["force/max_rate"] = "Enforce";
  341. translation_contexts["force"]["force/max_rate_hz"] = "Enforce";
  342. translation_contexts["normal"]["theme_override_styles/normal"] = "Ordinary";
  343. translation_contexts["normal"]["TextureButton::texture_normal"] = "Ordinary";
  344. translation_contexts["normal"]["Decal::texture_normal"] = "Geometry";
  345. translation_contexts["normal"]["detail_normal"] = "Geometry";
  346. translation_contexts["normal"]["normal"] = "Geometry";
  347. }
  348. EditorPropertyNameProcessor::~EditorPropertyNameProcessor() {
  349. singleton = nullptr;
  350. }