Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v8.1.0 Update #86

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"es2021": true,
"node": true
},
"extends": "standard",
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/node_modules
/lib
/@types
npm-debug.log
.DS_Store
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/test
/src
.eslintrc.js
.gitignore
17 changes: 0 additions & 17 deletions index.js

This file was deleted.

20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,36 @@
"node": ">=18"
},
"scripts": {
"lint": "eslint lib/*.js test/*.js index.js",
"lint": "eslint src/*.js test/*.js",
"test-generate-mo": "msgfmt test/fixtures/latin13.po -o test/fixtures/latin13.mo & msgfmt test/fixtures/utf8.po -o test/fixtures/utf8.mo & msgfmt test/fixtures/obsolete.po -o test/fixtures/obsolete.mo",
"test": "mocha",
"preversion": "npm run lint && npm test",
"postversion": "git push && git push --tags"
"postversion": "git push && git push --tags",
"prepublishOnly": "npm i && tsc && npm run lint && npm run test"
},
"main": "./index.js",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
erikyo marked this conversation as resolved.
Show resolved Hide resolved
"license": "MIT",
"dependencies": {
"content-type": "^1.0.5",
"encoding": "^0.1.13",
"readable-stream": "^4.5.2",
"safe-buffer": "^5.2.1"
"readable-stream": "^4.5.2"
},
"devDependencies": {
"@types/chai": "latest",
"@types/content-type": "^1.1.8",
"@types/mocha": "latest",
"@types/readable-stream": "^4.0.11",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.14.0",
"chai": "^5.0.3",
"eslint": "^8.56.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.1.1",
"mocha": "^10.3.0"
"mocha": "^10.3.0",
"typescript": "^5.4.5"
},
"keywords": [
"i18n",
Expand Down
52 changes: 52 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Transform } from "readable-stream";

export declare module 'encoding' {
erikyo marked this conversation as resolved.
Show resolved Hide resolved
export function convert(buf: Buffer, toCharset: string, fromCharset: string): Buffer;
}

export interface Compiler {
_table: GetTextTranslations;
compile(): Buffer;
}

export interface GetTextComment {
translator?: string;
reference?: string;
extracted?: string;
flag?: string;
previous?: string;
}

export interface GetTextTranslation {
msgctxt?: string;
msgid: string;
msgid_plural?: string;
msgstr: string[];
comments?: GetTextComment;
}

export interface GetTextTranslations {
charset: string;
headers: { [headerName: string]: string };
translations: { [msgctxt: string]: { [msgId: string]: GetTextTranslation } };
}

export interface parserOptions {
defaultCharset?: string;
validation?: boolean;
}

export interface po {
parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations;
compile: (table: GetTextTranslations, options?: parserOptions) => Buffer;
createParseStream: (options?: parserOptions, transformOptions?: import('readable-stream').TransformOptions) => Transform;
}

export interface mo {
parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations;
compile: (table: GetTextTranslations, options?: parserOptions) => Buffer;
}

export * from "./@types";
erikyo marked this conversation as resolved.
Show resolved Hide resolved

export default { po, mo } as { po: po, mo: mo };
29 changes: 29 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as poParser from './poparser.js';
import poCompiler from './pocompiler.js';
import moParser from './moparser.js';
import moCompiler from './mocompiler.js';

/**
* Translation parser and compiler for PO files
* @see https://www.gnu.org/software/gettext/manual/html_node/PO.html
*
* @type {import("./index.d.ts").po} po
*/
export const po = {
parse: poParser.parse,
createParseStream: poParser.stream,
compile: poCompiler
};

/**
* Translation parser and compiler for PO files
* @see https://www.gnu.org/software/gettext/manual/html_node/MO.html
*
* @type {import("./index.d.ts").mo} mo
*/
export const mo = {
parse: moParser,
compile: moCompiler
};

export default { mo, po };
12 changes: 6 additions & 6 deletions lib/mocompiler.js → src/mocompiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Buffer } from 'safe-buffer';
import encoding from 'encoding';
import { HEADERS, formatCharset, generateHeader, compareMsgid } from './shared.js';
import contentType from 'content-type';
Expand All @@ -7,20 +6,21 @@ import contentType from 'content-type';
* Exposes general compiler function. Takes a translation
* object as a parameter and returns binary MO object
*
* @param {Object} table Translation object
* @param {import('./index.d.ts').GetTextTranslations} table Translation object
* @return {Buffer} Compiled binary MO object
*/
export default function (table) {
const compiler = new Compiler(table);

return compiler.compile();
};
}

