/**************************************************************************/1/* translation_po.h */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#pragma once3132//#define DEBUG_TRANSLATION_PO3334#include "core/math/expression.h"35#include "core/string/translation.h"3637class TranslationPO : public Translation {38GDCLASS(TranslationPO, Translation);3940// TLDR: Maps context to a list of source strings and translated strings. In PO terms, maps msgctxt to a list of msgid and msgstr.41// The first key corresponds to context, and the second key (of the contained HashMap) corresponds to source string.42// The value Vector<StringName> in the second map stores the translated strings. Index 0, 1, 2 matches msgstr[0], msgstr[1], msgstr[2]... in the case of plurals.43// Otherwise index 0 matches to msgstr in a singular translation.44// Strings without context have "" as first key.45HashMap<StringName, HashMap<StringName, Vector<StringName>>> translation_map;4647int plural_forms = 0; // 0 means no "Plural-Forms" is given in the PO header file. The min for all languages is 1.48String plural_rule;4950// Cache temporary variables related to _get_plural_index() to make it faster51class EQNode : public RefCounted {52public:53String regex;54Ref<EQNode> left;55Ref<EQNode> right;56};57Ref<EQNode> equi_tests;5859int _find_unquoted(const String &p_src, char32_t p_chr) const;60int _eq_test(const Ref<EQNode> &p_node, const Variant &p_result) const;6162Vector<String> input_name;63mutable Ref<Expression> expr;64mutable Array input_val;65mutable StringName last_plural_key;66mutable StringName last_plural_context;67mutable int last_plural_n = -1; // Set it to an impossible value at the beginning.68mutable int last_plural_mapped_index = 0;6970void _cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node);71int _get_plural_index(int p_n) const;7273Vector<String> _get_message_list() const override;74Dictionary _get_messages() const override;75void _set_messages(const Dictionary &p_messages) override;7677protected:78static void _bind_methods();7980public:81Vector<String> get_translated_message_list() const override;82void get_message_list(List<StringName> *r_messages) const override;83int get_message_count() const override;84void add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context = "") override;85void add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context = "") override;86StringName get_message(const StringName &p_src_text, const StringName &p_context = "") const override;87StringName get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context = "") const override;88void erase_message(const StringName &p_src_text, const StringName &p_context = "") override;8990void set_plural_rule(const String &p_plural_rule);91int get_plural_forms() const;92String get_plural_rule() const;9394#ifdef DEBUG_TRANSLATION_PO95void print_translation_map();96#endif9798TranslationPO() {}99};100101102