Created
December 15, 2020 16:51
-
-
Save danielgek/8904a82318bcce2cb2f2a602dda7c2c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const fs = require('fs'); | |
const Airtable = require('airtable'); | |
const AIRTABLE_API_KEY = process.env.AIRTABLE_API_KEY; | |
const AIRTABLE_BASE = process.env.AIRTABLE_BASE; | |
Airtable.configure({ | |
endpointUrl: 'https://api.airtable.com', | |
apiKey: AIRTABLE_API_KEY, | |
}); | |
const base = Airtable.base(AIRTABLE_BASE); | |
const tables = [ | |
'Product Descriptions', | |
'Header', | |
'Homepage', | |
'Footer', | |
'Inspiration', | |
'Listing', | |
'All Products', | |
'Product', | |
'Basket', | |
'Configurator', | |
'About Us', | |
'Privacy', | |
'Terms', | |
'FAQ', | |
'Request Quote', | |
'Contact Us', | |
'Find me a box', | |
]; | |
const EN = {}; | |
const DE = {}; | |
function keyParser(obj, key, value) { | |
if (!key) return; | |
let lastKey = null; | |
const keyParts = key.trim().split('.'); | |
keyParts.forEach((part, index) => { | |
if (!obj[part] && index === 0) { | |
obj[part] = {}; | |
lastKey = obj[part]; | |
return; | |
} | |
if (obj[part] && index === 0) { | |
lastKey = obj[part]; | |
return; | |
} | |
if (lastKey[part]) { | |
lastKey = lastKey[part]; | |
return; | |
} | |
if (index + 1 === keyParts.length) { | |
lastKey[part] = value; | |
return; | |
} | |
if (!lastKey[part] && index !== 0) { | |
lastKey[part] = {}; | |
lastKey = lastKey[part]; | |
} | |
}); | |
} | |
Promise.all( | |
tables.map((b) => { | |
return new Promise((resolve, reject) => { | |
base(b) | |
.select() | |
.eachPage( | |
(records) => { | |
resolve( | |
records.map((rec) => ({ | |
key: rec.get('Key'), | |
en: (rec.get('EN') || '').trim(), | |
de: (rec.get('DE') || '').trim(), | |
})) | |
); | |
}, | |
(err) => { | |
if (err) { | |
console.log(err); | |
reject(err); | |
} | |
} | |
); | |
}); | |
}) | |
).then( | |
(tables) => { | |
tables.flat().forEach(({ key, en, de }) => { | |
keyParser(EN, key, en); | |
keyParser(DE, key, de); | |
}); | |
fs.writeFileSync( | |
path.join(__dirname, '..', '..', 'locales', 'en.json'), | |
JSON.stringify(EN, null, 2) // eslint-disable-line no-magic-numbers | |
); | |
fs.writeFileSync( | |
path.join(__dirname, '..', '..', 'locales', 'de.json'), | |
JSON.stringify(DE, null, 2) // eslint-disable-line no-magic-numbers | |
); | |
console.log('Updated translations!'); | |
}, | |
(err) => { | |
console.log(err); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment