Language Translator (1 Viewer)

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
this thread: (2) Solved - Possible to change text formatting through VBA? | Access World Forums (access-programmers.co.uk)
lead to to create this sample dataase.
Again, the code is not mine and I made a Link back to the original webite (see modTranslate).
I am not sure how much text can be translated. It's for you to find out.
You are responsible to debug, if there are errors.
I used the "original" code (using Excel automation).
If you can translate the excel function to pure VBA, then
we can incorporate it so we can discard Excel automation totally.
"encodeURL" is easy, there are lots of example in the net.

What is missing? audio translation.

Happy translating to all.
 

Attachments

  • Translator.accdb
    528 KB · Views: 189

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
Thanks for finding this code @arnelgp.
I've often wondered whether it was possible to incorporate Google Translate into Access. This could be very useful

I've made one addition to the code to reinstate any apostrophes in the translated text e.g. C'est, aujhourd'hui

Before After
1630139361093.png
1630139439916.png

This just requires one extra line at the end of the Translate function:
Rich (BB code):
 Translate = Replace(Translate, "'", "'") 'added by isladogs - 28/08/2021

In principle, it should be simple to add audio translate.
However, I don't know how well it would work in practice unless you have a 'dedicated voice' for the translated language
 

Attachments

  • Translator_v2.zip
    35.2 KB · Views: 171

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
thanks Isla.
btw, you need Internet connection to use Translate,
just as you would use Google Translate.

I added a function (CleanString), just in case there are other ("&#" + number + ";") characters
that might come out.
 

Attachments

  • Translator_V3.accdb
    600 KB · Views: 141

CJ_London

Super Moderator
Staff member
Local time
Today, 09:22
Joined
Feb 19, 2013
Messages
14,181
I see this uses the encodeurl function in excel - I believe that did not become available until excel 2013

this link provides some homegrown vba equivalents
 

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
Hi again
I did wonder whether other unwanted characters would turn up. Unfortunately neither my v2 nor your v3 handle quotation marks correctly.
For example (original on left, fixed version on right)

1630155754058.png
1630155801045.png


I've handled that by adding another replace line
Rich (BB code):
  Translate = Replace(Translate, """, """") 'added by isladogs - 28/08/2021

You may prefer to modify your CleanString function.

However, I've tried translating various phrases into languages using different character sets including Georgian, Tamil, Urdu & Telugu
All worked perfectly in my modified v2
So I'm just wondering what other unwanted characters apart from single/double quotes you have come across that need cleaning.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
incorporating change on "&quote;"
still using Excel though.
i cannot make the getHTTP to work.
it does not display "foreign" characters correctly.
will only work with Excel 2013 and up.
 

Attachments

  • Translator V4.accdb
    576 KB · Views: 146

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
i've found another site that does not use Excel totally.
and it does not need the CleanString() function?
make more test. hopefully this is the final version.
 

Attachments

  • Translator_V5.accdb
    860 KB · Views: 147

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
Thanks
Version 5 seems to work perfectly in my tests. It also no longer has the initial delay experienced on each first use of the Excel version.

I've added reverse translate and copy to clipboard in my copy and intend to test the use of audio (text to speech) in the next few days.
Of course, voices that are suitable for e.g. European languages cannot read other character sets such as Chinese and Arabic.
So it will probably only be a partial implementation at this stage.

Anyway, I'll wait to upload any additional features until I know you've finished modifying the main code functionality
 
Last edited:

Nixversteher

New member
Local time
Today, 01:22
Joined
Feb 25, 2018
Messages
13
I am not sure how much text can be translated
I think there can be a maximum of 2048 characters. However, since each character of the plaintext can be split into up to 3 escape sequence characters, the plaintext should have fewer characters.

Urlencode without excel.
 

Attachments

  • mTranslate.bas.txt
    1.8 KB · Views: 135

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
After further tests using @arnelgp's version 5, I've now added several new features, most importantly the use of text to speech (TTS)

In addition, the following items are included:
a) code to check for an Internet connection when the form is loaded
b) reverse translate - useful for checking accuracy of Google translate and for translating into additional languages
c) copy text to clipboard
d) character count (including spaces etc) - tests indicate a limit of around 9800 characters can be translated - more than as @Nixversteher suggested.

The selected text is exported to a VB script file (Text2Speech.vbs in the program folder) which is then 'executed' i.e. read aloud in the selected voice

For the best results, install additional voices from text to speech settings
If no voice is available for the selected language, the text is spoken in the default language:

As a test, I installed voices for all available languages. There are a total of 75 voices in 48 languages.
For details, see Appendix A: Supported languages and voices (microsoft.com)

