2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* translation_loader_po.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "translation_loader_po.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
|
|
|
#include "core/os/file_access.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/translation.h"
|
|
|
|
#include "core/string/translation_po.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-18 17:34:36 +00:00
|
|
|
RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
|
2014-02-10 01:10:30 +00:00
|
|
|
enum Status {
|
|
|
|
STATUS_NONE,
|
|
|
|
STATUS_READING_ID,
|
|
|
|
STATUS_READING_STRING,
|
2020-07-16 08:52:06 +00:00
|
|
|
STATUS_READING_CONTEXT,
|
2020-08-07 11:17:12 +00:00
|
|
|
STATUS_READING_PLURAL,
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Status status = STATUS_NONE;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String msg_id;
|
|
|
|
String msg_str;
|
2020-07-16 08:52:06 +00:00
|
|
|
String msg_context;
|
|
|
|
Vector<String> msgs_plural;
|
2014-02-10 01:10:30 +00:00
|
|
|
String config;
|
2016-05-27 22:58:28 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_FILE_CORRUPT;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-08-07 11:17:12 +00:00
|
|
|
Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO));
|
2014-02-10 01:10:30 +00:00
|
|
|
int line = 1;
|
2020-07-16 08:52:06 +00:00
|
|
|
int plural_forms = 0;
|
|
|
|
int plural_index = -1;
|
|
|
|
bool entered_context = false;
|
2017-09-01 20:33:39 +00:00
|
|
|
bool skip_this = false;
|
|
|
|
bool skip_next = false;
|
2018-07-24 11:32:37 +00:00
|
|
|
bool is_eof = false;
|
2020-07-14 11:16:49 +00:00
|
|
|
const String path = f->get_path();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-24 11:32:37 +00:00
|
|
|
while (!is_eof) {
|
|
|
|
String l = f->get_line().strip_edges();
|
|
|
|
is_eof = f->eof_reached();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-24 11:32:37 +00:00
|
|
|
// If we reached last line and it's not a content line, break, otherwise let processing that last loop
|
2020-12-15 12:04:21 +00:00
|
|
|
if (is_eof && l.is_empty()) {
|
2020-07-16 08:52:06 +00:00
|
|
|
if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) {
|
2018-07-24 11:32:37 +00:00
|
|
|
memdelete(f);
|
2020-07-16 08:52:06 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line));
|
2018-07-24 11:32:37 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
2018-07-24 11:32:37 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 08:52:06 +00:00
|
|
|
if (l.begins_with("msgctxt")) {
|
|
|
|
if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
|
|
|
|
}
|
|
|
|
|
|
|
|
// In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
|
|
|
|
// and set "entered_context" to true to prevent adding twice.
|
|
|
|
if (!skip_this && msg_id != "") {
|
|
|
|
if (status == STATUS_READING_STRING) {
|
|
|
|
translation->add_message(msg_id, msg_str, msg_context);
|
|
|
|
} else if (status == STATUS_READING_PLURAL) {
|
|
|
|
if (plural_index != plural_forms - 1) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
|
|
|
|
}
|
|
|
|
translation->add_plural_message(msg_id, msgs_plural, msg_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg_context = "";
|
|
|
|
l = l.substr(7, l.length()).strip_edges();
|
|
|
|
status = STATUS_READING_CONTEXT;
|
|
|
|
entered_context = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l.begins_with("msgid_plural")) {
|
|
|
|
if (plural_forms == 0) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line));
|
|
|
|
} else if (status != STATUS_READING_ID) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
|
|
|
|
}
|
|
|
|
// We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already.
|
|
|
|
// We just have to reset variables related to plurals for "msgstr[]" later on.
|
|
|
|
l = l.substr(12, l.length()).strip_edges();
|
|
|
|
plural_index = -1;
|
|
|
|
msgs_plural.clear();
|
|
|
|
msgs_plural.resize(plural_forms);
|
|
|
|
status = STATUS_READING_PLURAL;
|
|
|
|
} else if (l.begins_with("msgid")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (status == STATUS_READING_ID) {
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete(f);
|
2020-07-14 11:16:49 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 14:33:12 +00:00
|
|
|
if (msg_id != "") {
|
2020-07-16 08:52:06 +00:00
|
|
|
if (!skip_this && !entered_context) {
|
|
|
|
if (status == STATUS_READING_STRING) {
|
|
|
|
translation->add_message(msg_id, msg_str, msg_context);
|
|
|
|
} else if (status == STATUS_READING_PLURAL) {
|
|
|
|
if (plural_index != plural_forms - 1) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
|
|
|
|
}
|
|
|
|
translation->add_plural_message(msg_id, msgs_plural, msg_context);
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else if (config == "") {
|
2017-03-05 15:44:50 +00:00
|
|
|
config = msg_str;
|
2020-07-16 08:52:06 +00:00
|
|
|
// Record plural rule.
|
|
|
|
int p_start = config.find("Plural-Forms");
|
|
|
|
if (p_start != -1) {
|
|
|
|
int p_end = config.find("\n", p_start);
|
|
|
|
translation->set_plural_rule(config.substr(p_start, p_end - p_start));
|
|
|
|
plural_forms = translation->get_plural_forms();
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.substr(5, l.length()).strip_edges();
|
|
|
|
status = STATUS_READING_ID;
|
2020-07-16 08:52:06 +00:00
|
|
|
// If we did not encounter msgctxt, we reset context to empty to reset it.
|
|
|
|
if (!entered_context) {
|
|
|
|
msg_context = "";
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
msg_id = "";
|
|
|
|
msg_str = "";
|
2017-07-12 14:33:12 +00:00
|
|
|
skip_this = skip_next;
|
|
|
|
skip_next = false;
|
2020-07-16 08:52:06 +00:00
|
|
|
entered_context = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 08:52:06 +00:00
|
|
|
if (l.begins_with("msgstr[")) {
|
|
|
|
if (status != STATUS_READING_PLURAL) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
|
|
|
|
}
|
|
|
|
plural_index++; // Increment to add to the next slot in vector msgs_plural.
|
|
|
|
l = l.substr(9, l.length()).strip_edges();
|
|
|
|
} else if (l.begins_with("msgstr")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (status != STATUS_READING_ID) {
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete(f);
|
2020-07-16 08:52:06 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.substr(6, l.length()).strip_edges();
|
|
|
|
status = STATUS_READING_STRING;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (l == "" || l.begins_with("#")) {
|
2017-07-12 14:33:12 +00:00
|
|
|
if (l.find("fuzzy") != -1) {
|
|
|
|
skip_next = true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
line++;
|
2020-07-16 08:52:06 +00:00
|
|
|
continue; // Nothing to read or comment.
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:16:49 +00:00
|
|
|
if (!l.begins_with("\"") || status == STATUS_NONE) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.substr(1, l.length());
|
2020-03-20 07:47:43 +00:00
|
|
|
// Find final quote, ignoring escaped ones (\").
|
|
|
|
// The escape_next logic is necessary to properly parse things like \\"
|
|
|
|
// where the blackslash is the one being escaped, not the quote.
|
2017-03-05 15:44:50 +00:00
|
|
|
int end_pos = -1;
|
2020-03-20 07:47:43 +00:00
|
|
|
bool escape_next = false;
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < l.length(); i++) {
|
2020-03-20 07:47:43 +00:00
|
|
|
if (l[i] == '\\' && !escape_next) {
|
|
|
|
escape_next = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-20 07:47:43 +00:00
|
|
|
if (l[i] == '"' && !escape_next) {
|
2017-03-05 15:44:50 +00:00
|
|
|
end_pos = i;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-20 07:47:43 +00:00
|
|
|
|
|
|
|
escape_next = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:16:49 +00:00
|
|
|
if (end_pos == -1) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.substr(0, end_pos);
|
|
|
|
l = l.c_unescape();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (status == STATUS_READING_ID) {
|
2017-03-05 15:44:50 +00:00
|
|
|
msg_id += l;
|
2020-07-16 08:52:06 +00:00
|
|
|
} else if (status == STATUS_READING_STRING) {
|
2017-03-05 15:44:50 +00:00
|
|
|
msg_str += l;
|
2020-07-16 08:52:06 +00:00
|
|
|
} else if (status == STATUS_READING_CONTEXT) {
|
|
|
|
msg_context += l;
|
|
|
|
} else if (status == STATUS_READING_PLURAL && plural_index >= 0) {
|
|
|
|
msgs_plural.write[plural_index] = msgs_plural[plural_index] + l;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(f);
|
|
|
|
|
2020-07-16 08:52:06 +00:00
|
|
|
// Add the last set of data from last iteration.
|
2018-07-24 11:32:37 +00:00
|
|
|
if (status == STATUS_READING_STRING) {
|
|
|
|
if (msg_id != "") {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!skip_this) {
|
2020-07-16 08:52:06 +00:00
|
|
|
translation->add_message(msg_id, msg_str, msg_context);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
} else if (config == "") {
|
2018-07-24 11:32:37 +00:00
|
|
|
config = msg_str;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-07-16 08:52:06 +00:00
|
|
|
} else if (status == STATUS_READING_PLURAL) {
|
|
|
|
if (!skip_this && msg_id != "") {
|
|
|
|
if (plural_index != plural_forms - 1) {
|
|
|
|
memdelete(f);
|
|
|
|
ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
|
|
|
|
}
|
|
|
|
translation->add_plural_message(msg_id, msgs_plural, msg_context);
|
|
|
|
}
|
2018-07-24 11:32:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 11:16:49 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector<String> configs = config.split("\n");
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < configs.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
String c = configs[i].strip_edges();
|
|
|
|
int p = c.find(":");
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p == -1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
String prop = c.substr(0, p).strip_edges();
|
|
|
|
String value = c.substr(p + 1, c.length()).strip_edges();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-05-28 03:21:05 +00:00
|
|
|
if (prop == "X-Language" || prop == "Language") {
|
2014-02-10 01:10:30 +00:00
|
|
|
translation->set_locale(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = OK;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return translation;
|
2016-05-27 22:58:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 02:19:21 +00:00
|
|
|
RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_CANT_OPEN;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-05-27 22:58:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return load_translation(f, r_error);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
p_extensions->push_back("po");
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool TranslationLoaderPO::handles_type(const String &p_type) const {
|
|
|
|
return (p_type == "Translation");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String TranslationLoaderPO::get_resource_type(const String &p_path) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "po") {
|
2014-02-10 01:10:30 +00:00
|
|
|
return "Translation";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return "";
|
|
|
|
}
|