/**
* Creates a MO compiler object.
*
* @constructor
* @param {Object} table Translation table as defined in the README
* @param {import('./index.d.ts').GetTextTranslations} table Translation table as defined in the README
* @return {import('./index.d.ts').Compiler} Compiler
*/
function Compiler (table = {}) {
this._table = table;
Expand Down Expand Up @@ -148,7 +148,7 @@ Compiler.prototype._generateList = function () {
/**
* Calculate buffer size for the final binary object
*
* @param {Array} list An array of translation strings from _generateList
* @param {import('./index.d.ts').GetTextTranslations} list An array of translation strings from _generateList
* @return {Object} Size data of {msgid, msgstr, total}
*/
Compiler.prototype._calculateSize = function (list) {
Expand Down Expand Up @@ -183,7 +183,7 @@ Compiler.prototype._calculateSize = function (list) {
/**
* Generates the binary MO object from the translation list
*
* @param {Array} list translation list
* @param {import('./index.d.ts').GetTextTranslations} list translation list
* @param {Object} size Byte size information
* @return {Buffer} Compiled MO object
*/
Expand Down
12 changes: 9 additions & 3 deletions lib/moparser.js → src/moparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function (buffer, defaultCharset) {
const parser = new Parser(buffer, defaultCharset);

return parser.parse();
};
}

/**
* Creates a MO parser object.
Expand Down Expand Up @@ -87,14 +87,20 @@ Parser.prototype._loadTranslationTable = function () {
offsetOriginals += 4;
position = this._fileContents[this._readFunc](offsetOriginals);
offsetOriginals += 4;
msgid = this._fileContents.slice(position, position + length);
msgid = this._fileContents.subarray(
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);
msgstr = this._fileContents.subarray(
position,
position + length,
);

if (!i && !msgid.toString()) {
this._handleCharset(msgstr);
Expand Down
3 changes: 1 addition & 2 deletions lib/pocompiler.js → src/pocompiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Buffer } from 'safe-buffer';
import encoding from 'encoding';
import { HEADERS, foldLine, compareMsgid, formatCharset, generateHeader } from './shared.js';
import contentType from 'content-type';
Expand All @@ -14,7 +13,7 @@ export default function (table, options) {
const compiler = new Compiler(table, options);

return compiler.compile();
};
}

/**
* Creates a PO compiler object.
Expand Down
32 changes: 15 additions & 17 deletions lib/poparser.js → src/poparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ export function parse (input, options = {}) {
const parser = new Parser(input, options);

return parser.parse();
};
}

/**
* Parses a PO stream, emits translation table in object mode
*
* @typedef {{ defaultCharset: strubg, validation: boolean }} Options
* @typedef {{ defaultCharset: string, validation: boolean }} Options
* @param {Options} [options] Optional options with defaultCharset and validation
* @param {import('readable-stream').TransformOptions} [transformOptions] Optional stream options
*/
export function stream (options = {}, transformOptions = {}) {
return new PoParserTransform(options, transformOptions);
};
}

/**
* Creates a PO parser object. If PO object is a string,
Expand Down Expand Up @@ -77,8 +77,8 @@ Parser.prototype._handleCharset = function (buf = '') {
let match;

if ((pos = str.search(/^\s*msgid/im)) >= 0) {
pos = pos + str.substr(pos + 5).search(/^\s*(msgid|msgctxt)/im);
headers = str.substr(0, pos >= 0 ? pos + 5 : str.length);
pos = pos + str.substring(pos + 5).search(/^\s*(msgid|msgctxt)/im);
headers = str.substring(0, pos >= 0 ? pos + 5 : str.length);
}

if ((match = headers.match(/[; ]charset\s*=\s*([\w-]+)(?:[\s;]|\\n)*"\s*$/mi))) {
Expand Down Expand Up @@ -121,8 +121,6 @@ Parser.prototype.types = {
* String matches for lexer
*/
Parser.prototype.symbols = {
quotes: /["']/,
comments: /#/,
whitespace: /\s/,
key: /[\w\-[\]]/,
keyNames: /^(?:msgctxt|msgid(?:_plural)?|msgstr(?:\[\d+])?)$/
Expand All @@ -146,15 +144,15 @@ Parser.prototype._lexer = function (chunk) {
switch (this._state) {
case this.states.none:
case this.states.obsolete:
if (chr.match(this.symbols.quotes)) {
if (chr === '"' || chr === "'") {
this._node = {
type: this.types.string,
value: '',
quote: chr
};
this._lex.push(this._node);
this._state = this.states.string;
} else if (chr.match(this.symbols.comments)) {
} else if (chr === "#") {
this._node = {
type: this.types.comments,
value: ''
Expand Down Expand Up @@ -279,16 +277,16 @@ Parser.prototype._parseComments = function (tokens) {
lines.forEach(line => {
switch (line.charAt(0) || '') {
case ':':
comment.reference.push(line.substr(1).trim());
comment.reference.push(line.substring(1).trim());
break;
case '.':
comment.extracted.push(line.substr(1).replace(/^\s+/, ''));
comment.extracted.push(line.substring(1).replace(/^\s+/, ''));
break;
case ',':
comment.flag.push(line.substr(1).replace(/^\s+/, ''));
comment.flag.push(line.substring(1).replace(/^\s+/, ''));
break;
case '|':
comment.previous.push(line.substr(1).replace(/^\s+/, ''));
comment.previous.push(line.substring(1).replace(/^\s+/, ''));
break;
case '~':
break;
Expand Down Expand Up @@ -392,7 +390,7 @@ Parser.prototype._handleValues = function (tokens) {

curContext = false;
curComments = false;
} else if (tokens[i].key.substr(0, 6).toLowerCase() === 'msgstr') {
} else if (tokens[i].key.substring(0, 6).toLowerCase() === 'msgstr') {
if (lastNode) {
lastNode.msgstr = (lastNode.msgstr || []).concat(tokens[i].value);
}
Expand Down Expand Up @@ -515,7 +513,7 @@ Parser.prototype._finalize = function (tokens) {
/**
* Creates a transform stream for parsing PO input
*
* @typedef {{ defaultCharset: strubg, validation: boolean }} Options
* @typedef {{ defaultCharset: string, validation: boolean }} Options
* @constructor
* @param {Options} options Optional options with defaultCharset and validation
* @param {import('readable-stream').TransformOptions} transformOptions Optional stream options
Expand Down Expand Up @@ -581,9 +579,9 @@ PoParserTransform.prototype._transform = function (chunk, encoding, done) {
}
// it seems we found some 8bit bytes from the end of the string, so let's cache these
if (len) {
this._cache = [chunk.slice(chunk.length - len)];
this._cache = [chunk.subarray(chunk.length - len)];
this._cacheSize = this._cache[0].length;
chunk = chunk.slice(0, chunk.length - len);
chunk = chunk.subarray(0, chunk.length - len);
}

// chunk might be empty if it only continued of 8bit bytes and these were all cached
Expand Down
Loading