Skip to content

Commit

Permalink
refactor(translation): use translation adapters #1
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwillms committed Nov 1, 2020
1 parent ee91a13 commit 830eaa5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 11 deletions.
13 changes: 11 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# ------------------
# Translation driver
# ------------------
VUE_APP_TRANSLATION_DRIVER=none
# VUE_APP_TRANSLATION_DRIVER=google

# ---------------------------------------
# Google translation driver configuration
# ---------------------------------------
VUE_APP_GOOGLE_TRANSLATE_API_KEY=

# --------------
Expand All @@ -6,9 +15,9 @@ VUE_APP_GOOGLE_TRANSLATE_API_KEY=
VUE_APP_STORAGE_DRIVER=localstorage
# VUE_APP_STORAGE_DRIVER=firebase

# -----------------------------
# ----------------------------------
# Local storage driver configuration
# -----------------------------
# ----------------------------------
VUE_APP_LOCALSTORAGE_PROJECTS_KEY=translations
VUE_APP_LOCALSTORAGE_PARAGRAPHS_KEY_PREFIX=translation-

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ An alternative for Google Translator Toolkit orphans.

Supported translation APIs:

* None (no automatic translation)
* Google Translate

Supported storage drivers:
Expand Down Expand Up @@ -37,8 +38,6 @@ yarn serve

```bash
yarn build
firebase deploy --only firestore:rules
firebase deploy --only hosting
```

### Lints and fixes files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chunk from 'lodash.chunk'
import { LanguageCode } from '@/types'
import { TranslateFunc } from './types'

const API_KEY = process.env.VUE_APP_GOOGLE_TRANSLATE_API_KEY
const MAX_PARAGRAPHS_PER_REQUEST = 128
Expand All @@ -9,10 +9,10 @@ const sleep = (millis: number) => {
return new Promise(resolve => setTimeout(resolve, millis))
}

const translate = async (
targetLanguage: LanguageCode,
sourceText: string,
sourceLanguage: LanguageCode,
const translate: TranslateFunc = async (
targetLanguage,
sourceText,
sourceLanguage,
) => {
const sourceParagraphs = sourceText.split('\n\n')
const chunks = chunk(sourceParagraphs, MAX_PARAGRAPHS_PER_REQUEST)
Expand Down
16 changes: 16 additions & 0 deletions src/machine-translation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TranslationDriverMap } from './types'

import * as google from './google'
import * as none from './none'

const drivers: TranslationDriverMap = { google, none }
const chosenDriver = process.env.VUE_APP_TRANSLATION_DRIVER || 'none'
const driver = drivers[chosenDriver]

if (!driver) {
throw new Error(`Invalid translation driver "${chosenDriver}"`)
}

const translate = driver.translate

export { translate }
13 changes: 13 additions & 0 deletions src/machine-translation/none.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TranslateFunc } from './types'

const translate: TranslateFunc = async (
targetLanguage,
sourceText,
sourceLanguage,
) => {
return Promise.resolve().then(() => {
return sourceText
})
}

export { translate }
17 changes: 17 additions & 0 deletions src/machine-translation/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LanguageCode } from '@/types'

export interface TranslateFunc {
(
targetLanguage: LanguageCode,
sourceText: string,
sourceLanguage: LanguageCode,
): Promise<string>
}

export interface TranslationDriver {
translate: TranslateFunc
}

export interface TranslationDriverMap {
[key: string]: TranslationDriver
}
1 change: 0 additions & 1 deletion src/storage/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Store } from '@/store/store'
import { TranslationProject } from '@/types'

export interface ListTranslationsFunc {
Expand Down
2 changes: 1 addition & 1 deletion src/views/NewTranslation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { reactive } from 'vue'
import { editTranslation } from '@/helpers'
import { storeTranslation } from '@/storage'
import { v4 as uuid } from 'uuid'
import { translate as machineTranslation } from '@/machine-translation/google-cloud'
import { translate as machineTranslation } from '@/machine-translation'
export default {
components: {
Expand Down

0 comments on commit 830eaa5

Please sign in to comment.