Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erikyo committed May 10, 2024
1 parent 7f94d00 commit 85121d6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npx tsc
- run: npm test
1 change: 0 additions & 1 deletion src/moparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ function Parser (fileContents, defaultCharset = 'iso-8859-1') {
* Translation table
*
* @type {import('./types.js').GetTextTranslations} table Translation object
* @private
*/
this._table = {
charset: this._charset,
Expand Down
47 changes: 30 additions & 17 deletions src/pocompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,46 @@ export default function (table, options) {
}

/**
* Creates a PO compiler object.
* Takes the header object and converts all headers into the lowercase format
*
* @constructor
* @param {import('./types.js').GetTextTranslations|{}} table Translation table to be compiled
* @param {import('./types.js').parserOptions|{}} [options] Options
* @param {{ [x: string]: any; }} headersRaw the headers to prepare
* @returns {{ [x: string]: any; }} the headers in the lowercase format
*/
function Compiler (table = {}, options = {}) {
this._table = table;
this._options = options;

this._table.translations = this._table.translations || {};

let { headers = {} } = this._table;

headers = Object.keys(headers).reduce((result, key) => {
function prepareHeaders(headersRaw) {
return Object.keys(headersRaw).reduce((/** @type {{ [x: string]: any; }} */ result, key) => {
const lowerKey = key.toLowerCase();
const value = HEADERS.get(lowerKey)

if (HEADERS.has(lowerKey)) {
result[HEADERS.get(lowerKey)] = headers[key];
if (typeof value === 'string') {
result[value] = headersRaw[key];
} else {
result[key] = headers[key];
result[key] = headersRaw[key];
}

return result;
}, {});
}

this._table.headers = headers;
/**
* Creates a PO compiler object.
*
* @constructor
* @param {import('./types.js').GetTextTranslations} [table] Translation table to be compiled
* @param {import('./types.js').parserOptions} [options] Options
*/
function Compiler (table , options ) {
/** @type {import('./types.js').parserOptions} _options the parser options object */
this._options = options ?? {};
/**
* The compiled translation table
* @type {import('./types.js').GetTextTranslations} _table Translation table
*/
this._table = table ?? { translations: {}, charset: (this._options && this._options.defaultCharset ? this._options.defaultCharset : 'utf-8') };
/**
* Headers of the PO file
* @type {Headers} The Headers of the PO file
*/
this._table.headers = this._table.headers ?? {};

if (!('foldLength' in this._options)) {
this._options.foldLength = 76;
Expand Down
15 changes: 11 additions & 4 deletions src/poparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,14 @@ Parser.prototype._handleKeys = function (tokens) {
/**
* Separate different values into individual translation objects
*
* @param {Object} tokens Parsed tokens
* @return {Object} Tokens
* @param {import("./types.js").GetTextTranslation[]} tokens Parsed tokens
* @return {import("./types.js").GetTextTranslations} Tokens
*/
Parser.prototype._handleValues = function (tokens) {
const response = [];
/**
* @type {import("./types.js").GetTextTranslation}
*/
let lastNode;
let curContext;
let curComments;
Expand Down Expand Up @@ -445,10 +448,14 @@ Parser.prototype._validateToken = function (
/**
* Compose a translation table from tokens object
*
* @param {Object} tokens Parsed tokens
* @return {Object} Translation table
* @param {import("./types.js").GetTextTranslation[]} tokens Parsed tokens
* @return {import("./types.js").GetTextTranslations} Translation table
*/
Parser.prototype._normalize = function (tokens) {
/**
* Translation table to be returned
* @type {import("./types.js").GetTextTranslations} Translation table
*/
const table = {
charset: this._charset,
headers: undefined,
Expand Down
12 changes: 8 additions & 4 deletions src/shared.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// see https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html
/** @type {string} Header name for "Plural-Forms" */
const PLURAL_FORMS = 'Plural-Forms';
/** @typedef {Map<string, string>} Headers Map of header keys to header names */
export const HEADERS = new Map([
['project-id-version', 'Project-Id-Version'],
['report-msgid-bugs-to', 'Report-Msgid-Bugs-To'],
Expand All @@ -22,8 +24,10 @@ const PLURAL_FORM_HEADER_NPLURALS_REGEX = /nplurals\s*=\s*(?<nplurals>\d+)/;
* @return {Object} An object of key-value pairs
*/
export function parseHeader (str = '') {
return str.split('\n')
.reduce((headers, line) => {
/** @type {string} Header string */
return str
.split('\n')
.reduce((/** @type {Record<string, string>} */ headers , line) => {
const parts = line.split(':');
let key = (parts.shift() || '').trim();

Expand All @@ -42,7 +46,7 @@ export function parseHeader (str = '') {
/**
* Attempts to safely parse 'nplurals" value from "Plural-Forms" header
*
* @param {Object} [headers = {}] An object with parsed headers
* @param {{[key: string]: string}} [headers] An object with parsed headers
* @param {number} fallback Fallback value if "Plural-Forms" header is absent
* @returns {number} Parsed result
*/
Expand All @@ -63,7 +67,7 @@ export function parseNPluralFromHeadersSafely (headers, fallback = 1) {
/**
* Joins a header object of key value pairs into a header string
*
* @param {Object} header Object of key value pairs
* @param {{[key: string]: string}} header Object of key value pairs
* @return {String} Header string
*/
export function generateHeader (header = {}) {
Expand Down
8 changes: 7 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
* @property {string} [msgid_plural] The plural message ID.
* @property {string[]} msgstr Array of translated strings.
* @property {GetTextComment} [comments] Comments associated with the translation.
* @property {GetTextTranslation} [obsolete] Whether the translation is obsolete.
*/

/**
* Represents GetText translations.
* @typedef {Object} GetTextTranslations
* @property {string} charset Character set.
* @property {Object.<string, string>} headers Headers.
* @property {Object.<string, string>} [headers] Headers.
* @property {Object.<string, string>} [obsolete] Obsolete messages.
* @property {Object.<string, Object.<string, GetTextTranslation>>} translations Translations.
*/

Expand All @@ -31,4 +33,8 @@
* @typedef {Object} parserOptions
* @property {string} [defaultCharset] Default character set.
* @property {boolean} [validation] Whether to perform validation.
* @property {number} [foldLength] the fold length.
* @property {boolean} [escapeCharacters] Whether to escape characters.
* @property {boolean} [sort] Whether to sort messages.
* @property {string} [eol] End of line character.
*/
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"allowJs": true,

// Check js files for errors
"checkJs": false,
"checkJs": true,

// the directory sources are in
"rootDir": "src",
Expand Down

0 comments on commit 85121d6

Please sign in to comment.