Skip to content

Commit fa48ffa

Browse files
committed
update
1 parent 68495d3 commit fa48ffa

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

demo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import googletrans as gt
2+
3+
# Translate text from English to Korean
4+
print(gt.translate("Hello, How are you?", "ko"))
5+
6+
# Translate text from English to Japanese
7+
print(gt.translate("Hello, How are you?", "ja"))
8+
9+
# Translate text from English to French
10+
print(gt.translate("Hello, How are you?", "fr"))

googletrans/__init__.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
"""
2-
googletrans-python
3-
"""
41
import re
52
import html
63
import urllib.request
74
import urllib.parse
85

9-
__version__ = '0.1.0.0'
6+
__version__ = "0.2.0"
7+
8+
def translate(to_translate: str, to_language: str = "auto", from_language: str = "auto") -> str:
9+
"""
10+
Translates a text from one language to another using the Google Translate API.
11+
12+
Args:
13+
to_translate (str): The text to translate.
14+
to_language (str, optional): The language to translate the text to. Defaults to "auto".
15+
from_language (str, optional): The language of the text to translate. Defaults to "auto".
16+
17+
Returns:
18+
str: The translated text.
19+
"""
20+
link = f"http://translate.google.com/m?tl={to_language}&sl={from_language}&q={urllib.parse.quote(to_translate)}"
21+
request = urllib.request.Request(link, headers={
22+
'User-Agent': "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1;.NET CLR 1.1.4322;.NET CLR 2.0.50727;.NET CLR 3.0.04506.30)"})
23+
re_result = re.findall(
24+
r'(?s)class="(?:t0|result-container)">(.*?)<', urllib.request.urlopen(request).read().decode("utf-8"))
25+
return "" if len(re_result) == 0 else html.unescape(re_result[0])
1026

11-
def translate(to_translate, to_language="auto", from_language="auto"):
12-
link = f"http://translate.google.com/m?tl={to_language}&sl={from_language}&q={urllib.parse.quote(to_translate)}"
13-
request = urllib.request.Request(link, headers={'User-Agent':"Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1;.NET CLR 1.1.4322;.NET CLR 2.0.50727;.NET CLR 3.0.04506.30)"})
14-
re_result = re.findall(r'(?s)class="(?:t0|result-container)">(.*?)<', urllib.request.urlopen(request).read().decode("utf-8"))
15-
return "" if len(re_result) == 0 else html.unescape(re_result[0])
1627

1728
if __name__ == "__main__":
18-
print(translate("Hello, My name is python.", "ko"))
29+
print(translate("Hello, my name is Python.", "ko"))
30+

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
'Programming Language :: Python :: 3.6',
2525
'Programming Language :: Python :: 3.7',
2626
'Programming Language :: Python :: 3.8',
27-
'Programming Language :: Python :: 3.9',
2827
],
2928
test_suite = 'tests',
3029
tests_require = test_requirements

0 commit comments

Comments
 (0)