1 | /**************************************************************************/ |
2 | /* editor_translation.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 | |
31 | #include "editor/editor_translation.h" |
32 | |
33 | #include "core/io/compression.h" |
34 | #include "core/io/file_access_memory.h" |
35 | #include "core/io/translation_loader_po.h" |
36 | #include "editor/doc_translations.gen.h" |
37 | #include "editor/editor_translations.gen.h" |
38 | #include "editor/property_translations.gen.h" |
39 | |
40 | Vector<String> get_editor_locales() { |
41 | Vector<String> locales; |
42 | |
43 | EditorTranslationList *etl = _editor_translations; |
44 | while (etl->data) { |
45 | const String &locale = etl->lang; |
46 | locales.push_back(locale); |
47 | |
48 | etl++; |
49 | } |
50 | |
51 | return locales; |
52 | } |
53 | |
54 | void load_editor_translations(const String &p_locale) { |
55 | EditorTranslationList *etl = _editor_translations; |
56 | while (etl->data) { |
57 | if (etl->lang == p_locale) { |
58 | Vector<uint8_t> data; |
59 | data.resize(etl->uncomp_size); |
60 | int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE); |
61 | ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt." ); |
62 | |
63 | Ref<FileAccessMemory> fa; |
64 | fa.instantiate(); |
65 | fa->open_custom(data.ptr(), data.size()); |
66 | |
67 | Ref<Translation> tr = TranslationLoaderPO::load_translation(fa); |
68 | |
69 | if (tr.is_valid()) { |
70 | tr->set_locale(etl->lang); |
71 | TranslationServer::get_singleton()->set_tool_translation(tr); |
72 | break; |
73 | } |
74 | } |
75 | |
76 | etl++; |
77 | } |
78 | } |
79 | |
80 | void load_doc_translations(const String &p_locale) { |
81 | DocTranslationList *dtl = _doc_translations; |
82 | while (dtl->data) { |
83 | if (dtl->lang == p_locale) { |
84 | Vector<uint8_t> data; |
85 | data.resize(dtl->uncomp_size); |
86 | int ret = Compression::decompress(data.ptrw(), dtl->uncomp_size, dtl->data, dtl->comp_size, Compression::MODE_DEFLATE); |
87 | ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt." ); |
88 | |
89 | Ref<FileAccessMemory> fa; |
90 | fa.instantiate(); |
91 | fa->open_custom(data.ptr(), data.size()); |
92 | |
93 | Ref<Translation> tr = TranslationLoaderPO::load_translation(fa); |
94 | |
95 | if (tr.is_valid()) { |
96 | tr->set_locale(dtl->lang); |
97 | TranslationServer::get_singleton()->set_doc_translation(tr); |
98 | break; |
99 | } |
100 | } |
101 | |
102 | dtl++; |
103 | } |
104 | } |
105 | |
106 | void load_property_translations(const String &p_locale) { |
107 | PropertyTranslationList *etl = _property_translations; |
108 | while (etl->data) { |
109 | if (etl->lang == p_locale) { |
110 | Vector<uint8_t> data; |
111 | data.resize(etl->uncomp_size); |
112 | int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE); |
113 | ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt." ); |
114 | |
115 | Ref<FileAccessMemory> fa; |
116 | fa.instantiate(); |
117 | fa->open_custom(data.ptr(), data.size()); |
118 | |
119 | Ref<Translation> tr = TranslationLoaderPO::load_translation(fa); |
120 | |
121 | if (tr.is_valid()) { |
122 | tr->set_locale(etl->lang); |
123 | TranslationServer::get_singleton()->set_property_translation(tr); |
124 | break; |
125 | } |
126 | } |
127 | |
128 | etl++; |
129 | } |
130 | } |
131 | |