From f33387880e5e5a8f10eae6651ccc89bd108908b2 Mon Sep 17 00:00:00 2001 From: Nikita Kirsanov Date: Tue, 19 May 2020 11:28:07 +0000 Subject: [PATCH] =?UTF-8?q?Create=20Blog=20=E2=80=9Cl11n-with-babel-macros?= =?UTF-8?q?=5Fen=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blog/l11n-with-babel-macros_en.md | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/_content/blog/l11n-with-babel-macros_en.md diff --git a/src/_content/blog/l11n-with-babel-macros_en.md b/src/_content/blog/l11n-with-babel-macros_en.md new file mode 100644 index 0000000..9a7b249 --- /dev/null +++ b/src/_content/blog/l11n-with-babel-macros_en.md @@ -0,0 +1,97 @@ +--- +slug: l11n-with-babel-macros +lang: en +tweet_id: t2 +title: I18n with babel macros +date: 2020-05-19T11:26:08.598Z +thumbnail: + img: /images/uploads/matrix.jpg + author: " " + src: " " +tags: + - babel +preface: some +--- +```js +/* eslint-disable */ +// const { readFileSync } = require('fs') +const cha = require('./translations/de-DE/translations.json'); +const get = require('lodash.get'); +const { createMacro, MacroError } = require('babel-plugin-macros'); + +module.exports = createMacro( + ({ + references: { default: defaultRefs = [], t: tRefs = [] }, + state, + babel: { types: t, template }, + }) => { + let buildReplace = template( + `%%str%%.replace(/\\$(\\d)/g, (_, i) => %%placeholderValues%%[i-1])` + ); + + [...tRefs, ...defaultRefs].forEach(tPath => { + let { parentPath } = tPath; + let pathToTranslation; + let placeholderValues; + + if (parentPath.type === 'TaggedTemplateExpression') { + let quasi = parentPath.get('quasi'); + pathToTranslation = quasi.node.quasis[0].value.raw; + } else if (parentPath.type === 'CallExpression') { + if (tPath === parentPath.get('callee')) { + let [pathArg, placeholderValuesPath] = parentPath.get('arguments'); + + pathToTranslation = pathArg.node.value; + placeholderValues = placeholderValuesPath?.node; + } else if (parentPath.get('arguments').includes(tPath)) { + throw new MacroError( + `Invalid usage of 't': do not try to pass it as and arg` + ); + } + } else { + throw new MacroError(`Invalid usage of 't': what are you doing?`); + } + + if (pathToTranslation) { + let value = get(cha, pathToTranslation); + let valueNode; + + switch (typeof value) { + case 'string': { + valueNode = t.stringLiteral(value); + break; + } + case 'object': { + if (value !== null) { + valueNode = template(`(${JSON.stringify(value)})`)(); + break; + } + } + case 'undefined': + valueNode = t.identifier('null'); + } + + if ( + typeof value === 'string' && + placeholderValues && + placeholderValues.elements.every( + ([nonLiterals, literals], placeholder) => + /(Boolean|String|Numeric)Literal/.test(placeholder.type) + ) + ) { + let placeholders = placeholderValues.elements.map(v => v.value); + + value = value.replace(/\$(\d+)/g, (_, i) => placeholders[i - 1]); + + valueNode = t.stringLiteral(value); + } + + parentPath.replaceWith(valueNode); + } else { + console.warn('something bad happened :-('); + } + }); + } +); + +``` \ No newline at end of file