Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wallet] Add script to translate locale strings #1485

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mobile/fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_bundle(env)
return $bundle_name + env
end

def fastlane_supply(env, track, bundle_suffix, skip_deploy)
def fastlane_supply(env, track, bundle_suffix)
return supply(
json_key: 'fastlane/google-play-service-account.json',
track: track,
Expand Down
62 changes: 62 additions & 0 deletions packages/mobile/scripts/translateFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Translate a file using google translate
// tslint:disable: no-console

const fs = require('fs')
const request = require('request')

const fileName = process.argv[2]
const googleApiToken = process.argv[3]
console.info(`Translating file: ${fileName}`)

const json = fs.readFileSync(`../locales/en-US/${fileName}`)
const strings = JSON.parse(json)
console.info(`Found ${Object.keys(strings).length} strings`)

function translateString(s) {
return new Promise((resolve, reject) => {
console.info(`Looking up ${s}`)
request.post(
'https://translation.googleapis.com/language/translate/v2',
{
headers: {
Authorization: `Bearer ${googleApiToken}`,
},
json: {
format: 'text',
q: s,
source: 'en',
target: 'es',
},
},
(error, res, body) => {
if (error) {
reject(error)
return
}
resolve(body.data.translations[0].translatedText)
}
)
})
}

async function translateStrings(stringsToTranslate) {
const translations = {}

const promises = Promise.all(
Object.keys(stringsToTranslate).map(async (key) => {
const val = stringsToTranslate[key]

if (typeof val === 'string') {
const t = await translateString(val)
translations[key] = t
} else if (typeof stringsToTranslate === 'object') {
translations[key] = await translateStrings(val)
}
})
)

await promises
return translations
}

translateStrings(strings).then((translations) => console.log(JSON.stringify(translations)))