diff --git a/.gitignore b/.gitignore index fa6d0d0..64e3dca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ lib/ cli/ .env diff --git a/cli.js b/cli.js index 9dd2f6a..d6a962d 100755 --- a/cli.js +++ b/cli.js @@ -2,7 +2,7 @@ const program = require("commander"); const path = require("path"); const fs = require("fs"); -const translate = require("./cli/translate").default; +const translate = require("./src/translate"); program .arguments("") diff --git a/package.json b/package.json index cd1e0d9..29658c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "i18n-json-tool", - "version": "0.3.2", + "version": "0.3.3", "description": "Convert JSON i18n files via the yandex api for quick internationalization testing", "main": "lib/library.js", "scripts": { diff --git a/src/iso-codes.js b/src/iso-codes.js index 26bf702..4f689ca 100644 --- a/src/iso-codes.js +++ b/src/iso-codes.js @@ -1,7 +1,7 @@ /* * Credits to https://gist.github.com/jrnk/8eb57b065ea0b098d571 */ -export const isoCodes = [ +module.exports.isoCodes = [ { "code": "ab", "name": "Abkhaz" diff --git a/src/services.js b/src/services.js index 33fad49..19c5853 100644 --- a/src/services.js +++ b/src/services.js @@ -1,10 +1,12 @@ -export const SERVICES = { +const SERVICES = { GOOGLE: "google", YANDEX: "yandex", BING: "bing" }; -export const buildBingFetch = (apiKey, isoCode, phrase) => () => { +module.exports.SERVICES = SERVICES; + +const buildBingFetch = (apiKey, isoCode, phrase) => () => { const parser = new DOMParser(); return fetch( `https://api.microsofttranslator.com/V2/Http.svc/Translate?text=${encodeURIComponent( @@ -24,7 +26,9 @@ export const buildBingFetch = (apiKey, isoCode, phrase) => () => { }); }; -export const buildGoogleFetch = (apiKey, isoCode, phrase) => () => +module.exports.buildBingFetch = buildBingFetch; + +const buildGoogleFetch = (apiKey, isoCode, phrase) => () => fetch( `https://translation.googleapis.com/language/translate/v2?key=${apiKey}&q=${encodeURIComponent( phrase @@ -39,7 +43,9 @@ export const buildGoogleFetch = (apiKey, isoCode, phrase) => () => .then(res => res.json()) .then(res => res.data.translations[0].translatedText); -export const buildYandexFetch = (apiKey, isoCode, phrase) => () => +module.exports.buildGoogleFetch = buildGoogleFetch; + +const buildYandexFetch = (apiKey, isoCode, phrase) => () => fetch( `https://translate.yandex.net/api/v1.5/tr.json/translate?lang=${isoCode}` + `&key=${apiKey}` + @@ -49,11 +55,15 @@ export const buildYandexFetch = (apiKey, isoCode, phrase) => () => .then(res => res.json()) .then(res => res.text.join()); +module.exports.buildYandexFetch = buildYandexFetch; + const translationServices = { [SERVICES.BING]: buildBingFetch, [SERVICES.GOOGLE]: buildGoogleFetch, [SERVICES.YANDEX]: buildYandexFetch }; -export const createFetchForService = service => ({ apiKey, isoCode, phrase }) => +const createFetchForService = service => ({ apiKey, isoCode, phrase }) => translationServices[service](apiKey, isoCode, phrase); + +module.exports.createFetchForService = createFetchForService; \ No newline at end of file diff --git a/src/translate.js b/src/translate.js index d1b5398..5602aa8 100644 --- a/src/translate.js +++ b/src/translate.js @@ -1,31 +1,31 @@ -import fetch from "isomorphic-fetch"; -import { concat, curry, pipe, reduce, assocPath, mergeDeepWith } from "ramda"; -import { +const fetch = require("isomorphic-fetch"); +const { reduce, assocPath } = require("ramda"); +const { traverse, - buildEndpoint, findInterpolations, replaceInterpolations, revertInterpolations, verifyISOCode -} from "./utils"; -import { SERVICES, createFetchForService } from "./services"; +} = require("./utils"); +const { SERVICES, createFetchForService } = require("./services"); -export const translate = async ({ path, interpolations, promise }) => - await promise() - .then(phrase => { - const original = revertInterpolations(PLACEHOLDER)({ - phrase, - interpolations - }); - return Promise.resolve({ - path, - original - }); +const translate = async ({ path, interpolations, promise }) => + await promise().then(phrase => { + const original = revertInterpolations(PLACEHOLDER)({ + phrase, + interpolations }); + return Promise.resolve({ + path, + original + }); + }); + +module.exports.translate = translate; const PLACEHOLDER = "$$$"; -export default async ({ +module.exports = async ({ apiKey, isoCode, translations, diff --git a/src/utils.js b/src/utils.js index 8305cd5..17b78db 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,21 +1,22 @@ -import { keys } from "ramda"; +const { keys } = require("ramda"); -import { isoCodes } from "./iso-codes"; +const { isoCodes } = require("./iso-codes"); -export const verifyISOCode = code => !!isoCodes.find(iso => iso.code === code); +module.exports.verifyISOCode = code => + !!isoCodes.find(iso => iso.code === code); -export const traverse = obj => { +module.exports.traverse = obj => { const verify = obj => typeof obj === "string" || typeof obj === "number"; const results = []; function loopKeys(obj, prevPath) { const objKeys = keys(obj); objKeys.forEach(k => { let path = []; - if (prevPath) path = prevPath; if (!verify(obj[k])) { path.push(k); return loopKeys(obj[k], path); } + if (prevPath) path = prevPath; results.push({ path: [].concat(path, k), @@ -27,7 +28,7 @@ export const traverse = obj => { return results; }; -export const revertInterpolations = placeholder => ({ +module.exports.revertInterpolations = placeholder => ({ interpolations, phrase }) => { @@ -39,7 +40,7 @@ export const revertInterpolations = placeholder => ({ return originalPhrase; }; -export const replaceInterpolations = placeholder => ({ +module.exports.replaceInterpolations = placeholder => ({ interpolations, phrase, ...other @@ -51,7 +52,7 @@ export const replaceInterpolations = placeholder => ({ return { phrase: newPhrase.toString(), interpolations, ...other }; }; -export const findInterpolations = regexp => ({ phrase, ...other }) => { +module.exports.findInterpolations = regexp => ({ phrase, ...other }) => { const regex = new RegExp(regexp.toString(), "g"); const interpolations = []; let match; @@ -66,4 +67,3 @@ export const findInterpolations = regexp => ({ phrase, ...other }) => { ...other }; }; -