Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/string/translation_server.h
9898 views
1
/**************************************************************************/
2
/* translation_server.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/string/translation.h"
34
#include "core/string/translation_domain.h"
35
36
class TranslationServer : public Object {
37
GDCLASS(TranslationServer, Object);
38
39
String locale = "en";
40
String fallback;
41
42
Ref<TranslationDomain> main_domain;
43
Ref<TranslationDomain> editor_domain;
44
Ref<TranslationDomain> property_domain;
45
Ref<TranslationDomain> doc_domain;
46
HashMap<StringName, Ref<TranslationDomain>> custom_domains;
47
48
mutable HashMap<String, int> locale_compare_cache;
49
50
static inline TranslationServer *singleton = nullptr;
51
52
static void _bind_methods();
53
54
#ifndef DISABLE_DEPRECATED
55
String _standardize_locale_bind_compat_98972(const String &p_locale) const;
56
static void _bind_compatibility_methods();
57
#endif
58
59
struct LocaleScriptInfo {
60
String name;
61
String script;
62
String default_country;
63
HashSet<String> supported_countries;
64
};
65
static Vector<LocaleScriptInfo> locale_script_info;
66
67
struct Locale {
68
String language;
69
String script;
70
String country;
71
String variant;
72
73
bool operator==(const Locale &p_locale) const {
74
return (p_locale.language == language) &&
75
(p_locale.script == script) &&
76
(p_locale.country == country) &&
77
(p_locale.variant == variant);
78
}
79
80
explicit operator String() const;
81
82
Locale(const TranslationServer &p_server, const String &p_locale, bool p_add_defaults);
83
};
84
85
static HashMap<String, String> language_map;
86
static HashMap<String, String> script_map;
87
static HashMap<String, String> locale_rename_map;
88
static HashMap<String, String> country_name_map;
89
static HashMap<String, String> country_rename_map;
90
static HashMap<String, String> variant_map;
91
92
void init_locale_info();
93
94
public:
95
_FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
96
97
Ref<TranslationDomain> get_main_domain() const { return main_domain; }
98
Ref<TranslationDomain> get_editor_domain() const { return editor_domain; }
99
100
void set_locale(const String &p_locale);
101
String get_locale() const;
102
void set_fallback_locale(const String &p_locale);
103
String get_fallback_locale() const;
104
Ref<Translation> get_translation_object(const String &p_locale);
105
106
Vector<String> get_all_languages() const;
107
String get_language_name(const String &p_language) const;
108
109
Vector<String> get_all_scripts() const;
110
String get_script_name(const String &p_script) const;
111
112
Vector<String> get_all_countries() const;
113
String get_country_name(const String &p_country) const;
114
115
String get_locale_name(const String &p_locale) const;
116
117
PackedStringArray get_loaded_locales() const;
118
119
void add_translation(const Ref<Translation> &p_translation);
120
void remove_translation(const Ref<Translation> &p_translation);
121
122
StringName translate(const StringName &p_message, const StringName &p_context = "") const;
123
StringName translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
124
125
StringName pseudolocalize(const StringName &p_message) const;
126
127
bool is_pseudolocalization_enabled() const;
128
void set_pseudolocalization_enabled(bool p_enabled);
129
void reload_pseudolocalization();
130
131
String standardize_locale(const String &p_locale, bool p_add_defaults = false) const;
132
133
int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
134
135
String get_tool_locale();
136
StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
137
StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
138
StringName property_translate(const StringName &p_message, const StringName &p_context = "") const;
139
StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
140
StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
141
142
bool has_domain(const StringName &p_domain) const;
143
Ref<TranslationDomain> get_or_add_domain(const StringName &p_domain);
144
void remove_domain(const StringName &p_domain);
145
146
void setup();
147
148
void clear();
149
150
void load_translations();
151
152
#ifdef TOOLS_ENABLED
153
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
154
#endif // TOOLS_ENABLED
155
156
TranslationServer();
157
};
158
159