summaryrefslogtreecommitdiffstats
path: root/tools/localize/xmb.cpp
blob: 236705f186d9dae6206a9850a4a290b58897805b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "xmb.h"

#include "file_utils.h"
#include "localize.h"
#include "ValuesFile.h"
#include "XMLHandler.h"
#include "XLIFFFile.h"

#include <map>

using namespace std;

const char *const NS_MAP[] = {
    "xml", XMLNS_XMLNS,
    NULL, NULL
};

set<string> g_tags;

static string
strip_newlines(const string& str)
{
    string res;
    const size_t N = str.length();
    for (size_t i=0; i<N; i++) {
        char c = str[i];
        if (c != '\n' && c != '\r') {
            res += c;
        } else {
            res += ' ';
        }
    }
    return res;
}

static int
rename_id_attribute(XMLNode* node)
{
    vector<XMLAttribute>& attrs = node->EditAttributes();
    const size_t I = attrs.size();
    for (size_t i=0; i<I; i++) {
        XMLAttribute attr = attrs[i];
        if (attr.name == "id") {
            attr.name = "name";
            attrs.erase(attrs.begin()+i);
            attrs.push_back(attr);
            return 0;
        }
    }
    return 1;
}

static int
convert_xliff_to_ph(XMLNode* node, int* phID)
{
    int err = 0;
    if (node->Type() == XMLNode::ELEMENT) {
        if (node->Namespace() == XLIFF_XMLNS) {
            g_tags.insert(node->Name());
            node->SetName("", "ph");

            err = rename_id_attribute(node);
            if (err != 0) {
                char name[30];
                (*phID)++;
                sprintf(name, "id-%d", *phID);
                node->EditAttributes().push_back(XMLAttribute("", "name", name));
                err = 0;
            }
        }
        vector<XMLNode*>& children = node->EditChildren();
        const size_t I = children.size();
        for (size_t i=0; i<I; i++) {
            err |= convert_xliff_to_ph(children[i], phID);
        }
    }
    return err;
}

XMLNode*
resource_to_xmb_msg(const StringResource& res)
{
    // the msg element
    vector<XMLAttribute> attrs;
    string name = res.pos.file;
    name += ":";
    name += res.TypedID();
    attrs.push_back(XMLAttribute("", "name", name));
    attrs.push_back(XMLAttribute("", "desc", strip_newlines(res.comment)));
    attrs.push_back(XMLAttribute(XMLNS_XMLNS, "space", "preserve"));
    XMLNode* msg = XMLNode::NewElement(res.pos, "", "msg", attrs, XMLNode::EXACT);

    // the contents are in xliff/html, convert it to xliff
    int err = 0;
    XMLNode* value = res.value;
    string tag = value->Name();
    int phID = 0;
    for (vector<XMLNode*>::const_iterator it=value->Children().begin();
            it!=value->Children().end(); it++) {
        err |= convert_html_to_xliff(*it, tag, msg, &phID);
    }

    if (err != 0) {
        return NULL;
    }

    // and then convert that to xmb
    for (vector<XMLNode*>::iterator it=msg->EditChildren().begin();
            it!=msg->EditChildren().end(); it++) {
        err |= convert_xliff_to_ph(*it, &phID);
    }

    if (err == 0) {
        return msg;
    } else {
        return NULL;
    }
}

int
do_xlb_export(const string& outfile, const vector<string>& resFiles)
{
    int err = 0;

    size_t totalFileCount = resFiles.size();

    Configuration english;
        english.locale = "en_US";

    set<StringResource> allResources;

    const size_t J = resFiles.size();
    for (size_t j=0; j<J; j++) {
        string resFile = resFiles[j];

        ValuesFile* valuesFile = get_local_values_file(resFile, english, CURRENT_VERSION, "", true);
        if (valuesFile != NULL) {
            set<StringResource> resources = valuesFile->GetStrings();
            allResources.insert(resources.begin(), resources.end());
        } else {
            fprintf(stderr, "error reading file %s\n", resFile.c_str());
        }

        delete valuesFile;
    }

    // Construct the XLB xml
    vector<XMLAttribute> attrs;
    attrs.push_back(XMLAttribute("", "locale", "en"));
    XMLNode* localizationbundle = XMLNode::NewElement(GENERATED_POS, "", "localizationbundle",
            attrs, XMLNode::PRETTY);

    for (set<StringResource>::iterator it=allResources.begin(); it!=allResources.end(); it++) {
        XMLNode* msg = resource_to_xmb_msg(*it);
        if (msg) {
            localizationbundle->EditChildren().push_back(msg);
        } else {
            err = 1;
        }
    }

#if 0
    for (set<string>::iterator it=g_tags.begin(); it!=g_tags.end(); it++) {
        printf("tag: %s\n", it->c_str());
    }
    printf("err=%d\n", err);
#endif
    if (err == 0) {
        FILE* f = fopen(outfile.c_str(), "wb");
        if (f == NULL) {
            fprintf(stderr, "can't open outputfile: %s\n", outfile.c_str());
            return 1;
        }
        fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        fprintf(f, "%s\n", localizationbundle->ToString(NS_MAP).c_str());
        fclose(f);
    }

    return err;
}