Created
November 16, 2016 17:04
-
-
Save misterbyrne/04349cda01fecc51c189f9fb57ec87fc to your computer and use it in GitHub Desktop.
i18n import tool
This file contains 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 fs = require('fs'); | |
const i18n = require('./app/i18n'); | |
const translations = i18n.defaultLocales; | |
const input = require('./new-strings.json') | |
const getLanguage = locale => locale.match(/([a-z]{2,3})-?/)[1]; | |
const orderedJsonStringify = obj => JSON.stringify(Object.keys(obj).sort().reduce((r, k) => (r[k] = obj[k], r), {}), undefined, 2); | |
/* | |
This is a very very quickly hacked together tool to help importing a new string | |
into the relevant files in ./i18n | |
It must be placed the root of intercom-js | |
new-strings.json must be in the same location | |
execute with... | |
node ./add-translation.js | |
Populate the new-strings.json file with an object that looks ilke this: | |
{ | |
"my-new-string": { | |
"es": "the string in Spanish", | |
"de": "the string in German" | |
}, | |
"my-other-new-string": { | |
"es-ES": "the string in Spanish", | |
"de-DE": "the string in German" | |
} | |
} | |
It will only match pre-existing i18n resources and will do partial matches | |
if a full locale is supplied instead of just a language, e.g. | |
if the translation is for es-ES but we only have a es.json file | |
*/ | |
for (let key of Object.keys(input)) { | |
console.log(`Adding ${key}`); | |
const newTranslations = input[key]; | |
for (let locale of Object.keys(newTranslations)) { | |
let language = getLanguage(locale); | |
let translationKey = locale; | |
let translation = translations[translationKey]; | |
if (translation === undefined) { | |
translationKey = language; | |
translation = translations[translationKey]; | |
} | |
if (translation) { | |
translation[key] = newTranslations[locale]; | |
let path = `./app/i18n/${translationKey}.json` | |
console.log(`Saving ${translationKey} to ${path}`); | |
const output = orderedJsonStringify(translation); | |
fs.writeFileSync(path, output); | |
} else { | |
console.error(`No i18n found for ${locale}`); | |
} | |
} | |
} |
This file contains 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
{ | |
"asked-about": { | |
"ar-SA": "سأل عن", | |
"bg-BG": "Попитахте за", | |
"bs-BA": "Питао о", | |
"ca-ES": "Va preguntar sobre", | |
"cs-CZ": "Ptal/a se na", | |
"da-DK": "Spurgte om", | |
"de-DE": "Gefragt nach", | |
"de": "Gefragt nach", | |
"el-GR": "Ρώτησε για", | |
"es-ES": "Preguntó acerca de", | |
"et-EE": "Esitatud küsimuse sisu", | |
"fi-FI": "Kysyit", | |
"fr-FR": "Demandé à propos de", | |
"he-IL": "שאל בנושא", | |
"hr-HR": "Upitali ste o", | |
"hu-HU": "A következőt kérdezte", | |
"id-ID": "Menanyakan tentang", | |
"it-IT": "Ha chiesto in merito al tema", | |
"ja-JP": "질문한 내용", | |
"ko-KR": "질문한 내용", | |
"lt-LT": "Paklausė apie", | |
"lv-LV": "Vaicājāt par", | |
"nl-NL": "Stelde een vraag over", | |
"no-NO": "Spurte om", | |
"pl-PL": "Pytanie dotyczyło następującego zagadnienia", | |
"pt-BR": "Perguntou sobre", | |
"pt-PT": "Perguntou por", | |
"ro-RO": "Aţi întrebat despre", | |
"ru-RU": "Спрашивает о", | |
"sl-SI": "Vprašanje o", | |
"sr-RS": "Распитали сте се у вези са", | |
"sv-SE": "Frågade om", | |
"tr-TR": "Şununla ilgili soru soruldu:", | |
"vi-VN": "Đã hỏi về", | |
"xc-MN": "тухай асууж байна", | |
"zh-CN": "询问关于", | |
"zh-TW": "詢問關於" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment