Results 1 to 4 of 4

Thread: Want some guidance on translating texts in HTML snippets

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Want some guidance on translating texts in HTML snippets

    HTML snippet example:

    Code:
    <table class="classA"
        style="word-wrap: break-word;font-family: Arial, Helvetica, sans-senif;border-collapse: collapse;color: rgb(51, 51, 51);font-size: 12px;line-height: 18px;width: 700px;"
        border="1">
          <tbody>
            <tr>
              <td style="text-align: center;" colspan="2">
                <span></span>
                <br />
                <span style="font-family: Calibri;">
                  <span style="font-size: 18px; line-height: 27px;">
                    Text to translate <strong>Text to translate</strong>
                  </span>
                </span>
                <br />
                <span></span>
              </td>
            </tr>
            <tr>
              <td style="width: 500px;" rowspan="11">
                <img src="http://picture.jpg"
                alt="Text to translate" width="500" height="500" />
              </td>
              <td style="background-color: #f7f7f7; height: 35px; text-align: center;">
                <span style="font-size: 14px;">
                  <strong>
                    <span style="font-family: Calibri;">Text to translate</span>
                  </strong>
                </span>
              </td>
            </tr>
          </tbody>
        </table>
    Last edited by bPrice; Jul 26th, 2016 at 07:53 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: Want some guidance on translating texts in HTML snippets

    I am able to translate pure text via a translation API over HTTP, using for example, the below function which has the API calling wrapped in.

    Code:
    Public Function Translate(QueryText As String, SrcLanguage As LanguageType, DstLanguage As LanguageType) As String
    But the API doesn't directly support translating texts in HTML. So I have to write my own logic. As you can see that this is not a complete html document. It's just a snippet of code so I guess I can't just load it into DOM. My first thought is to use regex which I use all the time, but I am not sure how to correctly do it.

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Want some guidance on translating texts in HTML snippets

    I can show you how to get the strings that you have in red but not to translate them


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: Want some guidance on translating texts in HTML snippets

    Well, it has been some days and finally .. I got a reply.

    To get the text from the html snippet is not difficult, I can just use Regex to strip all the tags, and then look at what's left. However I need to translate the texts found and put them back to the html. I solved the problem by reusing mshtml.dll (I am not sure about the correctness in this ..):

    Code:
            htmlBodyObj.innerHTML = strCurrentHTML
    
            'Get reference of all textnodes
            GetTextNodeRecursive htmlBodyObj
    
            'Store all texts of textnodes in an array which later to be sent for translation together
            ReDim strTextNodeArray(1 To colTextnodesRefs.Count)
            i = 1
            For Each v In colTextnodesRefs
                strTextNodeArray(i) = v.NodeValue
                i = i + 1
            Next
            strTextNodeArray = ts.TranslateArray(strTextNodeArray, English, ts.GetLanguageType(strLanguage))
    
            'When translation finishes, transform the html
            i = LBound(strTextNodeArray)
            For Each v In colTextnodesRefs
                v.NodeValue = strTextNodeArray(i)
                i = i + 1
            Next
    I extract the textnodes, translate them, and reset the textnode value to the translated text. It will work on simple html but sometimes, one English word might reside in several, i.e. <span> element, and the text extracted will only be part of that word therefore translating not working properly ..

    It proved to be a much more difficult task.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width