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

Possible to specify translate.google.cn? #14

Closed
yucongo opened this issue Jun 4, 2021 · 10 comments
Closed

Possible to specify translate.google.cn? #14

yucongo opened this issue Jun 4, 2021 · 10 comments

Comments

@yucongo
Copy link

yucongo commented Jun 4, 2021

Hi. Thanks for the nice work.

I did some tests with translatepy with google (google_tr = Translator(use_google=True, use_yandex=False, use_bing=False, use_reverso=False, use_deepl=False).translate). It worked great.

However for users in some region that cannot access translate.google.com, they wont be able to use google in translatepy.

In googletrans you can do something like from googletrans import Translator; translator = Translator(service_urls=['translate.google.com', 'translate.google.co.kr',]). Is this possible with translatepy?

Thanks a lot.

@Animenosekai
Copy link
Owner

Thanks for using translatepy !

This feature isn't available yet on this library as I never understood the reason why people would want to choose to change the domain but now that you are giving me this example I think it would be great to have it in the future versions!

@Animenosekai
Copy link
Owner

Animenosekai commented Jun 14, 2021

@yucongo You will be able to change the services domain in v2 (specifying for example translate.google.cn for Google Translate) #15

@ffreemt
Copy link

ffreemt commented Jun 18, 2021

Thanks a lot.

How to use it? Translator([GoogleTranslate]).translate("Hello", "fr").result works (output: 'Bonjour'). Translator([GoogleTranslate("http://translate.google.cn")]).translate("Hello", "fr") results in errors: ValueError: Type of the parameter 'services_list' must be a class.

@Animenosekai
Copy link
Owner

Hmmm let me investigate this, I think I know where the problem is coming from

@ZhymabekRoman
Copy link
Contributor

Thanks for the bug report, @ffreemt. In general, it is not necessary to use the Translator class if you want to use only one translator service, you can directly use the class GoogleTranslate(service_url="translate.google.cn"). translate("hello", "fr")

@ZhymabekRoman
Copy link
Contributor

@Animenosekai , I have prepared a GIT patch that fixes this issue:

~/git/translate $ git diff
diff --git a/translatepy/translate.py b/translatepy/translate.py
index 4015beb..9730814 100644
--- a/translatepy/translate.py
+++ b/translatepy/translate.py
@@ -37,13 +37,13 @@ class Translate():

         self.services = []
         for service in services_list:
-            if not isinstance(service, type):
-                raise ValueError("Type of the parameter 'services_list' must be a class")
-
-            if not issubclass(service, BaseTranslator):
-                raise TypeError("Type of the parameter 'services_list' must be a child class of BaseTranslator class")
-
-            self.services.append(service(request=request))
+            if not isinstance(service, BaseTranslator):
+                if not issubclass(service, BaseTranslator):
+                    raise TypeError("{service} must be a child class of BaseTranslator class".format(service=service))
+                else:
+                    self.services.append(service(request=request))
+            else:
+                self.services.append(service)

     def translate(self, text: str, destination_language: str, source_language: str = "auto") -> TranslationResult:
         """
~/git/translate $ python
Python 3.9.5 (default, May  5 2021, 12:34:14)
[Clang 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from translatepy import Translate
>>> from translatepy.translators import GoogleTranslate
>>> dl = Translate([GoogleTranslate(service_url="translate.google.cn")])
>>> dl.translate("Hello", "fr")
TranslationResult(service=Google, source=Hello, source_language=en, destination_language=fr, result=Bonjour)
>>> exit()

@Animenosekai
Copy link
Owner

+            if not isinstance(service, BaseTranslator):
+                if not issubclass(service, BaseTranslator):
+                    raise TypeError("{service} must be a child class of BaseTranslator class".format(service=service))
+                else:
+                    self.services.append(service(request=request))
+            else:
+                self.services.append(service)

Nice, that's exactly what I was about to do!

@ffreemt
Copy link

ffreemt commented Jun 19, 2021

Thanks guys.

Got another problem though.

GoogleTranslate().translate("hello", "zh") raise TypeError('Parameter 'language' must be a string or Language instance, {} was given'.format(type(language).__name__))

Also tried chinse, Chinese, chi, same result. Language("chinese") etc. runs OK.

@ZhymabekRoman
Copy link
Contributor

ZhymabekRoman commented Jun 19, 2021

Thanks, @ffreemt ! I seem to have found the culprit of the problem:

~/git/translate $ python3
Python 3.9.5 (default, May  5 2021, 12:34:14) 
[Clang 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from translatepy import Language
>>> Language.by_google("zh")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/sdext2/data/com.termux/files/home/git/translate/translatepy/language.py", line 101, in by_google
    return cls(iso639.by_google.get(language_code.lower()))
  File "/data/sdext2/data/com.termux/files/home/git/translate/translatepy/language.py", line 16, in __init__
    raise TypeError("Parameter 'language' must be a string or Language instance, {} was given".format(type(language).__name__))
TypeError: Parameter 'language' must be a string or Language instance, NoneType was given

This is because the value zh is not in the Google column of Table ISO639. We are already discussing an alternative implementation #17

@ffreemt
Copy link

ffreemt commented Jun 19, 2021

In fact, I also tried GoogleTranslate().translate("hello", "zh-cn") and GoogleTranslate().translate("hello", "zh-CN"). Doesnt work either.

Language('chinese').google outputs 'zh-cn' by the way. The website seems to use zh-CN https://translate.google.com/?sl=de&tl=zh-CN&text=test&op=translate

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

4 participants