Debian Codegraph @

core/string/translation.h

raw symbols SCIP
1 /**************************************************************************/
2 /* translation.h */
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 #pragma once
32
33 #include "core/io/resource.h"
34 #include "core/object/gdvirtual.gen.inc"
35
36 class PluralRules;
37
38 class Translation : public Resource {
39 GDCLASS(Translation, Resource);
40 OBJ_SAVE_TYPE(Translation);
41 RES_BASE_EXTENSION("translation");
42
43 public:
44 struct MessageKey {
45 StringName msgctxt;
46 StringName msgid;
47
48 // Required to use this struct as a key in HashMap.
49 static uint32_t hash(const MessageKey &p_key) {
50 uint32_t h = hash_murmur3_one_32(HashMapHasherDefault::hash(p_key.msgctxt));
51 return hash_fmix32(hash_murmur3_one_32(HashMapHasherDefault::hash(p_key.msgid), h));
52 }
53 bool operator==(const MessageKey &p_key) const {
54 return msgctxt == p_key.msgctxt && msgid == p_key.msgid;
55 }
56 };
57
58 private:
59 String locale = "en";
60
61 HashMap<MessageKey, Vector<StringName>, MessageKey> translation_map;
62
63 mutable PluralRules *plural_rules_cache = nullptr;
64 String plural_rules_override;
65
66 virtual Vector<String> _get_message_list() const;
67
68 // For data storage.
69 virtual Dictionary _get_messages() const;
70 virtual void _set_messages(const Dictionary &p_messages);
71
72 protected:
73 static void _bind_methods();
74
75 PluralRules *_get_plural_rules() const;
76
77 GDVIRTUAL2RC(StringName, _get_message, StringName, StringName);
78 GDVIRTUAL4RC(StringName, _get_plural_message, StringName, StringName, int, StringName);
79
80 public:
81 void set_locale(const String &p_locale);
82 _FORCE_INLINE_ String get_locale() const { return locale; }
83
84 virtual void add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context = "");
85 virtual void add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context = "");
86 virtual StringName get_message(const StringName &p_src_text, const StringName &p_context = "") const; //overridable for other implementations
87 virtual StringName get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context = "") const;
88 virtual void erase_message(const StringName &p_src_text, const StringName &p_context = "");
89 virtual void get_message_list(List<StringName> *r_messages) const;
90 virtual int get_message_count() const;
91 virtual Vector<String> get_translated_message_list() const;
92
93 void set_plural_rules_override(const String &p_rules);
94 String get_plural_rules_override() const;
95
96 // This method is not exposed to scripting intentionally. It is only used by TranslationLoaderPO and tests.
97 int get_nplurals() const;
98
99 ~Translation();
100 };