FlightGear next
XLIFFParser.hxx
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2018 James Turner <james@flightgear.org>
2// SPDX-License-Identifier: GPL-2.0-or-later
3
8
9#pragma once
10
11#include <cstddef>
12#include <memory>
13#include <stack>
14#include <string>
15#include <tuple>
16#include <vector>
17
18#include <simgear/props/propsfwd.hxx>
19#include <simgear/xml/easyxml.hxx>
20
21#include "TranslationDomain.hxx"
23
24namespace flightgear
25{
26
27class XLIFFParser : public XMLVisitor
28{
29public:
30 XLIFFParser(const std::string& languageId, TranslationDomain* domain);
31
32protected:
33 void startXML () override;
34 void endXML () override;
35 void startElement (const char * name, const XMLAttributes &atts) override;
36 void endElement (const char * name) override;
37 void data (const char * s, int len) override;
38 void pi (const char * target, const char * data) override;
39 void warning (const char * message, int line, int column) override;
40
41private:
42 void startTransUnitElement(const XMLAttributes &atts);
43 void finishTransUnit(bool hasPlural);
44 void startContextGroup(const char* resname_c);
45 void endContextGroup();
46 void startPluralGroup(const char* id_c);
47 void endPluralGroup();
48
49 // Return the context (i.e. “resource”), the basic id and the element index
50 // (sibling elements with the same basic id differ by their indices).
51 std::tuple<std::string, std::string, int>
52 parseSimpleTransUnitId(const std::string& id);
53 // If we are inside <group restype="x-gettext-plurals" id="food/banana:0">,
54 // <trans-unit> elements inside this group are plural forms associated to
55 // the same source text; expected values for their 'id' attributes are
56 // 'food/banana:0[0]', 'food/banana:0[1]', etc.
57 void checkIdOfPluralTransUnit(std::string id);
58 void checkNumberOfPluralForms(std::size_t nbPluralFormsInTransUnit);
59
60 const std::string _languageId; // string value of /sim/intl/locale[n]/id
61 TranslationDomain* _domain;
62 std::shared_ptr<TranslationResource> _currentResource;
63
64 std::string _text;
65 std::string _resource, _basicId, _pluralGroupId;
66 int _index, _expectedPluralFormIndex;
67 std::string _sourceText;
68 std::vector<std::string> _targetTexts; // several elements = plural forms
69 // Certain <file> elements must be completely skipped
70 bool _skipElements = false;
71
72 // We'll keep track of the <group> nesting state in a stack containing
73 // std::unique_ptr<Group> instances.
74 enum class GroupType {
75 context,
76 plural
77 };
78
79 class Group {
80 public:
81 Group(GroupType type);
82 virtual ~Group() = default;
83
84 GroupType type;
85
86 protected:
87 Group(const Group&) = default;
88 Group(Group&&) = default;
89 Group& operator=(const Group&) = default;
90 Group& operator=(Group&&) = default;
91 };
92
93 struct ContextGroup final : Group {
94 ContextGroup(const std::string& name);
95 std::string name;
96 };
97
98 struct PluralGroup final : Group {
99 PluralGroup(const std::string& id);
100 std::string id;
101 };
102
103 std::stack<std::unique_ptr<Group>> _groupsStack;
104};
105
106} // namespace flightgear
Container for all TranslationResource's belonging to a domain.
Container class for related translation units.
Class that holds translation resources within a domain.
void data(const char *s, int len) override
void endXML() override
void startXML() override
XLIFFParser(const std::string &languageId, TranslationDomain *domain)
void endElement(const char *name) override
void startElement(const char *name, const XMLAttributes &atts) override
void warning(const char *message, int line, int column) override
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53
const char * name
static const double pi
Definition sview.cxx:59