Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translation with google
02-03-2014, 01:58 AM, (This post was last modified: 02-03-2014, 02:33 AM by edumarzolla.)
#1
Translation with google
I developed a macro in VBA Excel to translate a PO file using Google translator.
Hope it helps you...

edumarzolla

Code:
' using notepad++, replace all \n by \r\n and save the file
' after translate all the file,
' using notepad++, convert the encode to UFT-o withot BOM; replace all \r\n by \n and save the file
' using POeditor (free) you can open and save our file. The MO file will be generated with same name with same folder
' both files (PO and MO) must be saved on the correct language folder on your server (locale folder, and the name of the files must be messages.*)

' Run the TranslateFile SUB (before run, click CTRL+G to see the status of the transations)... it takes a long time... but works


  Sub TranslateFile()
    Dim s As String, textline As String, ss As String
    Dim sss() As String
    Dim i As Integer
    
    Close
    Open "write_the_original_PO_file_here_example_c:\edu.po" For Input As #1
    Open "write_the_NEW_PO_file_here_example_c:\edu_new.po" For Output As #2
    
    
    i = 0
    Do While Not EOF(1)
        Line Input #1, textline
        If InStr(textline, "msgid") > 0 Then
            While InStr(textline, "msgstr") = 0
                ReDim Preserve sss(i)
                sss(i) = textline
                i = i + 1
                Line Input #1, textline
            Wend
            
            For i = 0 To UBound(sss)
                Print #2, sss(i)
                Debug.Print Time & " - " & sss(i)
        
            Next
        
            For i = 0 To UBound(sss)
                If InStr(sss(i), "msgid") > 0 Then
                   ss = Replace(sss(i), "msgid", "")
                   If Trim(ss) = Chr(34) & Chr(34) Then
                        ss = "msgstr" & ss
                        Print #2, ss
                        Debug.Print Time & " - " & ss
                   Else
                        sss(i) = transalte_using_vba(Replace(sss(i), "msgid", ""))
                        sss(i) = "msgstr " & sss(i)
                        Print #2, sss(i)
                        Debug.Print Time & " - " & sss(i)
                   End If
                Else
                   sss(i) = transalte_using_vba(sss(i))
                   Print #2, sss(i)
                   Debug.Print Time & " - " & sss(i)
                End If
            Next
            
            i = 0
        
        
        Else
            Print #2, textline
            Debug.Print Time & " - " & textline
        End If

    Loop

End Sub

Function transalte_using_vba(str) As String
' Tools Refrence Select Microsoft internet Control

On Error GoTo erro

    Dim IE As Object, i As Long
    Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA

    Set IE = CreateObject("InternetExplorer.application")
    '   TO CHOOSE INPUT LANGUAGE


    inputstring = "en-US"  'change by the original language

    '   TO CHOOSE OUTPUT LANGUAGE

    outputstring = "pt-BR"  'change to language you want to translate

    text_to_convert = str

    'open website

    IE.Visible = False
    IE.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

    Application.Wait (Now + TimeValue("0:00:3"))

    Do Until IE.ReadyState = 4
        DoEvents
    Loop

    CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")

    For j = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
        result_data = result_data & Right(CLEAN_DATA(j), Len(CLEAN_DATA(j)) - InStr(CLEAN_DATA(j), ">"))
    Next


    IE.Quit
    transalte_using_vba = result_data
Exit Function

erro:
If Err.Number = -2147467259 Then
Debug.Print Err.Description
Resume
End If

End Function
_____________________
Eduardo Marzolla
skype: edumarsola
Reply
02-03-2014, 03:19 AM,
#2
RE: Translation with google
Did you mean to attach the excel file
Phil Daintree
webERP Admin
Logic Works Ltd
http://www.logicworks.co.nz
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)