Skip to content

Commit

Permalink
Merge commit '7f9bbfab420926051cccdb65e7d567059afa6bc9'
Browse files Browse the repository at this point in the history
  • Loading branch information
ppillot committed Jun 23, 2024
2 parents 7e82ffe + 7f9bbfa commit efc6dff
Show file tree
Hide file tree
Showing 23 changed files with 31,920 additions and 16,413 deletions.
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
'presets': [
'@vue/app'
'@vue/cli-plugin-babel/preset'
],
'plugins': [
[
Expand Down
228 changes: 72 additions & 156 deletions build/i18n.js
Original file line number Diff line number Diff line change
@@ -1,176 +1,92 @@
var fs = require('fs')
var readline = require('readline')
const { google } = require('googleapis')
// var googleAuth = require('google-auth-library')
var escapeQuotes = require('escape-quotes')
const fs = require('fs')
const https = require('node:https')
const escapeQuotes = require('escape-quotes')

// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/'
var TOKEN_PATH = 'tocken.json'
const SHEET_ID = '1tfpNe1SwHQ51arbPlnE6y7rDB-JKImhGICQGsMzQtes'
const I18N_URL = `https://docs.google.com/spreadsheets/d/${SHEET_ID}/gviz/tq?tqx=out:csv`
const NODE_OPEN = 'node_open'

// Load client secrets from a local file.
fs.readFile('./build/credentials.json', function processClientSecrets (err, content) {
if (err) {
console.log('Error loading client secret file: ' + err)
return
}
// Authorize a client with the loaded credentials, then call the
// Google Sheets API.
authorize(JSON.parse(content), buildI18N)
})
https.get(I18N_URL, (res) => {
const data = []

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize (credentials, callback) {
var clientSecret = credentials.installed.client_secret
var clientId = credentials.installed.client_id
var redirectUrl = credentials.installed.redirect_uris[0]
var oauth2Client = new google.auth.OAuth2(clientId, clientSecret, redirectUrl)
res.on('data', (d) => {
data.push(d)
})

// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function (err, token) {
if (err) {
getNewToken(oauth2Client, callback)
} else {
oauth2Client.credentials = JSON.parse(token)
callback(oauth2Client)
res.on('end', () => {
const lines = Buffer.concat(data).toString().split('\n')
if (lines.length === 0) {
console.log('No data found.')
return
}
})
}

/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
function getNewToken (oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
})
console.log('Authorize this app by visiting this url: ', authUrl)
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('Enter the code from that page here: ', function (code) {
rl.close()
oauth2Client.getToken(code, function (err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err)
return
}
oauth2Client.credentials = token
storeToken(token)
callback(oauth2Client)
})
})
}
// CSV, cells are between quotes, no space between comma and quotes
// Here, remove first and last quote in line, then split along the separator
const rows = lines.map((row) => row.substring(1, row.length - 1).split('","'))

/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken (token) {
try {
fs.mkdirSync(TOKEN_DIR)
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token))
console.log('Token stored to ' + TOKEN_PATH)
}
// HEADER
// first value is key, skip first col
const locales = rows[0].filter((item, itemNum) => { return itemNum > 0 })
// console.log(locales)
writeLocales(locales)

/**
* Print the names and majors of students in a sample spreadsheet:
* https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
*/
function buildI18N (auth) {
var sheets = google.sheets({ version: 'v4', auth })
sheets.spreadsheets.values.get({
spreadsheetId: '1tfpNe1SwHQ51arbPlnE6y7rDB-JKImhGICQGsMzQtes',
range: 'Sheet1'
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' + err)
return
}
var rows = response.data.values
if (rows.length === 0) {
console.log('No data found.')
} else {
// first value is key
const locales = rows[0].filter((item, itemNum) => { return itemNum > 0 })
// console.log(locales)
writeLocales(locales)
for (let localeNum = 0; localeNum < locales.length; localeNum++) {
let level = -1
let s = '{'
let previousToken = 'nodeOpening'
for (let i = 1; i < rows.length; i++) {
let row = rows[i]
// console.log(row)
// BODY
for (let localeNum = 0; localeNum < locales.length; localeNum++) {
let level = -1
let s = '{'
let previousToken = NODE_OPEN
for (let i = 1; i < rows.length; i++) {
let cells = rows[i]
// console.log(row)

if (row.length === 1) { // key name
// guess level
const l = row[0]
.split('')
.reduce((acc, index) => {
return acc + (index === '>')
}, 0) - 1
// Is it a key declaration?
if (cells[0][0] === '>' &&
cells.slice(1).every((v) => !v)
) {
// guess level of key
let l = 0
while (cells[0][l] === '>') {
l++
}
l--

const keyName = row[0].slice(l + 1)
const keyName = cells[0].substring(l + 1)

if (l === level) { // new sibling
s += '},'
s += '"' + keyName + '":{'
previousToken = 'nodeOpening'
} else if (l < level) { // new parent
for (let j = 0; j <= level - l; j++) {
s += '}'
}
s += ',"' + keyName + '":{'
previousToken = 'nodeOpening'
} else { // new child
s += (previousToken === 'nodeOpening')
? '"' + keyName + '":{'
: ',"' + keyName + '":{'
previousToken = 'nodeOpening'
if (l === level) { // new sibling
s += '},'
s += '"' + keyName + '":{'
previousToken = NODE_OPEN
} else if (l < level) { // new parent
for (let j = 0; j <= level - l; j++) {
s += '}'
}
level = l
} else { // new item
if (previousToken !== 'nodeOpening') {
s += ','
}
s += '"' + row[0] + '":"' + escapeQuotes(row[localeNum + 1].replace("\\'", "'"), '"', '\\') + '"'
previousToken = 'item'
s += ',"' + keyName + '":{'
previousToken = NODE_OPEN
} else { // new child
s += (previousToken === NODE_OPEN)
? '"' + keyName + '":{'
: ',"' + keyName + '":{'
previousToken = NODE_OPEN
}
level = l
} else { // new item
if (previousToken !== NODE_OPEN) {
s += ','
}
s += '"' + cells[0] + '":"' + escapeQuotes(cells[localeNum + 1].replace("\\'", "'"), '"', '\\') + '"'
previousToken = 'item'
}
for (let i = 0; i <= level + 1; i++) {
s += '}'
}
console.log(s)
let jsonLocale = JSON.parse(s)
const result = JSON.stringify(addHelp(jsonLocale, locales[localeNum]))
writeJSON(result, locales[localeNum])
}
for (let i = 0; i <= level + 1; i++) {
s += '}'
}
console.log(s)
let jsonLocale = JSON.parse(s)
const result = JSON.stringify(addHelp(jsonLocale, locales[localeNum]))
writeJSON(result, locales[localeNum])
}
})
}
})

function addHelp (json, locale) {
const content = fs.readFileSync('./src/locales/' + locale + '/help.' + locale + '.json', 'utf8')
Expand Down
Loading

0 comments on commit efc6dff

Please sign in to comment.