UNPKG

polyglot-cli

Version:
103 lines (89 loc) 3.35 kB
const fs = require('fs'); const path = require('path'); const co = require('co'); const config = require('./config'); const network = require('./network'); const utils = require('./utils'); const serializers = require('../serializers'); function __createKey(name, projectId) { return network.create('/translation_keys', { data: { attributes: {name}, relationships: { project: { data: {id: projectId, type: 'project'} } } } }).then((key) => key.id); } module.exports = { getProjectData(projectId = config.project.id) { return network.read(`/projects/${projectId}`); }, getLastUpdatedAt(projectId = config.project.id) { return this.getProjectData(projectId) .then((projectData) => projectData.updated_at); }, getDefaultLanguage(projectId = config.project.id) { return this.getProjectData(projectId) .then((projectData) => ({ locale: projectData.default_language.locale, id: projectData.default_language.id })); }, getRemoteTranslations(projectId = config.project.id) { return network.read(`/translations?filter[project_id]=${projectId}`); }, saveProject(fileSystem = fs, project = config.project) { project.version = config.version; fileSystem.writeFileSync(config.projectFile, JSON.stringify(project, null, 2)); }, checkInitErrors({user = true, project = true} = {}) { if (user && (!config.auth || !config.auth.token)) { return utils.error('You need to log in first'); } if (project && (!config.project || !config.project.id)) { return utils.error('Please initialize the project first'); } return null; }, loadTranslations(fileSystem, locale, project = config.project, cwd = process.cwd()) { let translations; const filePath = path.join(cwd, project.path); if (project.filetype === '1') { utils.debug('Loading a single file'); translations = JSON.parse(fileSystem.readFileSync(filePath)); } else { utils.debug('Loading a locale file'); translations = { [locale]: JSON.parse(fileSystem.readFileSync(path.join(filePath, `${locale}.json`))) }; } // Deserialize utils.debug(`Deserialize using the ${project.serializer} serializer`); return serializers.forRemote(translations, project)[locale]; }, ensureRemoteKeys: co.wrap(function* (localKeys, remoteKeys, project = config.project) { const keysToCreate = localKeys.filter((key) => !(key in remoteKeys)); const map = Object.assign({}, remoteKeys); for (const key of keysToCreate) { const keyId = yield __createKey(key, project.id); utils.debug(`Create key ${key}: ${keyId}`); map[key] = keyId; } return map; }), sendTranslations: co.wrap(function* (language, keys, toImport = {}, localTranslations) { const translationKeys = Object.keys(toImport); for (const translationKey of translationKeys) { const remote = toImport[translationKey].remote; const key = keys[translationKey]; const value = localTranslations[translationKey]; const data = utils.constructTranslationObject(language, key, value); const method = remote ? network.update : network.create; const url = remote ? `/translations/${remote}` : '/translations'; yield method(url, data); } }) };