-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add new translator - Translators.LibreTranslate. - Defined translators "API" has been changed again, configuration must be updated. DefinedTranslators has been renamed to Translators. Every pre-defined translator is a function now. It's for translators to have configuration parameters like api key and stuff like that. Instead of `DefinedTranslators.GOOGLE_TRANSLATE` you should use `Translators.GoogleTranslate()`. Debug translators have been moved to the `DefinedTranslators.debug` object. `Translators` can be extended by your own translators. Pre-defined translators cannot be deleted or modified (or at least not supposed to be). - Configuration loading optimizations: it's now cached if you don't touch the profiles settings. It means a configuration code itself might not be executed on every new sentence. The functions passed to configuration parameters are called as before. Still, it's better not to include initialization logic in the outer scope of configuration scripts. - Improve types in JS editor - Add `URL`, `URLSearchParams`, node.js `net` variables to configs
- Loading branch information
Showing
16 changed files
with
342 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,40 @@ | ||
import indexArrayBy from '../utils/indexArrayBy'; | ||
import {COMMON_PROFILE_ID} from '../windows/settings/profiles/constants'; | ||
import getConfiguration from './getConfiguration'; | ||
import type Store from 'electron-store'; | ||
import {Configuration} from './Configuration'; | ||
import initializeSavedProfiles from './initializeSavedProfiles'; | ||
import {StoreKeys} from '../constants/store-keys'; | ||
import SavedProfiles from '../windows/settings/profiles/SavedProfiles'; | ||
import ref from '../utils/ref'; | ||
import electronStore from '../electron-store/electronStore'; | ||
import store from '../electron-store/store'; | ||
|
||
const configurationCache = ref<Configuration>(); | ||
|
||
store.onDidChange(StoreKeys.SAVED_PROFILES, () => { | ||
configurationCache.current = undefined; | ||
}); | ||
|
||
electronStore.onDidChange(StoreKeys.SAVED_PROFILES, () => { | ||
configurationCache.current = undefined; | ||
}); | ||
|
||
const getProfileConfig = (profileId?: string): Configuration => { | ||
const cache = configurationCache.current; | ||
if (cache) { | ||
return cache; | ||
} | ||
|
||
const getProfileConfig = (store: Store, profileId?: string): Configuration => { | ||
const savedProfiles = store.get(StoreKeys.SAVED_PROFILES) as (SavedProfiles | undefined) ?? initializeSavedProfiles(store); | ||
const savedProfilesMap = indexArrayBy(savedProfiles.profiles, 'id'); | ||
|
||
const commonProfile = savedProfilesMap.get(COMMON_PROFILE_ID); | ||
const activeProfileId = profileId ?? savedProfiles.activeProfileId; | ||
const activeProfile = activeProfileId ? savedProfilesMap.get(activeProfileId) : undefined; | ||
|
||
return getConfiguration(commonProfile?.configSource, activeProfile?.configSource); | ||
const result = getConfiguration(commonProfile?.configSource, activeProfile?.configSource); | ||
configurationCache.current = result; | ||
return result; | ||
}; | ||
|
||
export default getProfileConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Store from 'electron-store'; | ||
|
||
const store = new Store(); | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
import {DefinedTranslators, Translator} from '../configuration/Configuration'; | ||
import {DefinedTranslators, LibreTranslatorConfig} from '../configuration/Configuration'; | ||
import GoogleTranslator from './translators/GoogleTranslator'; | ||
import IdentityTranslator from './translators/utility/IdentityTranslator'; | ||
import InfiniteTranslator from './translators/utility/InfiniteTranslator'; | ||
import NoneTranslator from './translators/utility/NoneTranslator'; | ||
import LibreTranslator from './translators/LibreTranslator'; | ||
|
||
class IdentityTranslator implements Translator { | ||
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
return Promise.resolve(text); | ||
} | ||
} | ||
|
||
class NoneTranslator implements Translator { | ||
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
return undefined as any; | ||
} | ||
} | ||
|
||
class InfiniteTranslator implements Translator { | ||
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
return new Promise<string>(() => {}); | ||
export class DefinedTranslatorsImpl implements DefinedTranslators { | ||
constructor() { | ||
Object.keys(this).forEach((keyName) => { | ||
const key = keyName as keyof DefinedTranslatorsImpl; | ||
Object.defineProperty(this, key, { | ||
value: this[key], | ||
writable: false, | ||
configurable: false, | ||
enumerable: true | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export class DefinedTranslatorsImpl implements DefinedTranslators { | ||
GOOGLE_TRANSLATE = new GoogleTranslator(); | ||
X_IDENTITY = new IdentityTranslator(); | ||
X_NONE = new NoneTranslator(); | ||
X_INFINITE = new InfiniteTranslator(); | ||
GoogleTranslate = () => { | ||
return new GoogleTranslator(); | ||
}; | ||
|
||
public static INSTANCE: DefinedTranslators = new DefinedTranslatorsImpl(); | ||
LibreTranslate = (config?: LibreTranslatorConfig) => { | ||
return new LibreTranslator(config || {}); | ||
}; | ||
|
||
private constructor() {} | ||
debug = Object.freeze({ | ||
Identity: () => new IdentityTranslator(), | ||
Infinite: () => new InfiniteTranslator(), | ||
None: () => new NoneTranslator() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import httpRequest from '../../utils/httpRequest'; | ||
import {LibreTranslatorConfig, Translator} from '../../configuration/Configuration'; | ||
|
||
class LibreTranslator implements Translator { | ||
constructor(private readonly config: LibreTranslatorConfig) { | ||
} | ||
|
||
async translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
const host = this.config.host || 'https://libretranslate.com'; | ||
const url = new URL('/translate', host).toString(); | ||
|
||
const responseData: any = await httpRequest(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
q: text, | ||
source: sourceLanguage, | ||
target: targetLanguage, | ||
format: this.config.format, | ||
api_key: this.config.apiKey | ||
}) | ||
}).then((response) => response.json()); | ||
|
||
if (!responseData || responseData.translatedText == null) { | ||
console.error('LibreTranslator unable to translate: ', responseData); | ||
throw responseData; | ||
} | ||
|
||
return responseData.translatedText as string; | ||
} | ||
} | ||
|
||
export default LibreTranslator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Translator} from '../../../configuration/Configuration'; | ||
|
||
class IdentityTranslator implements Translator { | ||
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
return Promise.resolve(text); | ||
} | ||
} | ||
|
||
export default IdentityTranslator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Translator} from '../../../configuration/Configuration'; | ||
|
||
class InfiniteTranslator implements Translator { | ||
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
return new Promise<string>(() => {}); | ||
} | ||
} | ||
|
||
export default InfiniteTranslator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {Translator} from '../../../configuration/Configuration'; | ||
|
||
class NoneTranslator implements Translator { | ||
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string> { | ||
return undefined as any; | ||
} | ||
} | ||
|
||
export default NoneTranslator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.