Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for MyMemory Translators #2

Open
Prasanta-Hembram opened this issue Jun 2, 2023 · 2 comments
Open

Adding support for MyMemory Translators #2

Prasanta-Hembram opened this issue Jun 2, 2023 · 2 comments

Comments

@Prasanta-Hembram
Copy link

Hi, I have written a translator which uses Mymemory api but, I get result as undefined. :



class MyMemoryTranslator {

	translate = (text, from, to) => {

		// Set MyMemory API parameters
		const url = "https://api.mymemory.translated.net/get";
		const from1 = from;
		const to1 = to;
		const params = new URLSearchParams({
			q: text,
			de: "[email protected]"
		});

		// Send request to MyMemory API
		fetch(`${url}?${params}&langpair=${from1}|${to1}`)
			.then(response => response.json())
			.then(data => {
				// Get translated text from response
				const translatedText = data.responseData.translatedText;
				translatedText;

			});

	}

	translateBatch = (texts, from, to) =>
		Promise.all(texts.map((text) => this.translate(text, from, to)));

	getLengthLimit = () => 4000;
	getRequestsTimeout = () => 300;
	checkLimitExceeding = (text) => {
		const textLength = !Array.isArray(text) ? text.length : text.reduce((len, text) => len + text.length, 0);

		return textLength - this.getLengthLimit();
	};

	static isSupportedLanguage(language) {
		return MyMemoryTranslator.supportedLanguages.includes(language);
	}

	static getSupportedLanguages() {
		return [
			"en", "ar", "az", "zh", "cs",
			"nl", "eo", "fi", "fr", "de",
			"el", "hi", "hu", "id", "ga",
			"it", "ja", "ko", "fa", "pl",
			"pt", "ru", "sk", "es", "sv",
			"tr", "uk", "vi"
		];
	}

	static isSupportedAutoFrom() {
		return false;
	}
}


MyMemoryTranslator;


@AlphaBack
Copy link

class MyMemoryTranslator {
	
	apiPath = 'https://api.mymemory.translated.net/get';

	translate = (text, from, to) => {
		return fetch(`${this.apiPath}?q=${text}&langpair=${from}|${to}`, {
			credentials: 'omit',
			headers: {
				'User-Agent':
					'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0',
				Accept: '*/*',
				'Accept-Language': 'en-US,en;q=0.5',
				'Sec-Fetch-Dest': 'empty',
				'Sec-Fetch-Mode': 'cors',
				'Sec-Fetch-Site': 'same-origin',
			},
			method: 'GET',
			mode: 'cors',
		})
			.then((r) => r.json())
			.then(({ responseData }) => responseData.translatedText);
	};

	translateBatch = (texts, from, to) =>
		Promise.all(texts.map((text) => this.translate(text, from, to)));

	getLengthLimit = () => 4000;
	getRequestsTimeout = () => 300;
	checkLimitExceeding = (text) => {
		const textLength = !Array.isArray(text)
			? text.length
			: text.reduce((len, text) => len + text.length, 0);

		return textLength - this.getLengthLimit();
	};

	static isSupportedAutoFrom = () => true;

	// prettier-ignore
	static getSupportedLanguages = () => [
		"en", "ar", "az", "zh", "cs",
        "nl", "eo", "fi", "fr", "de",
        "el", "hi", "hu", "id", "ga",
        "it", "ja", "ko", "fa", "pl",
        "pt", "ru", "sk", "es", "sv",
        "tr", "uk", "vi"
	];
}

MyMemoryTranslator;

@Prasanta-Hembram
Copy link
Author

Thank you so much @AlphaBack for taking the time to solve this issue. Now it is working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants