| 1 |
import encoding from 'encoding'; |
| 2 |
import { formatCharset, parseHeader } from './shared.js'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Parses a binary MO object into translation table |
| 6 |
* |
| 7 |
* @param {Buffer} buffer Binary MO object |
| 8 |
* @param {String} [defaultCharset] Default charset to use |
| 9 |
* @return {Object} Translation object |
| 10 |
*/ |
| 11 |
export default function (buffer, defaultCharset) { |
| 12 |
const parser = new Parser(buffer, defaultCharset); |
| 13 |
|
| 14 |
return parser.parse(); |
| 15 |
}; |
| 16 |
|
| 17 |
/** |
| 18 |
* Creates a MO parser object. |
| 19 |
* |
| 20 |
* @constructor |
| 21 |
* @param {Buffer} fileContents Binary MO object |
| 22 |
* @param {String} [defaultCharset] Default charset to use |
| 23 |
*/ |
| 24 |
function Parser (fileContents, defaultCharset = 'iso-8859-1') { |
| 25 |
this._fileContents = fileContents; |
| 26 |
|
| 27 |
/** |
| 28 |
* Method name for writing int32 values, default littleendian |
| 29 |
*/ |
| 30 |
this._writeFunc = 'writeUInt32LE'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Method name for reading int32 values, default littleendian |
| 34 |
*/ |
| 35 |
this._readFunc = 'readUInt32LE'; |
| 36 |
|
| 37 |
this._charset = defaultCharset; |
| 38 |
|
| 39 |
this._table = { |
| 40 |
charset: this._charset, |
| 41 |
headers: undefined, |
| 42 |
translations: {} |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Magic constant to check the endianness of the input file |
| 48 |
*/ |
| 49 |
Parser.prototype.MAGIC = 0x950412de; |
| 50 |
|
| 51 |
/** |
| 52 |
* Checks if number values in the input file are in big- or littleendian format. |
| 53 |
* |
| 54 |
* @return {Boolean} Return true if magic was detected |
| 55 |
*/ |
| 56 |
Parser.prototype._checkMagick = function () { |
| 57 |
if (this._fileContents.readUInt32LE(0) === this.MAGIC) { |
| 58 |
this._readFunc = 'readUInt32LE'; |
| 59 |
this._writeFunc = 'writeUInt32LE'; |
| 60 |
|
| 61 |
return true; |
| 62 |
} else if (this._fileContents.readUInt32BE(0) === this.MAGIC) { |
| 63 |
this._readFunc = 'readUInt32BE'; |
| 64 |
this._writeFunc = 'writeUInt32BE'; |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
return false; |
| 70 |
}; |
| 71 |
|
| 72 |
/** |
| 73 |
* Read the original strings and translations from the input MO file. Use the |
| 74 |
* first translation string in the file as the header. |
| 75 |
*/ |
| 76 |
Parser.prototype._loadTranslationTable = function () { |
| 77 |
let offsetOriginals = this._offsetOriginals; |
| 78 |
let offsetTranslations = this._offsetTranslations; |
| 79 |
let position; |
| 80 |
let length; |
| 81 |
let msgid; |
| 82 |
let msgstr; |
| 83 |
|
| 84 |
for (let i = 0; i < this._total; i++) { |
| 85 |
// msgid string |
| 86 |
length = this._fileContents[this._readFunc](offsetOriginals); |
| 87 |
offsetOriginals += 4; |
| 88 |
position = this._fileContents[this._readFunc](offsetOriginals); |
| 89 |
offsetOriginals += 4; |
| 90 |
msgid = this._fileContents.slice(position, position + length); |
| 91 |
|
| 92 |
// matching msgstr |
| 93 |
length = this._fileContents[this._readFunc](offsetTranslations); |
| 94 |
offsetTranslations += 4; |
| 95 |
position = this._fileContents[this._readFunc](offsetTranslations); |
| 96 |
offsetTranslations += 4; |
| 97 |
msgstr = this._fileContents.slice(position, position + length); |
| 98 |
|
| 99 |
if (!i && !msgid.toString()) { |
| 100 |
this._handleCharset(msgstr); |
| 101 |
} |
| 102 |
|
| 103 |
msgid = encoding.convert(msgid, 'utf-8', this._charset) |
| 104 |
.toString('utf8'); |
| 105 |
msgstr = encoding.convert(msgstr, 'utf-8', this._charset) |
| 106 |
.toString('utf8'); |
| 107 |
|
| 108 |
this._addString(msgid, msgstr); |
| 109 |
} |
| 110 |
|
| 111 |
// dump the file contents object |
| 112 |
this._fileContents = null; |
| 113 |
}; |
| 114 |
|
| 115 |
/** |
| 116 |
* Detects charset for MO strings from the header |
| 117 |
* |
| 118 |
* @param {Buffer} headers Header value |
| 119 |
*/ |
| 120 |
Parser.prototype._handleCharset = function (headers) { |
| 121 |
const headersStr = headers.toString(); |
| 122 |
let match; |
| 123 |
|
| 124 |
if ((match = headersStr.match(/[; ]charset\s*=\s*([\w-]+)/i))) { |
| 125 |
this._charset = this._table.charset = formatCharset(match[1], this._charset); |
| 126 |
} |
| 127 |
|
| 128 |
headers = encoding.convert(headers, 'utf-8', this._charset) |
| 129 |
.toString('utf8'); |
| 130 |
|
| 131 |
this._table.headers = parseHeader(headers); |
| 132 |
}; |
| 133 |
|
| 134 |
/** |
| 135 |
* Adds a translation to the translation object |
| 136 |
* |
| 137 |
* @param {String} msgid Original string |
| 138 |
* @params {String} msgstr Translation for the original string |
| 139 |
*/ |
| 140 |
Parser.prototype._addString = function (msgid, msgstr) { |
| 141 |
const translation = {}; |
| 142 |
let msgctxt; |
| 143 |
let msgidPlural; |
| 144 |
|
| 145 |
msgid = msgid.split('\u0004'); |
| 146 |
if (msgid.length > 1) { |
| 147 |
msgctxt = msgid.shift(); |
| 148 |
translation.msgctxt = msgctxt; |
| 149 |
} else { |
| 150 |
msgctxt = ''; |
| 151 |
} |
| 152 |
msgid = msgid.join('\u0004'); |
| 153 |
|
| 154 |
const parts = msgid.split('\u0000'); |
| 155 |
msgid = parts.shift(); |
| 156 |
|
| 157 |
translation.msgid = msgid; |
| 158 |
|
| 159 |
if ((msgidPlural = parts.join('\u0000'))) { |
| 160 |
translation.msgid_plural = msgidPlural; |
| 161 |
} |
| 162 |
|
| 163 |
msgstr = msgstr.split('\u0000'); |
| 164 |
translation.msgstr = [].concat(msgstr || []); |
| 165 |
|
| 166 |
if (!Object.hasOwn(this._table.translations, msgctxt)) { |
| 167 |
Object.defineProperty(this._table.translations, msgctxt, { value: {}, writable: true, enumerable: true, configurable: true }); |
| 168 |
} |
| 169 |
|
| 170 |
Object.defineProperty(this._table.translations[msgctxt], msgid, { value: translation, writable: true, enumerable: true, configurable: true }); |
| 171 |
}; |
| 172 |
|
| 173 |
/** |
| 174 |
* Parses the MO object and returns translation table |
| 175 |
* |
| 176 |
* @return {Object} Translation table |
| 177 |
*/ |
| 178 |
Parser.prototype.parse = function () { |
| 179 |
if (!this._checkMagick()) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* GetText revision nr, usually 0 |
| 185 |
*/ |
| 186 |
this._revision = this._fileContents[this._readFunc](4); |
| 187 |
|
| 188 |
/** |
| 189 |
* Total count of translated strings |
| 190 |
*/ |
| 191 |
this._total = this._fileContents[this._readFunc](8); |
| 192 |
|
| 193 |
/** |
| 194 |
* Offset position for original strings table |
| 195 |
*/ |
| 196 |
this._offsetOriginals = this._fileContents[this._readFunc](12); |
| 197 |
|
| 198 |
/** |
| 199 |
* Offset position for translation strings table |
| 200 |
*/ |
| 201 |
this._offsetTranslations = this._fileContents[this._readFunc](16); |
| 202 |
|
| 203 |
// Load translations into this._translationTable |
| 204 |
this._loadTranslationTable(); |
| 205 |
|
| 206 |
return this._table; |
| 207 |
}; |