Merge pull request #59276 from bruvzg/mo_trans

This commit is contained in:
Rémi Verschelde 2022-03-22 12:48:14 +01:00 committed by GitHub
commit 44267327a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,6 +35,110 @@
#include "core/string/translation_po.h"
RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
if (r_error) {
*r_error = ERR_FILE_CORRUPT;
}
const String path = f->get_path();
Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO));
String config;
uint32_t magic = f->get_32();
if (magic == 0x950412de) {
// Load binary MO file.
uint16_t version_maj = f->get_16();
uint16_t version_min = f->get_16();
if (version_maj > 1) {
ERR_FAIL_V_MSG(RES(), vformat("Unsupported MO file %s, version %d.%d.", path, version_maj, version_min));
}
uint32_t num_strings = f->get_32();
uint32_t id_table_offset = f->get_32();
uint32_t trans_table_offset = f->get_32();
// Read string tables.
for (uint32_t i = 0; i < num_strings; i++) {
String msg_id;
String msg_id_plural;
String msg_context;
// Read id strings and context.
{
Vector<uint8_t> data;
f->seek(id_table_offset + i * 8);
uint32_t str_start = 0;
uint32_t str_len = f->get_32();
uint32_t str_offset = f->get_32();
data.resize(str_len + 1);
f->seek(str_offset);
f->get_buffer(data.ptrw(), str_len);
data.write[str_len] = 0;
bool is_plural = false;
for (uint32_t j = 0; j < str_len + 1; j++) {
if (data[j] == 0x04) {
msg_context.parse_utf8((const char *)data.ptr(), j);
str_start = j + 1;
}
if (data[j] == 0x00) {
if (is_plural) {
msg_id_plural.parse_utf8((const char *)(data.ptr() + str_start), j - str_start);
} else {
msg_id.parse_utf8((const char *)(data.ptr() + str_start), j - str_start);
is_plural = true;
}
str_start = j + 1;
}
}
}
// Read translated strings.
{
Vector<uint8_t> data;
f->seek(trans_table_offset + i * 8);
uint32_t str_start = 0;
uint32_t str_len = f->get_32();
uint32_t str_offset = f->get_32();
data.resize(str_len + 1);
f->seek(str_offset);
f->get_buffer(data.ptrw(), str_len);
data.write[str_len] = 0;
if (msg_id.is_empty()) {
config = String::utf8((const char *)data.ptr(), str_len);
// 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));
}
} else {
Vector<String> plural_msg;
for (uint32_t j = 0; j < str_len + 1; j++) {
if (data[j] == 0x00) {
if (msg_id_plural.is_empty()) {
translation->add_message(msg_id, String::utf8((const char *)(data.ptr() + str_start), j - str_start), msg_context);
} else {
plural_msg.push_back(String::utf8((const char *)(data.ptr() + str_start), j - str_start));
}
str_start = j + 1;
}
}
if (!plural_msg.is_empty()) {
translation->add_plural_message(msg_id, plural_msg, msg_context);
}
}
}
}
memdelete(f);
} else {
// Try to load as text PO file.
f->seek(0);
enum Status {
STATUS_NONE,
STATUS_READING_ID,
@ -49,13 +153,11 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
String msg_str;
String msg_context;
Vector<String> msgs_plural;
String config;
if (r_error) {
*r_error = ERR_FILE_CORRUPT;
}
Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO));
int line = 1;
int plural_forms = 0;
int plural_index = -1;
@ -63,7 +165,6 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
bool skip_this = false;
bool skip_next = false;
bool is_eof = false;
const String path = f->get_path();
while (!is_eof) {
String l = f->get_line().strip_edges();
@ -226,6 +327,10 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
} else if (status == STATUS_READING_CONTEXT) {
msg_context += l;
} else if (status == STATUS_READING_PLURAL && plural_index >= 0) {
if (plural_index >= plural_forms) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), "Unexpected plural form while parsing: " + path + ":" + itos(line));
}
msgs_plural.write[plural_index] = msgs_plural[plural_index] + l;
}
@ -252,6 +357,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
translation->add_plural_message(msg_id, msgs_plural, msg_context);
}
}
}
ERR_FAIL_COND_V_MSG(config.is_empty(), RES(), "No config found in file: " + path + ".");
@ -290,6 +396,7 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat
void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("po");
p_extensions->push_back("mo");
}
bool TranslationLoaderPO::handles_type(const String &p_type) const {
@ -297,7 +404,7 @@ bool TranslationLoaderPO::handles_type(const String &p_type) const {
}
String TranslationLoaderPO::get_resource_type(const String &p_path) const {
if (p_path.get_extension().to_lower() == "po") {
if (p_path.get_extension().to_lower() == "po" || p_path.get_extension().to_lower() == "mo") {
return "Translation";
}
return "";