Skip to content

Commit

Permalink
add separate node translation api
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoffer Jahren committed Mar 24, 2023
1 parent bc5f2a4 commit ead2db3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.0.0-next.3",
"version": "4.0.12",
"packages": [
"packages/*"
],
Expand Down
39 changes: 35 additions & 4 deletions packages/babel-plugin-extract-messages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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<string, unknown> = {}

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
Expand All @@ -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<string, unknown> = {
props = {
id: getTextFromExpression(
t,
firstArgument as Expression,
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
6 changes: 6 additions & 0 deletions packages/core/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ export class I18n extends EventEmitter<Events> {
)(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)
}
Expand Down

0 comments on commit ead2db3

Please sign in to comment.