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
|
|
|
/*************************************************************************/
|
2018-01-01 13:40:08 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 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"
|
|
|
|
#include "os/file_access.h"
|
|
|
|
#include "translation.h"
|
|
|
|
|
2016-05-27 22:58:28 +00:00
|
|
|
RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const String &p_path) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
enum Status {
|
|
|
|
|
|
|
|
STATUS_NONE,
|
|
|
|
STATUS_READING_ID,
|
|
|
|
STATUS_READING_STRING,
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
String config;
|
2016-05-27 22:58:28 +00:00
|
|
|
|
2015-08-23 23:15:56 +00:00
|
|
|
if (r_error)
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_FILE_CORRUPT;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Ref<Translation> translation = Ref<Translation>(memnew(Translation));
|
2014-02-10 01:10:30 +00:00
|
|
|
int line = 1;
|
2017-09-01 20:33:39 +00:00
|
|
|
bool skip_this = false;
|
|
|
|
bool skip_next = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String l = f->get_line();
|
|
|
|
|
|
|
|
if (f->eof_reached()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (status == STATUS_READING_STRING) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-12 14:33:12 +00:00
|
|
|
if (msg_id != "") {
|
|
|
|
if (!skip_this)
|
|
|
|
translation->add_message(msg_id, msg_str);
|
|
|
|
} else if (config == "")
|
2017-03-05 15:44:50 +00:00
|
|
|
config = msg_str;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (status == STATUS_NONE)
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
memdelete(f);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: ");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.strip_edges();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
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);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: ");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
2017-07-12 14:33:12 +00:00
|
|
|
if (msg_id != "") {
|
|
|
|
if (!skip_this)
|
|
|
|
translation->add_message(msg_id, msg_str);
|
|
|
|
} else if (config == "")
|
2017-03-05 15:44:50 +00:00
|
|
|
config = msg_str;
|
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;
|
|
|
|
msg_id = "";
|
|
|
|
msg_str = "";
|
2017-07-12 14:33:12 +00:00
|
|
|
skip_this = skip_next;
|
|
|
|
skip_next = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
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++;
|
|
|
|
continue; //nothing to read or comment
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!l.begins_with("\"") || status == STATUS_NONE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//not a string? failure!
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: ");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
l = l.substr(1, l.length());
|
2014-02-10 01:10:30 +00:00
|
|
|
//find final quote
|
2017-03-05 15:44:50 +00:00
|
|
|
int end_pos = -1;
|
|
|
|
for (int i = 0; i < l.length(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (l[i] == '"' && (i == 0 || l[i - 1] != '\\')) {
|
|
|
|
end_pos = i;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (end_pos == -1) {
|
|
|
|
ERR_EXPLAIN(p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: ");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (status == STATUS_READING_ID)
|
|
|
|
msg_id += l;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
msg_str += l;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (config == "") {
|
|
|
|
ERR_EXPLAIN("No config found in file: " + p_path);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(RES());
|
|
|
|
}
|
|
|
|
|
|
|
|
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(":");
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p == -1)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
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
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (prop == "X-Language") {
|
2014-02-10 01:10:30 +00:00
|
|
|
translation->set_locale(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:15:56 +00:00
|
|
|
if (r_error)
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return translation;
|
2016-05-27 22:58:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2016-05-27 22:58:28 +00:00
|
|
|
|
|
|
|
if (r_error)
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_CANT_OPEN;
|
2016-05-27 22:58:28 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
ERR_FAIL_COND_V(!f, RES());
|
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");
|
|
|
|
//p_extensions->push_back("mo"); //mo in the future...
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
bool TranslationLoaderPO::handles_type(const String &p_type) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (p_type == "Translation");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String TranslationLoaderPO::get_resource_type(const String &p_path) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_path.get_extension().to_lower() == "po")
|
2014-02-10 01:10:30 +00:00
|
|
|
return "Translation";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
TranslationLoaderPO::TranslationLoaderPO() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|