106 lines
3.3 KiB
JavaScript
106 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
var POParser = require('po-parser');
|
|
var plugin = require('./plugin-B0KIcFlc.cjs');
|
|
var ExtractorCodec = require('./ExtractorCodec-D9Tw618d.cjs');
|
|
require('fs/promises');
|
|
require('path');
|
|
require('@parcel/watcher');
|
|
require('fs');
|
|
require('module');
|
|
require('@swc/core');
|
|
|
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
|
|
var POParser__default = /*#__PURE__*/_interopDefaultCompat(POParser);
|
|
|
|
var POCodec = ExtractorCodec.defineCodec(() => {
|
|
// See also https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html
|
|
const DEFAULT_METADATA = {
|
|
// Recommended by spec
|
|
'Content-Type': 'text/plain; charset=utf-8',
|
|
'Content-Transfer-Encoding': '8bit',
|
|
// Otherwise other tools might set this
|
|
'X-Generator': 'next-intl',
|
|
// Crowdin defaults to using msgid as source key
|
|
'X-Crowdin-SourceKey': 'msgstr'
|
|
};
|
|
|
|
// Move all parts before the last dot to msgctxt
|
|
const NAMESPACE_SEPARATOR = '.';
|
|
|
|
// Metadata is stored so it can be retained when writing
|
|
const metadataByLocale = new Map();
|
|
return {
|
|
decode(content, context) {
|
|
const catalog = POParser__default.default.parse(content);
|
|
if (catalog.meta) {
|
|
metadataByLocale.set(context.locale, catalog.meta);
|
|
}
|
|
const messages = catalog.messages || [];
|
|
return messages.map(msg => {
|
|
const {
|
|
extractedComments,
|
|
msgctxt,
|
|
msgid,
|
|
msgstr,
|
|
...rest
|
|
} = msg;
|
|
if (extractedComments && extractedComments.length > 1) {
|
|
throw new Error(`Multiple extracted comments are not supported. Found ${extractedComments.length} comments for msgid "${msgid}".`);
|
|
}
|
|
return {
|
|
...rest,
|
|
id: msgctxt ? [msgctxt, msgid].join(NAMESPACE_SEPARATOR) : msgid,
|
|
message: msgstr,
|
|
...(extractedComments && extractedComments.length > 0 && {
|
|
description: extractedComments[0]
|
|
})
|
|
};
|
|
});
|
|
},
|
|
encode(messages, context) {
|
|
const encodedMessages = plugin.getSortedMessages(messages).map(msg => {
|
|
const {
|
|
description,
|
|
id,
|
|
message,
|
|
...rest
|
|
} = msg;
|
|
const lastDotIndex = id.lastIndexOf(NAMESPACE_SEPARATOR);
|
|
const hasNamespace = id.includes(NAMESPACE_SEPARATOR);
|
|
const msgid = hasNamespace ? id.slice(lastDotIndex + NAMESPACE_SEPARATOR.length) : id;
|
|
return {
|
|
msgid,
|
|
msgstr: message,
|
|
...(description && {
|
|
extractedComments: [description]
|
|
}),
|
|
...(hasNamespace && {
|
|
msgctxt: id.slice(0, lastDotIndex)
|
|
}),
|
|
...rest
|
|
};
|
|
});
|
|
return POParser__default.default.serialize({
|
|
meta: {
|
|
Language: context.locale,
|
|
...DEFAULT_METADATA,
|
|
...metadataByLocale.get(context.locale)
|
|
},
|
|
messages: encodedMessages
|
|
});
|
|
},
|
|
toJSONString(source, context) {
|
|
const parsed = this.decode(source, context);
|
|
const messagesObject = {};
|
|
for (const message of parsed) {
|
|
plugin.setNestedProperty(messagesObject, message.id, message.message);
|
|
}
|
|
return JSON.stringify(messagesObject);
|
|
}
|
|
};
|
|
});
|
|
|
|
exports.default = POCodec;
|