Hack Forums
[Python] translate file from one lang to another - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] translate file from one lang to another (/showthread.php?tid=6049352)



translate file from one lang to another - Mr.7 - Feb 14, 2020

i am facing trouble with translating file from one lang to another. kindly please help me to complete it


RE: translate file from one lang to another - lockhead - Feb 14, 2020

I gotchu my dude

PHP Code

<?php 
import requests

with open
("file.txt", "r") as f:
    
text = f.readlines()
    
translate = requests.get("https://translate.google.com/#view=home&op=translate&sl=auto&tl=en&text={}".format(text))
    print(
translate.text) 



RE: translate file from one lang to another - Riԁdle - Feb 14, 2020

Could use https://pythonhosted.org/goslate/ , above post won't work, 1 it doesn't try to extract translated text from the form, 2 the form gets it's text set by javascript, so you'd need to use a browser (like selenium) then extract the text from the form, and 3 there is a limit (2000 I believe) on how many characters you can use in a url so you'd have to chunk the text you want to translate


RE: translate file from one lang to another - Mr.7 - Feb 15, 2020

(Feb 14, 2020 06:31 AM)Riԁdle Wrote:  Could use https://pythonhosted.org/goslate/ , above post won't work, 1 it doesn't try to extract translated text from the form, 2 the form gets it's text set by javascript, so you'd need to use a browser (like selenium) then extract the text from the form, and 3 there is a limit (2000 I believe) on how many characters you can use in a url so you'd have to chunk the text you want to translate


Getting this error🤦‍♂️

translated_line = gs.translate(line)
TypeError: translate() missing 1 required positional argument: 'target_language'


RE: translate file from one lang to another - Riԁdle - Feb 15, 2020

The basic usage is simple:

Code
>>> import goslate
>>> gs = goslate.Goslate()
>>> print(gs.translate('hello world', 'de'))
hallo welt

Example code from link above, you forgot the second parameter, which is the target_language, which looks like it takes abbreviated language name as a string


RE: translate file from one lang to another - Mr.7 - Feb 15, 2020

(Feb 15, 2020 06:14 AM)Riԁdle Wrote:  The basic usage is simple:

Code
>>> import goslate
>>> gs = goslate.Goslate()
>>> print(gs.translate('hello world', 'de'))
hallo welt

Example code from link above, you forgot the second parameter, which is the target_language, which looks like it takes abbreviated language name as a string

i am getting that but unable to translate whole document at a time🤦‍♂️...... i am trying whatever i can do i am doing but not able to do on complete document


RE: translate file from one lang to another - lockhead - Feb 15, 2020

(Feb 15, 2020 08:52 AM)Mr.7 Wrote:  i am getting that but unable to translate whole document at a time🤦‍♂️...... i am trying whatever i can do i am doing but not able to do on complete document

PHP Code

<?php 
import goslate

gs 
= goslate.Goslate()
inputFileName = "my_file.txt"
lang = "de"
with open(inputFileName, "r") as inputf:
 
   outputFileName = f"{inputFileName[-4:}_translate_{lang}.txt"
 
   with open(outputFileName, "w") as outputf:
 
       for line in inputf.readlines():
        
text = inputf.readlines()
        
outputf.write(gs.translate(line, lang)) 



RE: translate file from one lang to another - Mr.7 - Feb 16, 2020

(Feb 15, 2020 12:43 PM)lockhead Wrote:  
PHP Code

<?php 
import goslate

gs 
= goslate.Goslate()
inputFileName = "my_file.txt"
lang = "de"
with open(inputFileName, "r") as inputf:
 
   outputFileName = f"{inputFileName[-4:}_translate_{lang}.txt"
 
   with open(outputFileName, "w") as outputf:
 
       for line in inputf.readlines():
 
       text = inputf.readlines()
 
       outputf.write(gs.translate(line, lang)) 


its working but how to write lines in new line.....
its wrtting whole document in single line only


RE: translate file from one lang to another - lockhead - Feb 16, 2020

My dude do you have any idea what you're doing? You really shouldn't just ask people to do all the work for you if you don't have the slightest clue of what's going on.

PHP Code

<?php 
for line in inputf.readlines():
    
#if you remove the "#" a newline character will be added at the end of each line
    #line += "\n"
    
outputf.write(gs.translate(line, lang)) 



RE: translate file from one lang to another - Mr.7 - Feb 16, 2020

(Feb 16, 2020 02:38 AM)lockhead Wrote:  My dude do you have any idea what you're doing? You really shouldn't just ask people to do all the work for you if you don't have the slightest clue of what's going on.

PHP Code

<?php 
for line in inputf.readlines():
 
   #if you remove the "#" a newline character will be added at the end of each line
 
   #line += "\n"
 
   outputf.write(gs.translate(line, lang)) 

this is code

import goslate

gs = goslate.Goslate()
inputFileName = "data1.txt"
lang = "hi"
with open(inputFileName, "r" , encoding="utf8") as inputf:
outputFileName = f"{inputFileName[-4:]}_translate_{lang}.txt"

with open(outputFileName, "w", encoding="UTF-8") as outputf:
for line in inputf.readlines():

text = inputf.readlines()
line += "\n"
outputf.write(gs.translate(line, lang))

getting error like this
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 429: Too Many Requests


RE: translate file from one lang to another - lockhead - Feb 16, 2020

(Feb 16, 2020 02:44 AM)Mr.7 Wrote:  this is code

import goslate

gs = goslate.Goslate()
inputFileName = "data1.txt"
lang = "hi"
with open(inputFileName, "r" , encoding="utf8") as inputf:
   outputFileName = f"{inputFileName[-4:]}_translate_{lang}.txt"

   with open(outputFileName, "w", encoding="UTF-8") as outputf:
       for line in inputf.readlines():

           text = inputf.readlines()
           line += "\n"
           outputf.write(gs.translate(line, lang))

gettingerror like this
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib._error_.HTTPError: HTTPerror 429: Too Many Requests
PHP Code

<?php 
import goslate

gs 
= goslate.Goslate()
inputFileName = "my_file.txt"
lang = "de"
with open(inputFileName, "r") as inputf:
    
outputFileName = f"{inputFileName[-4:}_translate_{lang}.txt"
    
with open(outputFileName, "w") as outputf:
        
text = ""
        
for line in inputf.readlines():
            if 
line[-1] != "\n":
                
line += "\n"
            
text += line
        outputf
.write(gs.translate(text, lang))