editor_translation.cpp 4.8 KB

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