resource_importer_csv_translation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**************************************************************************/
  2. /* resource_importer_csv_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 "resource_importer_csv_translation.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/string/optimized_translation.h"
  34. #include "core/string/translation_server.h"
  35. String ResourceImporterCSVTranslation::get_importer_name() const {
  36. return "csv_translation";
  37. }
  38. String ResourceImporterCSVTranslation::get_visible_name() const {
  39. return "CSV Translation";
  40. }
  41. void ResourceImporterCSVTranslation::get_recognized_extensions(List<String> *p_extensions) const {
  42. p_extensions->push_back("csv");
  43. }
  44. String ResourceImporterCSVTranslation::get_save_extension() const {
  45. return ""; //does not save a single resource
  46. }
  47. String ResourceImporterCSVTranslation::get_resource_type() const {
  48. return "Translation";
  49. }
  50. bool ResourceImporterCSVTranslation::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  51. return true;
  52. }
  53. int ResourceImporterCSVTranslation::get_preset_count() const {
  54. return 0;
  55. }
  56. String ResourceImporterCSVTranslation::get_preset_name(int p_idx) const {
  57. return "";
  58. }
  59. void ResourceImporterCSVTranslation::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  60. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress", PROPERTY_HINT_ENUM, "Disabled,Auto"), 1)); // Enum for compatibility with previous versions.
  61. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "delimiter", PROPERTY_HINT_ENUM, "Comma,Semicolon,Tab"), 0));
  62. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "unescape_keys"), false));
  63. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "unescape_translations"), true));
  64. }
  65. Error ResourceImporterCSVTranslation::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  66. Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ);
  67. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_INVALID_PARAMETER, "Cannot open file from path '" + p_source_file + "'.");
  68. String delimiter;
  69. switch ((int)p_options["delimiter"]) {
  70. case 1: {
  71. delimiter = ";";
  72. } break;
  73. case 2: {
  74. delimiter = "\t";
  75. } break;
  76. default: {
  77. delimiter = ",";
  78. } break;
  79. }
  80. // Parse the header row.
  81. HashMap<int, Ref<Translation>> column_to_translation;
  82. int context_column = -1;
  83. int plural_column = -1;
  84. {
  85. const Vector<String> line = f->get_csv_line(delimiter);
  86. for (int i = 1; i < line.size(); i++) {
  87. if (line[i].left(1) == "_") {
  88. continue;
  89. }
  90. if (line[i].to_lower() == "?context") {
  91. ERR_CONTINUE_MSG(context_column != -1, "Error importing CSV translation: Multiple '?context' columns found. Only one is allowed. Subsequent ones will be ignored.");
  92. context_column = i;
  93. continue;
  94. }
  95. if (line[i].to_lower() == "?plural") {
  96. ERR_CONTINUE_MSG(plural_column != -1, "Error importing CSV translation: Multiple '?plural' columns found. Only one is allowed. Subsequent ones will be ignored.");
  97. plural_column = i;
  98. continue;
  99. }
  100. const String locale = TranslationServer::get_singleton()->standardize_locale(line[i]);
  101. ERR_CONTINUE_MSG(locale.is_empty(), vformat("Error importing CSV translation: Invalid locale format '%s', should be 'language_Script_COUNTRY_VARIANT@extra'. This column will be ignored.", line[i]));
  102. Ref<Translation> translation;
  103. translation.instantiate();
  104. translation->set_locale(locale);
  105. column_to_translation[i] = translation;
  106. }
  107. if (column_to_translation.is_empty()) {
  108. WARN_PRINT(vformat("CSV file '%s' does not contain any translation.", p_source_file));
  109. return OK;
  110. }
  111. }
  112. // Parse content rows.
  113. bool context_used = false;
  114. bool plural_used = false;
  115. {
  116. const bool unescape_keys = p_options.has("unescape_keys") ? bool(p_options["unescape_keys"]) : false;
  117. const bool unescape_translations = p_options.has("unescape_translations") ? bool(p_options["unescape_translations"]) : true;
  118. bool reading_plural_rows = false;
  119. String plural_msgid;
  120. String plural_msgctxt;
  121. HashMap<int, Vector<String>> plural_msgstrs;
  122. do {
  123. const Vector<String> line = f->get_csv_line(delimiter);
  124. // Skip empty lines.
  125. if (line.size() == 1 && line[0].is_empty()) {
  126. continue;
  127. }
  128. if (line[0].to_lower() == "?pluralrule") {
  129. for (int i = 1; i < line.size(); i++) {
  130. if (line[i].is_empty() || !column_to_translation.has(i)) {
  131. continue;
  132. }
  133. Ref<Translation> translation = column_to_translation[i];
  134. ERR_CONTINUE_MSG(!translation->get_plural_rules_override().is_empty(), vformat("Error importing CSV translation: Multiple '?pluralrule' definitions found for locale '%s'. Only one is allowed. Subsequent ones will be ignored.", translation->get_locale()));
  135. translation->set_plural_rules_override(line[i]);
  136. }
  137. continue;
  138. }
  139. const String msgid = unescape_keys ? line[0].c_unescape() : line[0];
  140. if (!reading_plural_rows && msgid.is_empty()) {
  141. continue;
  142. }
  143. // It's okay if you define context or plural columns but don't use them.
  144. const String msgctxt = (context_column != -1 && context_column < line.size()) ? line[context_column] : String();
  145. if (!msgctxt.is_empty()) {
  146. context_used = true;
  147. }
  148. const String msgid_plural = (plural_column != -1 && plural_column < line.size()) ? line[plural_column] : String();
  149. if (!msgid_plural.is_empty()) {
  150. plural_used = true;
  151. }
  152. // End of plural rows.
  153. if (reading_plural_rows && (!msgid.is_empty() || !msgctxt.is_empty() || !msgid_plural.is_empty())) {
  154. reading_plural_rows = false;
  155. for (KeyValue<int, Ref<Translation>> E : column_to_translation) {
  156. Ref<Translation> translation = E.value;
  157. const Vector<String> &msgstrs = plural_msgstrs[E.key];
  158. if (!msgstrs.is_empty()) {
  159. translation->add_plural_message(plural_msgid, msgstrs, plural_msgctxt);
  160. }
  161. }
  162. plural_msgstrs.clear();
  163. }
  164. // Start of plural rows.
  165. if (!reading_plural_rows && !msgid_plural.is_empty()) {
  166. reading_plural_rows = true;
  167. plural_msgid = msgid;
  168. plural_msgctxt = msgctxt;
  169. }
  170. for (int i = 1; i < line.size(); i++) {
  171. if (!column_to_translation.has(i)) {
  172. continue;
  173. }
  174. const String msgstr = unescape_translations ? line[i].c_unescape() : line[i];
  175. if (msgstr.is_empty()) {
  176. continue;
  177. }
  178. if (reading_plural_rows) {
  179. plural_msgstrs[i].push_back(msgstr);
  180. } else {
  181. column_to_translation[i]->add_message(msgid, msgstr, msgctxt);
  182. }
  183. }
  184. } while (!f->eof_reached());
  185. if (reading_plural_rows) {
  186. for (KeyValue<int, Ref<Translation>> E : column_to_translation) {
  187. Ref<Translation> translation = E.value;
  188. const Vector<String> &msgstrs = plural_msgstrs[E.key];
  189. if (!msgstrs.is_empty()) {
  190. translation->add_plural_message(plural_msgid, msgstrs, plural_msgctxt);
  191. }
  192. }
  193. }
  194. }
  195. bool compress;
  196. switch ((int)p_options["compress"]) {
  197. case 0: { // Disabled.
  198. compress = false;
  199. } break;
  200. default: { // Auto.
  201. compress = !context_used && !plural_used;
  202. } break;
  203. }
  204. for (KeyValue<int, Ref<Translation>> E : column_to_translation) {
  205. Ref<Translation> xlt = E.value;
  206. if (compress) {
  207. Ref<OptimizedTranslation> cxl = memnew(OptimizedTranslation);
  208. cxl->generate(xlt);
  209. xlt = cxl;
  210. }
  211. String save_path = p_source_file.get_basename() + "." + xlt->get_locale() + ".translation";
  212. ResourceUID::ID save_id = hash64_murmur3_64(xlt->get_locale().hash64(), p_source_id) & 0x7FFFFFFFFFFFFFFF;
  213. bool uid_already_exists = ResourceUID::get_singleton()->has_id(save_id);
  214. if (uid_already_exists) {
  215. // Avoid creating a new file with a duplicate UID.
  216. // Always use this UID, even if the user has moved it to a different path.
  217. save_path = ResourceUID::get_singleton()->get_id_path(save_id);
  218. }
  219. ResourceSaver::save(xlt, save_path);
  220. if (r_gen_files) {
  221. r_gen_files->push_back(save_path);
  222. }
  223. if (!uid_already_exists) {
  224. // No need to call set_uid if save_path already refers to save_id.
  225. ResourceSaver::set_uid(save_path, save_id);
  226. }
  227. }
  228. return OK;
  229. }