Disappointingly only 16 of those voices were available on my test workstations using text to speech:
16 available voices:
0 - Microsoft Hazel Desktop - English (Great Britain)
1 - Microsoft Hedda Desktop - German
2 - Microsoft David Desktop - English (United States)
3 - Microsoft Zira Desktop - English (United States)
4 - Microsoft Helena Desktop - Spanish (Spain)
5 - Microsoft Sabina Desktop - Spanish (Mexico)
6 - Microsoft Hortense Desktop - French
7 - Microsoft Elsa Desktop - Italian (Italy)
8 - Microsoft Haruka Desktop - Japanese
9 - Microsoft Heami Desktop - Korean
10 - Microsoft Paulina Desktop - Polish
11 - Microsoft Maria Desktop - Portuguese(Brazil)
12 - Microsoft Irina Desktop - Russian
13 - Microsoft Huihui Desktop - Chinese (Simplified)
14 - Microsoft Tracy Desktop - Chinese(Traditional, HongKong SAR)
15 - Microsoft Hanhan Desktop - Chinese (Taiwan)


Voices 8, 9, 12, 13, 14, 15 can't be used for TTS in Access as the character set cannot be read
As a result, languages such as Chinese, Japanese or Russian aren't exported to the VBS file

However, In my tests, the following 8 languages all worked successfully:
8 available voices:
0 - Microsoft Hazel Desktop - English (Great Britain)
1 - Microsoft Hedda Desktop - German
2 - Microsoft Zira Desktop - English (United States)
3 - Microsoft Helena Desktop - Spanish (Spain)
4 - Microsoft Hortense Desktop - French
5 - Microsoft Elsa Desktop - Italian (Italy)
6 - Microsoft Paulina Desktop - Polish
7 - Microsoft Maria Desktop - Portuguese(Brazil)


I would be very interested if anyone can explain how to make use of all additional voices which can be installed
Other feedback would be welcomed.
For more details, see my separate thread: Translate & Speak | Access World Forums (access-programmers.co.uk)

NOTE:
I didn't use the code provided by @Nixversteher as its fairly similar to that in @arnelgp's v5.
However that code helped explain why the maximum URL length in Internet Explorer is 2083 characters.
IE is still used in the current Access web browser control.
A new browser control based on Chromium Edge will be released in the near future and, amongst other benefits, this will have a far larger character limit for URLs
 

Attachments

  • Translate&Speak.PNG
    Translate&Speak.PNG
    39.3 KB · Views: 166
  • References.PNG
    References.PNG
    19.4 KB · Views: 161
  • Textt2Speech.PNG
    Textt2Speech.PNG
    33.5 KB · Views: 160
  • SpeechNA.PNG
    SpeechNA.PNG
    4.7 KB · Views: 160
  • VoiceNA.PNG
    VoiceNA.PNG
    5.1 KB · Views: 171
  • Translate&Speak.zip
    82.9 KB · Views: 154
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
i will not touch the "voice" translation, but
i think i manage to increase the translation from 2048 (2 ^ 11) to 65536 (2 ^ 16).
i have successfully translated 5 paragraph of Lorem ipsum.
the resulting translation length is 3996.
 

Attachments

  • Translator_V6A.accdb
    940 KB · Views: 137

Nixversteher

New member
Local time
Today, 01:22
Joined
Feb 25, 2018
Messages
13
That's right, the url can now be much longer than the 20xx characters I mentioned. But I think the free version of Google Translator only accepts up to 5000 characters of text.
 

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
i will not touch the "voice" translation, but
i think i manage to increase the translation from 2048 (2 ^ 11) to 65536 (2 ^ 16).
i have successfully translated 5 paragraph of Lorem ipsum.
the resulting translation length is 3996.

