Path: blob/master/editor/translations/pot_generator.cpp
9896 views
/**************************************************************************/1/* pot_generator.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "pot_generator.h"3132#include "core/config/project_settings.h"33#include "core/error/error_macros.h"34#include "editor/translations/editor_translation.h"35#include "editor/translations/editor_translation_parser.h"3637POTGenerator *POTGenerator::singleton = nullptr;3839#ifdef DEBUG_POT40void POTGenerator::_print_all_translation_strings() {41for (HashMap<String, Vector<POTGenerator::MsgidData>>::Element E = all_translation_strings.front(); E; E = E.next()) {42Vector<MsgidData> v_md = all_translation_strings[E.key()];43for (int i = 0; i < v_md.size(); i++) {44print_line("++++++");45print_line("msgid: " + E.key());46print_line("context: " + v_md[i].ctx);47print_line("msgid_plural: " + v_md[i].plural);48for (const String &F : v_md[i].locations) {49print_line("location: " + F);50}51}52}53}54#endif5556void POTGenerator::generate_pot(const String &p_file) {57Vector<String> files = GLOBAL_GET("internationalization/locale/translations_pot_files");5859if (files.is_empty()) {60WARN_PRINT("No files selected for POT generation.");61return;62}6364// Clear all_translation_strings of the previous round.65all_translation_strings.clear();6667// Collect all translatable strings according to files order in "POT Generation" setting.68for (int i = 0; i < files.size(); i++) {69Vector<Vector<String>> translations;7071const String &file_path = files[i];72String file_extension = file_path.get_extension();7374if (EditorTranslationParser::get_singleton()->can_parse(file_extension)) {75EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &translations);76} else {77ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()");78return;79}8081for (const Vector<String> &translation : translations) {82ERR_CONTINUE(translation.is_empty());83const String &msgctxt = (translation.size() > 1) ? translation[1] : String();84const String &msgid_plural = (translation.size() > 2) ? translation[2] : String();85const String &comment = (translation.size() > 3) ? translation[3] : String();86_add_new_msgid(translation[0], msgctxt, msgid_plural, file_path, comment);87}88}8990if (GLOBAL_GET("internationalization/locale/translation_add_builtin_strings_to_pot")) {91for (const Vector<String> &extractable_msgids : get_extractable_message_list()) {92_add_new_msgid(extractable_msgids[0], extractable_msgids[1], extractable_msgids[2], "", "");93}94}9596_write_to_pot(p_file);97}9899void POTGenerator::_write_to_pot(const String &p_file) {100Error err;101Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);102if (err != OK) {103ERR_PRINT("Failed to open " + p_file);104return;105}106107String project_name = GLOBAL_GET("application/config/name").operator String().replace("\n", "\\n");108Vector<String> files = GLOBAL_GET("internationalization/locale/translations_pot_files");109String extracted_files = "";110for (int i = 0; i < files.size(); i++) {111extracted_files += "# " + files[i].replace("\n", "\\n") + "\n";112}113const String header =114"# LANGUAGE translation for " + project_name + " for the following files:\n" +115extracted_files +116"#\n"117"# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n"118"#\n"119"#, fuzzy\n"120"msgid \"\"\n"121"msgstr \"\"\n"122"\"Project-Id-Version: " +123project_name +124"\\n\"\n"125"\"MIME-Version: 1.0\\n\"\n"126"\"Content-Type: text/plain; charset=UTF-8\\n\"\n"127"\"Content-Transfer-Encoding: 8-bit\\n\"\n";128129file->store_string(header);130131for (const KeyValue<String, Vector<MsgidData>> &E_pair : all_translation_strings) {132String msgid = E_pair.key;133const Vector<MsgidData> &v_msgid_data = E_pair.value;134for (int i = 0; i < v_msgid_data.size(); i++) {135String context = v_msgid_data[i].ctx;136String plural = v_msgid_data[i].plural;137const HashSet<String> &locations = v_msgid_data[i].locations;138const HashSet<String> &comments = v_msgid_data[i].comments;139140// Put the blank line at the start, to avoid a double at the end when closing the file.141file->store_line("");142143// Write comments.144bool is_first_comment = true;145for (const String &E : comments) {146if (is_first_comment) {147file->store_line("#. TRANSLATORS: " + E.replace("\n", "\n#. "));148} else {149file->store_line("#. " + E.replace("\n", "\n#. "));150}151is_first_comment = false;152}153154// Write file locations.155for (const String &E : locations) {156file->store_line("#: " + E.trim_prefix("res://").replace("\n", "\\n"));157}158159// Write context.160if (!context.is_empty()) {161file->store_line("msgctxt " + context.json_escape().quote());162}163164// Write msgid.165_write_msgid(file, msgid, false);166167// Write msgid_plural.168if (!plural.is_empty()) {169_write_msgid(file, plural, true);170file->store_line("msgstr[0] \"\"");171file->store_line("msgstr[1] \"\"");172} else {173file->store_line("msgstr \"\"");174}175}176}177}178179void POTGenerator::_write_msgid(Ref<FileAccess> r_file, const String &p_id, bool p_plural) {180if (p_plural) {181r_file->store_string("msgid_plural ");182} else {183r_file->store_string("msgid ");184}185186if (p_id.is_empty()) {187r_file->store_line("\"\"");188return;189}190191const Vector<String> lines = p_id.split("\n");192const String &last_line = lines[lines.size() - 1]; // `lines` cannot be empty.193int pot_line_count = lines.size();194if (last_line.is_empty()) {195pot_line_count--;196}197198if (pot_line_count > 1) {199r_file->store_line("\"\"");200}201202for (int i = 0; i < lines.size() - 1; i++) {203r_file->store_line((lines[i] + "\n").json_escape().quote());204}205206if (!last_line.is_empty()) {207r_file->store_line(last_line.json_escape().quote());208}209}210211void POTGenerator::_add_new_msgid(const String &p_msgid, const String &p_context, const String &p_plural, const String &p_location, const String &p_comment) {212// Insert new location if msgid under same context exists already.213if (all_translation_strings.has(p_msgid)) {214Vector<MsgidData> &v_mdata = all_translation_strings[p_msgid];215for (int i = 0; i < v_mdata.size(); i++) {216if (v_mdata[i].ctx == p_context) {217if (!v_mdata[i].plural.is_empty() && !p_plural.is_empty() && v_mdata[i].plural != p_plural) {218WARN_PRINT("Redefinition of plural message (msgid_plural), under the same message (msgid) and context (msgctxt)");219}220if (!p_location.is_empty()) {221v_mdata.write[i].locations.insert(p_location);222}223if (!p_comment.is_empty()) {224v_mdata.write[i].comments.insert(p_comment);225}226return;227}228}229}230231// Add a new entry.232MsgidData mdata;233mdata.ctx = p_context;234mdata.plural = p_plural;235if (!p_location.is_empty()) {236mdata.locations.insert(p_location);237}238if (!p_comment.is_empty()) {239mdata.comments.insert(p_comment);240}241all_translation_strings[p_msgid].push_back(mdata);242}243244POTGenerator *POTGenerator::get_singleton() {245if (!singleton) {246singleton = memnew(POTGenerator);247}248return singleton;249}250251POTGenerator::~POTGenerator() {252memdelete(singleton);253singleton = nullptr;254}255256257