editor_translation.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Vector<String> get_editor_locales() {
  37. Vector<String> locales;
  38. EditorTranslationList *etl = _editor_translations;
  39. while (etl->data) {
  40. const String &locale = etl->lang;
  41. locales.push_back(locale);
  42. etl++;
  43. }
  44. return locales;
  45. }
  46. void load_editor_translations(const String &p_locale) {
  47. EditorTranslationList *etl = _editor_translations;
  48. while (etl->data) {
  49. if (etl->lang == p_locale) {
  50. Vector<uint8_t> data;
  51. data.resize(etl->uncomp_size);
  52. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  53. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  54. FileAccessMemory *fa = memnew(FileAccessMemory);
  55. fa->open_custom(data.ptr(), data.size());
  56. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, true);
  57. if (tr.is_valid()) {
  58. tr->set_locale(etl->lang);
  59. TranslationServer::get_singleton()->set_tool_translation(tr);
  60. break;
  61. }
  62. }
  63. etl++;
  64. }
  65. }
  66. void load_doc_translations(const String &p_locale) {
  67. DocTranslationList *dtl = _doc_translations;
  68. while (dtl->data) {
  69. if (dtl->lang == p_locale) {
  70. Vector<uint8_t> data;
  71. data.resize(dtl->uncomp_size);
  72. int ret = Compression::decompress(data.ptrw(), dtl->uncomp_size, dtl->data, dtl->comp_size, Compression::MODE_DEFLATE);
  73. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  74. FileAccessMemory *fa = memnew(FileAccessMemory);
  75. fa->open_custom(data.ptr(), data.size());
  76. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, false);
  77. if (tr.is_valid()) {
  78. tr->set_locale(dtl->lang);
  79. TranslationServer::get_singleton()->set_doc_translation(tr);
  80. break;
  81. }
  82. }
  83. dtl++;
  84. }
  85. }