From d516b914f93b77fe980578161f5cd38ddb5b798a Mon Sep 17 00:00:00 2001 From: zdm Date: Sat, 17 Feb 2024 11:26:17 +0200 Subject: [PATCH] fix: translations --- lib/get-text.js | 142 ++++++++++++++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 53 deletions(-) diff --git a/lib/get-text.js b/lib/get-text.js index 80d23971..0030c7a2 100644 --- a/lib/get-text.js +++ b/lib/get-text.js @@ -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 ], + }, + } ); + } }, }; @@ -187,6 +207,7 @@ export default class GetText { return result( [ 500, message ] ); } + // XXX extract commants #parseCallExpression ( ast, node, { start } = {} ) { let method; @@ -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 } = {} ) {