From 6742207c77f5afc578a7062ffc56fcd28867d950 Mon Sep 17 00:00:00 2001 From: zuiidea Date: Wed, 14 Nov 2018 22:44:46 +0800 Subject: [PATCH] :bug: Fix cannot translate fields with variables. --- scripts/translate.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scripts/translate.js b/scripts/translate.js index 5980b43..b2d9444 100644 --- a/scripts/translate.js +++ b/scripts/translate.js @@ -56,9 +56,21 @@ const transform = async ({ from, to, locales, outputPath }) => { let res = key let way = 'youdao' if (key.indexOf('/') !== 0) { - res = await youdao({ - q: key, - }) + const reg = '{([^{}]*)}' + const tasks = key + .match(new RegExp(`${reg}|((?<=(${reg}|^)).*?(?=(${reg}|$)))`, 'g')) + .map(item => { + if (new RegExp(reg).test(item)) { + return Promise.resolve(item) + } + return youdao({ + q: item, + from, + to, + }) + }) + + res = (await Promise.all(tasks)).join('') } else { res = `/${to + key}` way = 'link'