diff --git a/lerna.json b/lerna.json index ae8526f9f..99f59e230 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "4.0.0-next.3", + "version": "4.0.12", "packages": [ "packages/*" ], diff --git a/packages/babel-plugin-extract-messages/src/index.ts b/packages/babel-plugin-extract-messages/src/index.ts index a70522d4f..0164f8a08 100644 --- a/packages/babel-plugin-extract-messages/src/index.ts +++ b/packages/babel-plugin-extract-messages/src/index.ts @@ -147,6 +147,11 @@ export default function ({ types: t }: { types: BabelTypes }): PluginObj { t.isIdentifier(node.object, { name: "i18n" }) && t.isIdentifier(node.property, { name: "_" }) + const isNodeJSI18nMethod = (node: Node) => + t.isMemberExpression(node) && + t.isIdentifier(node.object, { name: "i18n" }) && + t.isIdentifier(node.property, { name: "nodeTranslate" }) + function hasI18nComment(node: Node): boolean { return ( node.leadingComments && @@ -220,12 +225,40 @@ export default function ({ types: t }: { types: BabelTypes }): PluginObj { }, CallExpression(path, ctx) { + console.log("call expr", { + isNodie: isNodeJSI18nMethod(path.node.callee), + }) const hasComment = [path.node, path.parent].some((node) => hasI18nComment(node) ) const firstArgument = path.node.arguments[0] + let props: Record = {} + + if (isNodeJSI18nMethod(path.node.callee)) { + console.log("arg which is obj expr", { + ...path.node.arguments.map((arg) => t.isObjectExpression(arg)), + }) + } + + if ( + isNodeJSI18nMethod(path.node.callee) && + t.isObjectExpression(path.node.arguments[0]) + ) { + props = { + ...extractFromObjectExpression( + t, + path.node.arguments[0], + ctx.file.hub, + ["id", "message", "comment", "context"] + ), + } + + collectMessage(path, props, ctx) + return + } + // support `i18n._` calls written by users in form i18n._(id, variables, descriptor) // without explicit annotation with comment // calls generated by macro has a form i18n._(/*i18n*/ {descriptor}) and @@ -234,7 +267,7 @@ export default function ({ types: t }: { types: BabelTypes }): PluginObj { isI18nMethod(path.node.callee) && !firstArgument?.leadingComments if (!hasComment && !isNonMacroI18n) return - let props: Record = { + props = { id: getTextFromExpression( t, firstArgument as Expression, @@ -284,9 +317,7 @@ export default function ({ types: t }: { types: BabelTypes }): PluginObj { // Extract message descriptors ObjectExpression(path, ctx) { - if (!hasI18nComment(path.node)) { - return - } + if (!hasI18nComment(path.node)) return const props = extractFromObjectExpression(t, path.node, ctx.file.hub, [ "id", diff --git a/packages/babel-plugin-extract-messages/test/fixtures/js-call-expression.js b/packages/babel-plugin-extract-messages/test/fixtures/js-call-expression.js index 3ecdc0078..33b212f99 100644 --- a/packages/babel-plugin-extract-messages/test/fixtures/js-call-expression.js +++ b/packages/babel-plugin-extract-messages/test/fixtures/js-call-expression.js @@ -8,3 +8,4 @@ const withValues = i18n._('Values {param}', { param: param }); const withContext = i18n._('Some id', {},{ context: 'Context1'}); +const withNodeMessageDescriptor = i18n.nodeTranslate({ id: 'my.id', message: 'My Id Message', comment: 'My comment'}); diff --git a/packages/core/src/i18n.ts b/packages/core/src/i18n.ts index 59a706111..6ccb1276c 100644 --- a/packages/core/src/i18n.ts +++ b/packages/core/src/i18n.ts @@ -262,6 +262,12 @@ export class I18n extends EventEmitter { )(values, formats) } + // Alias for _ to be used in node without macro setting + // uses message descriptors only + nodeTranslate(id: MessageDescriptor, values: Values | undefined = {}) { + return this._(id, values, {}) + } + date(value: string | Date, format?: Intl.DateTimeFormatOptions): string { return date(this._locales || this._locale, value, format) }