Add experimental feature to translate links
This CL adds support for translating links directly from the context
menu. This support is experimental, since many details of the
implementation might change after testing it for a while and finding
ways to improve it (hopefully also thanks to the feedback given by folks
who might also test this).
For this reason, the feature is only enabled in Canary builds of the
extension for now (or when the extension is being developed).
This CL also changes the Crowdin configuration file so strings that
change in the source file and are uploaded for translation don't remove
the existing translations. This is because although no strings are
changed in this CL, the description/context of 2 strings has changed,
and this is to be sure that the translations are not erased.
Bug: translateselectedtext:7
Change-Id: I862af18eee8830c901e7fe7d2b473cab43d9bbe7
diff --git a/src/common/urlFactory.ts b/src/common/urlFactory.ts
new file mode 100644
index 0000000..6e410af
--- /dev/null
+++ b/src/common/urlFactory.ts
@@ -0,0 +1,38 @@
+import {DataType} from '../background';
+
+export default class URLFactory {
+ static getTranslationURL(
+ lang: string, info: chrome.contextMenus.OnClickData,
+ dataType: DataType) {
+ switch (dataType) {
+ case DataType.DataTypeText:
+ return URLFactory.getTranslationURLForText(lang, info.selectionText);
+
+ case DataType.DataTypeURL:
+ return URLFactory.getTranslationURLForURL(lang, info.linkUrl);
+
+ default:
+ console.error('Can\'t return translation URL for unknown data type.');
+ return 'about:blank?translate_selected_text_error';
+ }
+ }
+
+ static getTranslationURLForText(lang: string, text: string): string {
+ const params = new URLSearchParams({
+ sl: 'auto',
+ tl: lang,
+ text: text,
+ op: 'translate',
+ });
+ return 'https://translate.google.com/?' + params.toString();
+ }
+
+ static getTranslationURLForURL(lang: string, url: string): string {
+ const params = new URLSearchParams({
+ sl: 'auto',
+ tl: lang,
+ u: url,
+ });
+ return 'https://translate.google.com/translate?' + params.toString();
+ }
+}