aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gettext-parser/lib/moparser.js
blob: 8c204716b0b685ebd825d2a4ee683bb8531f657c (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';

var encoding = require('encoding');
var sharedFuncs = require('./shared');

/**
 * Parses a binary MO object into translation table
 *
 * @param {Buffer} buffer Binary MO object
 * @param {String} [defaultCharset] Default charset to use
 * @return {Object} Translation object
 */
module.exports = function(buffer, defaultCharset) {
    var parser = new Parser(buffer, defaultCharset);
    return parser.parse();
};

/**
 * Creates a MO parser object.
 *
 * @constructor
 * @param {Buffer} fileContents Binary MO object
 * @param {String} [defaultCharset] Default charset to use
 */
function Parser(fileContents, defaultCharset) {

    this._fileContents = fileContents;

    /**
     * Method name for writing int32 values, default littleendian
     */
    this._writeFunc = 'writeUInt32LE';

    /**
     * Method name for reading int32 values, default littleendian
     */
    this._readFunc = 'readUInt32LE';

    this._charset = defaultCharset || 'iso-8859-1';

    this._table = {
        charset: this._charset,
        headers: undefined,
        translations: {}
    };
}

/**
 * Magic constant to check the endianness of the input file
 */
Parser.prototype.MAGIC = 0x950412de;

/**
 * Checks if number values in the input file are in big- or littleendian format.
 *
 * @return {Boolean} Return true if magic was detected
 */
Parser.prototype._checkMagick = function() {
    if (this._fileContents.readUInt32LE(0) === this.MAGIC) {
        this._readFunc = 'readUInt32LE';
        this._writeFunc = 'writeUInt32LE';
        return true;
    } else if (this._fileContents.readUInt32BE(0) === this.MAGIC) {
        this._readFunc = 'readUInt32BE';
        this._writeFunc = 'writeUInt32BE';
        return true;
    } else {
        return false;
    }
};

/**
 * Read the original strings and translations from the input MO file. Use the
 * first translation string in the file as the header.
 */
Parser.prototype._loadTranslationTable = function() {
    var offsetOriginals = this._offsetOriginals,
        offsetTranslations = this._offsetTranslations,
        position, length,
        msgid, msgstr;

    for (var i = 0; i < this._total; i++) {
        // msgid string
        length = this._fileContents[this._readFunc](offsetOriginals);
        offsetOriginals += 4;
        position = this._fileContents[this._readFunc](offsetOriginals);
        offsetOriginals += 4;
        msgid = this._fileContents.slice(position, position + length);

        // matching msgstr
        length = this._fileContents[this._readFunc](offsetTranslations);
        offsetTranslations += 4;
        position = this._fileContents[this._readFunc](offsetTranslations);
        offsetTranslations += 4;
        msgstr = this._fileContents.slice(position, position + length);

        if (!i && !msgid.toString()) {
            this._handleCharset(msgstr);
        }

        msgid = encoding.convert(msgid, 'utf-8', this._charset).toString('utf-8');
        msgstr = encoding.convert(msgstr, 'utf-8', this._charset).toString('utf-8');

        this._addString(msgid, msgstr);
    }

    // dump the file contents object
    this._fileContents = null;
};

/**
 * Detects charset for MO strings from the header
 *
 * @param {Buffer} headers Header value
 */
Parser.prototype._handleCharset = function(headers) {

    var headersStr = headers.toString(),
        match;

    if ((match = headersStr.match(/[; ]charset\s*=\s*([\w\-]+)/i))) {
        this._charset = this._table.charset = sharedFuncs.formatCharset(match[1], this._charset);
    }

    headers = encoding.convert(headers, 'utf-8', this._charset).toString('utf-8');

    this._table.headers = sharedFuncs.parseHeader(headers);
};

/**
 * Adds a translation to the translation object
 *
 * @param {String} msgid Original string
 * @params {String} msgstr Translation for the original string
 */
Parser.prototype._addString = function(msgid, msgstr) {
    var translation = {},
        parts, msgctxt, msgid_plural;

    msgid = msgid.split('\u0004');
    if (msgid.length > 1) {
        msgctxt = msgid.shift();
        translation.msgctxt = msgctxt;
    } else {
        msgctxt = '';
    }
    msgid = msgid.join('\u0004');

    parts = msgid.split('\u0000');
    msgid = parts.shift();

    translation.msgid = msgid;

    if ((msgid_plural = parts.join('\u0000'))) {
        translation.msgid_plural = msgid_plural;
    }

    msgstr = msgstr.split('\u0000');
    translation.msgstr = [].concat(msgstr || []);

    if (!this._table.translations[msgctxt]) {
        this._table.translations[msgctxt] = {};
    }

    this._table.translations[msgctxt][msgid] = translation;
};

/**
 * Parses the MO object and returns translation table
 *
 * @return {Object} Translation table
 */
Parser.prototype.parse = function() {
    if (!this._checkMagick()) {
        return false;
    }

    /**
     * GetText revision nr, usually 0
     */
    this._revision = this._fileContents[this._readFunc](4);

    /**
     * Total count of translated strings
     */
    this._total = this._fileContents[this._readFunc](8);

    /**
     * Offset position for original strings table
     */
    this._offsetOriginals = this._fileContents[this._readFunc](12);

    /**
     * Offset position for translation strings table
     */
    this._offsetTranslations = this._fileContents[this._readFunc](16);

    // Load translations into this._translationTable
    this._loadTranslationTable();

    return this._table;
};