editor_property_name_processor.cpp 15 KB

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