Skip to content

Commit

Permalink
✨ Improve language detection system
Browse files Browse the repository at this point in the history
  • Loading branch information
leolabs committed Mar 19, 2020
1 parent 6c6e21f commit a0c6f95
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 226 deletions.
11 changes: 3 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,11 @@ const translate = async (
console.log();

console.log(`✨ Initializing ${translationService.name}...`);
translationService.initialize(config, matcherMap[matcher]);
process.stdout.write(chalk`├── Getting available languages `);
const availableLanguages = await translationService.getAvailableLanguages();
console.log(
chalk`({green.bold ${String(availableLanguages.length)} languages})`,
);
await translationService.initialize(config, matcherMap[matcher]);
console.log(chalk`└── {green.bold Done}`);
console.log();

if (!availableLanguages.includes(sourceLang)) {
if (!translationService.supportsLanguage(sourceLang)) {
throw new Error(
`${translationService.name} doesn't support the source language ${sourceLang}`,
);
Expand Down Expand Up @@ -232,7 +227,7 @@ const translate = async (
let removedTranslations = 0;

for (const language of targetLanguages) {
if (!availableLanguages.includes(language)) {
if (!translationService.supportsLanguage(language)) {
console.log(
chalk`🙈 {yellow.bold ${translationService.name} doesn't support} {red.bold ${language}}{yellow.bold . Skipping this language.}`,
);
Expand Down
8 changes: 5 additions & 3 deletions src/services/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class DeepL implements TranslationService {
private apiKey: string;
private interpolationMatcher: Matcher;

initialize(config?: string, interpolationMatcher?: Matcher) {
async initialize(config?: string, interpolationMatcher?: Matcher) {
if (!config) {
throw new Error(`Please provide an API key for DeepL.`);
}
Expand All @@ -23,8 +23,10 @@ export class DeepL implements TranslationService {
this.apiKey = config;
}

async getAvailableLanguages() {
return ['en', 'de', 'fr', 'es', 'pt', 'it', 'nl', 'pl', 'ru'];
supportsLanguage(language: string) {
return ['en', 'de', 'fr', 'es', 'pt', 'it', 'nl', 'pl', 'ru'].includes(
language,
);
}

async translateStrings(
Expand Down
15 changes: 5 additions & 10 deletions src/services/dry-run.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { TranslationService } from '.';
import languages from '../util/languages';
import { TranslationService, TString } from '.';
import chalk from 'chalk';

export class DryRun implements TranslationService {
public name = 'Dry Run';

initialize() {}
async initialize() {}

async getAvailableLanguages() {
return languages;
supportsLanguage() {
return true;
}

async translateStrings(
strings: { key: string; value: string }[],
from: string,
to: string,
) {
async translateStrings(strings: TString[]) {
console.log();

if (strings.length > 0) {
Expand Down
26 changes: 15 additions & 11 deletions src/services/google-translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
reInsertInterpolations,
Matcher,
} from '../matchers';
import { TranslationService } from '.';
import { TranslationService, TString } from '.';

export class GoogleTranslate implements TranslationService {
private translate: Translate;
private interpolationMatcher: Matcher;
private supportedLanguages: string[] = [];

public name = 'Google Translate';

Expand All @@ -19,36 +20,39 @@ export class GoogleTranslate implements TranslationService {
);
}

initialize(config?: string, interpolationMatcher?: Matcher) {
async initialize(config?: string, interpolationMatcher?: Matcher) {
this.translate = new Translate({
autoRetry: true,
keyFilename: config || undefined,
});

this.interpolationMatcher = interpolationMatcher;
this.supportedLanguages = await this.getAvailableLanguages();
}

async getAvailableLanguages() {
const [languages] = await this.translate.getLanguages();
return languages.map(l => l.code.toLowerCase());
}

async translateStrings(
strings: { key: string; value: string }[],
from: string,
to: string,
) {
supportsLanguage(language: string) {
return this.supportedLanguages.includes(language);
}

async translateStrings(strings: TString[], from: string, to: string) {
return Promise.all(
strings.map(async ({ key, value }) => {
const { clean, replacements } = replaceInterpolations(
value,
this.interpolationMatcher,
);

const translationResult = (await this.translate.translate(clean, {
from,
to,
}))[0];
const translationResult = (
await this.translate.translate(clean, {
from,
to,
})
)[0];

return {
key: key,
Expand Down
14 changes: 11 additions & 3 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ export interface TranslationResult {
value: string;
translated: string;
}

export interface TString {
key: string;
value: string;
}
export interface TranslationService {
name: string;
initialize: (config?: string, interpolationMatcher?: Matcher) => void;
getAvailableLanguages: () => Promise<string[]>;
initialize: (
config?: string,
interpolationMatcher?: Matcher,
) => Promise<void>;
supportsLanguage: (language: string) => boolean;
translateStrings: (
strings: { key: string; value: string }[],
strings: TString[],
from: string,
to: string,
) => Promise<TranslationResult[]>;
Expand Down
9 changes: 4 additions & 5 deletions src/services/manual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import {
reInsertInterpolations,
Matcher,
} from '../matchers';
import languages from '../util/languages';
import { TranslationService } from '.';

export class ManualTranslation implements TranslationService {
private interpolationMatcher: Matcher;
public name = 'Manual Translation';

initialize(config?, interpolationMatcher?: Matcher) {
async initialize(config?, interpolationMatcher?: Matcher) {
this.interpolationMatcher = interpolationMatcher;
}

async getAvailableLanguages() {
return languages;
supportsLanguage() {
return true;
}

async translateStrings(
Expand Down Expand Up @@ -45,7 +44,7 @@ export class ManualTranslation implements TranslationService {
name: 'result',
message: `[${from} -> ${to}] ${
key !== value ? `(${key}) ` : ''
}${value}:`,
}"${value}":`,
},
]);

Expand Down
186 changes: 0 additions & 186 deletions src/util/languages.ts

This file was deleted.

0 comments on commit a0c6f95

Please sign in to comment.