Just did a quick test with a 7000+ character text string (the text from post #10) which I 'translated' to various languages
The output was identical in your version 5 & 6A and in my version

I've checked and all the text is included in the translated version. So Google Translate isn't limited to 5000 characters

In any case, I'm not sure how your added code in v6A would work.

Code:
 'arnelgp
    Dim s As String
 
    With objHTML
        .Open
        'arnelgp
        s = objHTTP.responsetext
        s = Replace$(s, "maxlength=""2048""", "maxlength=""65536""")    'replace 2 ^ 11 (2048) with 2 ^ 16 (65536)a
       ' .write objHTTP.responsetext
        .write objHTTP.responsetext
        .Close
    End With

You've defined the string variable s & your Replace line is obviously intended to increase the max length as stated
However, it seems you don't then use s subsequently. Am I missing something?

UPDATE:
Rather than continue to discuss two separate apps in one thread, I've now created a separate thread for the Translate & Speak app:
Translate & Speak | Access World Forums (access-programmers.co.uk)

Thanks again for finding the Google Translate code
 
Last edited:

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
UPDATE
Just found issues in the GglTranslate function with 4 characters (&, +, %, #) which are also used in URLs.
All are obvious with hindsight
The '+' is ignored and any text after & including '&' or '#' is omitted. Using a % results in all spaces becoming %20 as well

So testing with a phrase like 'You + me & Fred' results in the output 'You me' (if 'translated' to English!)
Similarly '50% profit #3' becomes '50%%20profit%20'

For now, I've done a quick work round replacing those 4 characters with very unlikely character strings (at least unlikely in English)
I have also added code to handle line returns correctly

Rich (BB code):
Public Function GglTranslate(strInput As String, FrmLng As String, ToLng As String) As String

    Dim strURL As String
    Dim objHTTP As Object
    Dim objHTML As Object
    Dim objDivs As Object, objDiv As Object
    Dim strTranslated As String
    
    'Isladogs - code to handle characters also used in URLs
    Dim strInputMod As String
    strInputMod = Replace(strInput, "&", "ZGZ")
    strInputMod = Replace(strInputMod, "+", "QZQ")
    strInputMod = Replace(strInputMod, "%", "XGQ")
    strInputMod = Replace(strInputMod, "#", "KQJ")

    'handle line returns(vbCrLf)
    strInputMod = Replace(strInputMod, Chr(13) & Chr(10), "MQW")

    ' send query to web page
    strURL = "https://translate.google.com/m?hl=" & FrmLng & _
        "&sl=" & FrmLng & _
        "&tl=" & ToLng & _
        "&ie=UTF-8&prev=_m&q=" & strInputMod

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") 'late binding
    objHTTP.Open "GET", strURL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.Send ""
    ' create an html document
    Set objHTML = CreateObject("htmlfile")
    With objHTML
        .Open
        .Write objHTTP.responseText
        .Close
    End With

    Set objDivs = objHTML.getElementsByTagName("div")

    For Each objDiv In objDivs

        If objDiv.classname = "result-container" Then
            strTranslated = objDiv.innertext
            If strTranslated <> "" Then
                GglTranslate = strTranslated
                Exit For
            End If
        End If
        'Debug.Print objDiv.classname, objDiv.innertext
    Next objDiv

    'Isladogs - code to restore characters also used in URLs
    GglTranslate = Replace(GglTranslate, "ZGZ", "&")
    GglTranslate = Replace(GglTranslate, "QZQ", "+")
    GglTranslate = Replace(GglTranslate, "XGQ", "%")
    GglTranslate = Replace(GglTranslate, "KQJ", "#")

     'code to restore line returns
    GglTranslate = Replace(GglTranslate, "MQW", Chr(13) & Chr(10))

    Set objHTML = Nothing
    Set objHTTP = Nothing

End Function

EDIT: I have just found out that the limit is 9800 characters. If you exceed that, there is no output.
Interestingly, the character limit if you type / paste text directly into Google Translate is just 500!

Hope that helps
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
yes, should have been:
Code:
    With objHTML
        .Open
        'arnelgp
        s = objHTTP.responsetext
        s = Replace$(s, "maxlength=""2048""", "maxlength=""65536""")    'replace 2 ^ 11 (2048) with 2 ^ 16 (65536)
        '.write objHTTP.responsetext
        .write s
        .Close
    End With
again, i am not going into "voice" translation since not all language can be voice-translate, specially arabic.
let the users add that on their own language.

and yes, post another thread, this is supposed to be my sample database thread and you keep showing up grabbing the limelight.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:22
Joined
May 7, 2009
Messages
16,086
now with "limited" voice translation.
the voice synthesizer comes from GNU: eSpeak: Speech Synthesizer (sourceforge.net)
there are limited language (i think around 40).
you need to install eSpeak to have voice translation.

so whether you are using old xp os, voice is available to you.
 

Attachments

  • Translator_V7.accdb
    672 KB · Views: 164
Local time
Today, 09:22
Joined
Jul 25, 2020
Messages
61
Spell Checker, Goole Translator XMLHTTP Requests & Early Binding of the Microsoft Speech Object Library.
 

Attachments

  • Translate.zip
    1.5 MB · Views: 181

isladogs

CID VIP
Local time
Today, 09:22
Joined
Jan 14, 2017
Messages
16,062
now with "limited" voice translation.
the voice synthesizer comes from GNU: eSpeak: Speech Synthesizer (sourceforge.net)
there are limited language (i think around 40).
you need to install eSpeak to have voice translation.

so whether you are using old xp os, voice is available to you.
An early version of text to speech with one voice (Microsoft Sam) was built in to Windows XP
How do I... Configure Text-to-Speech in Microsoft Windows XP - TechRepublic

... though TTS has improved dramatically in the 20 or so years since XP
 
Last edited:
Local time
Today, 09:22
Joined
Jul 25, 2020
Messages
61
A couple of Windows 10 Voices (Mark & Eva) for anyone that can make use of them..
Just unzip and Double Click to invoke the registry entry...
 

Attachments

  • windows-10-voices-add.zip
    1.8 KB · Views: 152

Users who are viewing this thread

Top Bottom