Skip to content

Commit

Permalink
fix: translations
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Feb 17, 2024
1 parent 89ca989 commit d516b91
Showing 1 changed file with 89 additions and 53 deletions.
142 changes: 89 additions & 53 deletions lib/get-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,42 @@ export default class GetText {

#parseYaml ( content ) {
const locale = {
"l10n": ( msgId, plural, pluralNumber ) => {
this.#poFile.addEctractedMessages( {
[ msgId ]: {
"pluralId": plural,
"references": [ this.#relativePath ],
},
} );
"l10n": ( single, plural, pluralNumber ) => {
if ( single ) {
this.#poFile.addEctractedMessages( {
[ single ]: {
"references": [ this.#relativePath ],
},
} );
}

if ( plural ) {
this.#poFile.addEctractedMessages( {
[ plural ]: {
"pluralId": plural,
"references": [ this.#relativePath ],
},
} );
}
},

"l10nt": ( msgId, plural, pluralNumber ) => {
this.#poFile.addEctractedMessages( {
[ msgId ]: {
"pluralId": plural,
"references": [ this.#relativePath ],
},
} );
"l10nt": ( single, plural, pluralNumber ) => {
if ( single ) {
this.#poFile.addEctractedMessages( {
[ single ]: {
"references": [ this.#relativePath ],
},
} );
}

if ( plural ) {
this.#poFile.addEctractedMessages( {
[ plural ]: {
"pluralId": plural,
"references": [ this.#relativePath ],
},
} );
}
},
};

Expand Down Expand Up @@ -187,6 +207,7 @@ export default class GetText {
return result( [ 500, message ] );
}

// XXX extract commants
#parseCallExpression ( ast, node, { start } = {} ) {
let method;

Expand Down Expand Up @@ -235,66 +256,81 @@ export default class GetText {
// not a supported function / method name
if ( method !== "l10n" && method !== "l10nt" ) return;

var id,
message = {
"pluralId": null,
var single = {
"id": null,
"flags": null,
"references": null,
},
plural = {
"id": null,
"pluralId": null,
"flags": null,
"extractedComments": null,
"references": null,
};

// message id
const msgId = this.#parseMessage( node.arguments[ 0 ], {
const message = this.#parseMessage( node.arguments[ 0 ], {
start,
} );
if ( !msgId.value ) return;
id = msgId.value;
if ( msgId.isTemplate ) message.flags = [ "javascript-format" ];

if ( message.value ) {
single.id = message.value;

if ( message.isTemplate ) single.flags = [ "javascript-format" ];

// references
const { line, column } = this.#getNodeLocation( node.arguments[ 0 ], { start } );
single.reference = `${ this.#relativePath }:${ line }:${ column }`;
}

// plural
if ( node.arguments[ 1 ] ) {
const plural = this.#parseMessage( node.arguments[ 1 ], {
const message = this.#parseMessage( node.arguments[ 1 ], {
start,
} );

if ( !plural.value ) return;
if ( message.value ) {
plural.id = message.value;
plural.pluralId = message.value;

if ( !plural.isTemplate ) {
throw this.#error( `Plural message must be msgid taggeed template`, {
node,
start,
} );
}
if ( message.isTemplate ) plural.flags = [ "javascript-format" ];

if ( !msgId.isTemplate ) {
throw this.#error( `Single form message used with plurak form must be msgid taggeed template`, {
node,
start,
} );
// references
const { line, column } = this.#getNodeLocation( node.arguments[ 1 ], { start } );
plural.reference = `${ this.#relativePath }:${ line }:${ column }`;
}

message.pluralId = plural.value;
if ( plural.isTemplate ) message.flags = [ "javascript-format" ];
}

// extracted comments
const extractedComments = this.#extractNodeComments( ast, node );
if ( extractedComments ) {
message.extractedComments = {};
message.extractedComments.comments ??= [];
message.extractedComments.comments.push( extractedComments );
}

// references
const { line, column } = this.#getNodeLocation( node, {
start,
} );
if ( !single.id && !plural.id ) return;

const reference = `${ this.#relativePath }:${ line }:${ column }`;
// XXX
var extractedComments;

message.references = reference;
// extracted comments
// const extractedComments = this.#extractNodeComments( ast, node );
// if ( extractedComments ) {
// message.extractedComments = {};
// message.extractedComments.comments ??= [];
// message.extractedComments.comments.push( extractedComments );
// }

if ( single.id ) {
this.#poFile.addEctractedMessages( {
[ single.id ]: {
...single,
extractedComments,
},
} );
}

this.#poFile.addEctractedMessages( { [ id ]: message } );
if ( plural.id ) {
this.#poFile.addEctractedMessages( {
[ plural.id ]: {
...plural,
extractedComments,
},
} );
}
}

#parseMessage ( node, { start } = {} ) {
Expand Down

0 comments on commit d516b91

Please sign in to comment.