Skip to content

Commit

Permalink
Fix cli
Browse files Browse the repository at this point in the history
  • Loading branch information
rangle-mobinni committed Oct 25, 2018
1 parent 36ee445 commit fa2de7b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
lib/
cli/
.env
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<file>")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/iso-codes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Credits to https://gist.github.com/jrnk/8eb57b065ea0b098d571
*/
export const isoCodes = [
module.exports.isoCodes = [
{
"code": "ab",
"name": "Abkhaz"
Expand Down
20 changes: 15 additions & 5 deletions src/services.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
Expand All @@ -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}` +
Expand All @@ -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;
36 changes: 18 additions & 18 deletions src/translate.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
18 changes: 9 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -27,7 +28,7 @@ export const traverse = obj => {
return results;
};

export const revertInterpolations = placeholder => ({
module.exports.revertInterpolations = placeholder => ({
interpolations,
phrase
}) => {
Expand All @@ -39,7 +40,7 @@ export const revertInterpolations = placeholder => ({
return originalPhrase;
};

export const replaceInterpolations = placeholder => ({
module.exports.replaceInterpolations = placeholder => ({
interpolations,
phrase,
...other
Expand All @@ -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;
Expand All @@ -66,4 +67,3 @@ export const findInterpolations = regexp => ({ phrase, ...other }) => {
...other
};
};

0 comments on commit fa2de7b

Please sign in to comment.