| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 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; |
| 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 |
|
| 97 |
int get_nplurals() const; |
| 98 |
|
| 99 |
~Translation(); |
| 100 |
}; |