Browse Source

fix(extract-translations): add support for <i18n-t> tags

bertyhell/feature/translations-extraction-script
Bert Verhelst 3 years ago
parent
commit
20a4b64df7
  1. 28
      extra/extract-translations.js

28
extra/extract-translations.js

@ -5,7 +5,7 @@ const JSON5 = require("json5");
// Extract translations from $t() functions in the source code and add the missing translations to all language files in src/languages/*.js // Extract translations from $t() functions in the source code and add the missing translations to all language files in src/languages/*.js
async function extractTranslations() { async function extractTranslations() {
// Load all es6 module translation files into a commonJS process // Load all ES6 module translation files into a commonJS process
const languageList = {}; const languageList = {};
const filesNames = await fs.readdir("src/languages"); const filesNames = await fs.readdir("src/languages");
for (let fileName of filesNames) { for (let fileName of filesNames) {
@ -18,17 +18,17 @@ async function extractTranslations() {
const en = languageList.en; const en = languageList.en;
// Search the source code for usages of $t() const englishExtracted = [];
const results = await findInFiles.find({
// Search the source code for usages of $t(...)
const tFuncResults = await findInFiles.find({
term: "\\$t\\(([^)]+?)\\)", term: "\\$t\\(([^)]+?)\\)",
flags: "g", flags: "g",
}, "./src", "\\.(vue|js)"); }, "./src", "\\.(vue|js)");
const englishExtracted = []; // Add the found strings to the englishExtracted list
// Make a list of all the found strings
const warnings = []; const warnings = [];
Object.values(results).map(result => { Object.values(tFuncResults).map(result => {
result.matches.map(match => { result.matches.map(match => {
const functionParams = match.substring(3, match.length - 1).trim(); const functionParams = match.substring(3, match.length - 1).trim();
const firstChar = functionParams[0]; const firstChar = functionParams[0];
@ -43,6 +43,20 @@ async function extractTranslations() {
}); });
}); });
// Search the source code for usages of <i18n-t tag="..." keypath="...">
const i18nTTagResults = await findInFiles.find({
term: "<i18n-t[^>]+keypath=\"([^\"]+)\"[^>]*>",
flags: "g",
}, "./src", "\\.vue");
// Add the found strings to the englishExtracted list
Object.values(i18nTTagResults).map(result => {
result.matches.map(match => {
const content = _.trim(match.split("keypath")[1].split("\"")[1], "\"' ");
englishExtracted.push(content);
});
});
// Update all languages with the missing strings // Update all languages with the missing strings
for (let extractedTranslation of englishExtracted) { for (let extractedTranslation of englishExtracted) {
for (let langDict of Object.values(languageList)) { for (let langDict of Object.values(languageList)) {

Loading…
Cancel
Save