Path: blob/master/editor/translations/editor_translation.cpp
21090 views
/**************************************************************************/1/* editor_translation.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 "editor_translation.h"3132#include "core/io/compression.h"33#include "core/io/file_access_memory.h"34#include "core/io/translation_loader_po.h"35#include "core/string/translation_server.h"36#include "editor/translations/doc_translations.gen.h"37#include "editor/translations/editor_translations.gen.h"38#include "editor/translations/extractable_translations.gen.h"39#include "editor/translations/property_translations.gen.h"4041Vector<String> get_editor_locales() {42Vector<String> locales;4344for (const EditorTranslationList *etl = _editor_translations; etl->data; etl++) {45const String &locale = etl->lang;46locales.push_back(locale);47}4849return locales;50}5152static void _load(const Ref<TranslationDomain> p_domain, const String &p_locale, const EditorTranslationList *p_etl) {53for (const EditorTranslationList *etl = p_etl; etl->data; etl++) {54if (etl->lang == p_locale) {55LocalVector<uint8_t> data;56data.resize_uninitialized(etl->uncomp_size);57const int64_t ret = Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);58ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");5960Ref<FileAccessMemory> fa;61fa.instantiate();62fa->open_custom(data.ptr(), data.size());6364Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);65if (tr.is_valid()) {66tr->set_locale(etl->lang);67p_domain->add_translation(tr);68break;69}70}71}72}7374void load_editor_translations(const String &p_locale) {75Ref<TranslationDomain> domain;7677domain = TranslationServer::get_singleton()->get_editor_domain();78domain->clear();79_load(domain, p_locale, _editor_translations);80_load(domain, p_locale, _extractable_translations);8182domain = TranslationServer::get_singleton()->get_property_domain();83domain->clear();84_load(domain, p_locale, _property_translations);85}8687void load_doc_translations(const String &p_locale) {88const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_doc_domain();89domain->clear();90_load(domain, p_locale, _doc_translations);91}9293Vector<Vector<String>> get_extractable_message_list() {94Vector<Vector<String>> list;9596for (const EditorTranslationList *etl = _extractable_translations; etl->data; etl++) {97if (strcmp(etl->lang, "source")) {98continue;99}100101LocalVector<uint8_t> data;102data.resize_uninitialized(etl->uncomp_size);103const int64_t ret = Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);104ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");105106Ref<FileAccessMemory> fa;107fa.instantiate();108fa->open_custom(data.ptr(), data.size());109110// Taken from TranslationLoaderPO, modified to work specifically with POTs.111{112const String path = fa->get_path();113114fa->seek(0);115116enum Status {117STATUS_NONE,118STATUS_READING_ID,119STATUS_READING_STRING,120STATUS_READING_CONTEXT,121STATUS_READING_PLURAL,122};123124Status status = STATUS_NONE;125126String msg_id;127String msg_id_plural;128String msg_context;129130int line = 1;131bool entered_context = false;132bool is_eof = false;133134while (!is_eof) {135String l = fa->get_line().strip_edges();136is_eof = fa->eof_reached();137138// If we reached last line and it's not a content line, break, otherwise let processing that last loop.139if (is_eof && l.is_empty()) {140if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || status == STATUS_READING_PLURAL) {141ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected EOF while reading POT file at: " + path + ":" + itos(line));142} else {143break;144}145}146147if (l.begins_with("msgctxt")) {148ERR_FAIL_COND_V_MSG(status != STATUS_READING_STRING && status != STATUS_READING_PLURAL, Vector<Vector<String>>(),149"Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));150151// In POT files, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read152// and set "entered_context" to true to prevent adding twice.153if (!msg_id.is_empty()) {154Vector<String> msgs;155msgs.push_back(msg_id);156msgs.push_back(msg_context);157msgs.push_back(msg_id_plural);158list.push_back(msgs);159}160msg_context = "";161l = l.substr(7).strip_edges();162status = STATUS_READING_CONTEXT;163entered_context = true;164}165166if (l.begins_with("msgid_plural")) {167if (status != STATUS_READING_ID) {168ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));169}170l = l.substr(12).strip_edges();171status = STATUS_READING_PLURAL;172} else if (l.begins_with("msgid")) {173ERR_FAIL_COND_V_MSG(status == STATUS_READING_ID, Vector<Vector<String>>(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));174175if (!msg_id.is_empty() && !entered_context) {176Vector<String> msgs;177msgs.push_back(msg_id);178msgs.push_back(msg_context);179msgs.push_back(msg_id_plural);180list.push_back(msgs);181}182183l = l.substr(5).strip_edges();184status = STATUS_READING_ID;185// If we did not encounter msgctxt, we reset context to empty to reset it.186if (!entered_context) {187msg_context = "";188}189msg_id = "";190msg_id_plural = "";191entered_context = false;192}193194if (l.begins_with("msgstr[")) {195ERR_FAIL_COND_V_MSG(status != STATUS_READING_PLURAL, Vector<Vector<String>>(),196"Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));197l = l.substr(9).strip_edges();198} else if (l.begins_with("msgstr")) {199ERR_FAIL_COND_V_MSG(status != STATUS_READING_ID, Vector<Vector<String>>(),200"Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));201l = l.substr(6).strip_edges();202status = STATUS_READING_STRING;203}204205if (l.is_empty() || l.begins_with("#")) {206line++;207continue; // Nothing to read or comment.208}209210ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, Vector<Vector<String>>(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));211212l = l.substr(1);213// Find final quote, ignoring escaped ones (\").214// The escape_next logic is necessary to properly parse things like \\"215// where the backslash is the one being escaped, not the quote.216int end_pos = -1;217bool escape_next = false;218for (int i = 0; i < l.length(); i++) {219if (l[i] == '\\' && !escape_next) {220escape_next = true;221continue;222}223224if (l[i] == '"' && !escape_next) {225end_pos = i;226break;227}228229escape_next = false;230}231232ERR_FAIL_COND_V_MSG(end_pos == -1, Vector<Vector<String>>(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));233234l = l.substr(0, end_pos);235l = l.c_unescape();236237if (status == STATUS_READING_ID) {238msg_id += l;239} else if (status == STATUS_READING_CONTEXT) {240msg_context += l;241} else if (status == STATUS_READING_PLURAL) {242msg_id_plural += l;243}244245line++;246}247}248}249250return list;251}252253254