1 /*************************************************************************/
2 /*  translation.h                                                        */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #ifndef TRANSLATION_H
32 #define TRANSLATION_H
33 
34 #include "core/resource.h"
35 
36 class Translation : public Resource {
37 
38 	GDCLASS(Translation, Resource);
39 	OBJ_SAVE_TYPE(Translation);
40 	RES_BASE_EXTENSION("translation");
41 
42 	String locale;
43 	Map<StringName, StringName> translation_map;
44 
45 	PoolVector<String> _get_message_list() const;
46 
47 	PoolVector<String> _get_messages() const;
48 	void _set_messages(const PoolVector<String> &p_messages);
49 
50 protected:
51 	static void _bind_methods();
52 
53 public:
54 	void set_locale(const String &p_locale);
get_locale()55 	_FORCE_INLINE_ String get_locale() const { return locale; }
56 
57 	void add_message(const StringName &p_src_text, const StringName &p_xlated_text);
58 	virtual StringName get_message(const StringName &p_src_text) const; //overridable for other implementations
59 	void erase_message(const StringName &p_src_text);
60 
61 	void get_message_list(List<StringName> *r_messages) const;
62 	int get_message_count() const;
63 
64 	Translation();
65 };
66 
67 class TranslationServer : public Object {
68 
69 	GDCLASS(TranslationServer, Object);
70 
71 	String locale;
72 	String fallback;
73 
74 	Set<Ref<Translation> > translations;
75 	Ref<Translation> tool_translation;
76 
77 	Map<String, String> locale_name_map;
78 
79 	bool enabled;
80 
81 	static TranslationServer *singleton;
82 	bool _load_translations(const String &p_from);
83 
84 	static void _bind_methods();
85 
86 public:
get_singleton()87 	_FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
88 
set_enabled(bool p_enabled)89 	void set_enabled(bool p_enabled) { enabled = p_enabled; }
is_enabled()90 	_FORCE_INLINE_ bool is_enabled() const { return enabled; }
91 
92 	void set_locale(const String &p_locale);
93 	String get_locale() const;
94 
95 	String get_locale_name(const String &p_locale) const;
96 
97 	Array get_loaded_locales() const;
98 
99 	void add_translation(const Ref<Translation> &p_translation);
100 	void remove_translation(const Ref<Translation> &p_translation);
101 
102 	StringName translate(const StringName &p_message) const;
103 
104 	static Vector<String> get_all_locales();
105 	static Vector<String> get_all_locale_names();
106 	static bool is_locale_valid(const String &p_locale);
107 	static String standardize_locale(const String &p_locale);
108 	static String get_language_code(const String &p_locale);
109 
110 	void set_tool_translation(const Ref<Translation> &p_translation);
111 	StringName tool_translate(const StringName &p_message) const;
112 
113 	void setup();
114 
115 	void clear();
116 
117 	void load_translations();
118 
119 	TranslationServer();
120 };
121 
122 #endif // TRANSLATION_H
123