sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Google translate in a webDialog?

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 3 Posters 562 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D Offline
      driven
      last edited by

      hi has anyone added google translate to a plugin?

      I know it's a pain to do conventionally, as a webdialog is not a website so you can't use the normal route...

      I've been hacking on an alternative which works on my mac, but has some timing issues so I thought I'd post it to garner comments and advice...

      it may work on PC's of other persuasions...

      don't have access so not sure if my liberal shortcutting will work...

      it's part of a 'namespace d' script for users to translate my own plugin themselves, so this is just the barebones that I can get to work...

      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      # waiting("round corners")
        @ready = false
        @steady = false
        @go = false
        @source = ""
      
        def get_source
          source = ""
          source = @dlg1.get_element_value("source")
          @source = source
        end
      
        def setup_trans_late
          url = "http://translate.google.com/translate_t?text=" + "hello"
          @dlg1 = UI;;WebDialog.new
          @dlg1.set_url(url)
          #@dlg1.show_modal
               ready = (UI.start_timer(0.1, true) {
               if not @source === "hello"
               get_source
            else
               UI.stop_timer(ready)
            end
            })
               @ready = true
               puts "ready = #{@ready}"
        end
      
        def set_return(input)
            script = %Q(source.value=") + input + '"'
            @dlg1.execute_script(script.to_s)
            steady = (UI.start_timer(0.1, true) {
             if not @source === (input.to_s)
               get_source
             else
               UI.stop_timer(steady)
             end
            })
               @steady = true
               puts "steady = #{@steady}"
        end
      
       def reset
         @ready = true # until close
         @steady = false
         @go = false
         p 'reset'; nil
       end
      
        def get_trans
           @dlg1.execute_script("
      
           val=result_box.textContent
           var original_html=result_box.innerHTML
               original_html=original_html.replace('<span', '<input ' + 'id= for_su ' + 'value= "" ')
               original_html=original_html.replace('</span>', '</ >')
           result_box.innerHTML=original_html
           for_su.value=val
           ")
                 ret = ""
                 ret = @dlg1.get_element_value("for_su")
                 @ret = ret
                 p @ret if not @ret === ""
                 @go = true
                 puts "go = #{@go}"
                 reset
      
       end
      
      
       setup_trans_late if not @ready
      # set_return('square corner') if @ready
      # get_trans if @steady
      
      def show_dialog
        @dlg2 = UI;;WebDialog.new("Translation_Test", false,
         "trans_L8", 200, 150, 150, 150, true);
        html = <<-HTML
      <html><head></head><body>
            <input id="test"  type="text" >
            <input id="start2" onclick="trans_L8()" type="button" value="translate">
            <input id="start2" onclick="shw_goog()" type="button" value="show google">
            <div id="translation"></div>
      <script>   function trans_L8() {
          window.location = 'skp;trans_L8@'+test.value;
          }
          function shw_goog() {
          window.location = 'skp;shw_goog';
        }</script>
      </body>
        </html>
        HTML
      
         wait_4_goog = (UI.start_timer(0.3, false) {RUBY_PLATFORM =~ /(darwin)/ ? @dlg2.show_modal() ; @dlg2.show()})
      
        @dlg2.set_html html
         @dlg2.add_action_callback("shw_goog") {|dialog, params|
           RUBY_PLATFORM =~ /(darwin)/ ? @dlg1.show_modal() ; @dlg1.show()
         }
        @dlg2.add_action_callback("trans_L8") {|dialog, params|
          set_return(params.to_s) if @ready
         wait_4_goog = (UI.start_timer(3, false) {ret_tran = get_trans if @steady})
         wait_4_goog = (UI.start_timer(3, false) {puts "#{@source.to_s} = #{@ret.to_s}" })
         wait_4_goog = (UI.start_timer(3, false) {@dlg2.execute_script("translation.textContent='#{@source.to_s} = #{@ret.to_s}'")})
        }
      end
      
      show_dialog
      
      

      I've been going round in circles with the timers to optimise the return times, but haven't cracked it yet,,, possibly very bad coding...

      john

      learn from the mistakes of others, you may not live long enough to make them all yourself...

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        I tried this and also the Bing translator, but gave up. They specifically do not want them run on local html pages. And I think the license agreement for the Google translator forbids it.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • D Offline
          driven
          last edited by

          hi Dan
          I'm running it in it's own page, but hidden on a mac

          the licence forbids direct access via code with polling, I'm not doing that...

          the User selects a word string hits enter, I upload that into the input field, wait for the reply, inject a textbox and assign the return as a value then retrieve that value...

          It's little different that copy/pasting and requires user input, so from my understanding it's ok to do...

          It's all working now for most languages, until SU lack of Encoding.default_internal screws up some input, after a write/read.
          so a char that is in two WebDialogs and appears in RubyConsole as returns from both WDlg's
          suddenly fails to send to a Third WD...

          #<Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT>

          ASCII-8BIT seems to come when storing a hash ... fails on retrieval
          john

          learn from the mistakes of others, you may not live long enough to make them all yourself...

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            Are you using marshal for storing the hash ?

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • D Offline
              driven
              last edited by

              @dan rathbun said:

              Are you using marshal for storing the hash ?

              I have in some versions, but read that storing in a .rb file, forces the encoding for the "filetype"

              However, I found the issue...

              IO.read versus File.read

              it seems IO requires ASCII compatible chars, but File doesn't...

              I was using both, and the failing dialog was the IO.read one...

              john

              learn from the mistakes of others, you may not live long enough to make them all yourself...

              1 Reply Last reply Reply Quote 0
              • A Offline
                Aerilius
                last edited by

                Bing translator has a free API (unlike Google), and allows to send an array of strings and get an array back. I have to admit, compared to Google's statistical translator, Microsoft's rule-based translator has much success on technical documents, whereas Google Translate is better in prose and informal language.
                When you feed in SketchUp's UI strings, MS Translator gives you the exact localized strings! I have an external ruby script that batch-translates .strings files. I haven't yet tried to do anything like that in SketchUp's webdialogs.

                1 Reply Last reply Reply Quote 0
                • D Offline
                  driven
                  last edited by

                  my thinking is by having the full version open, you can choose to use the 'other' interpretation..

                  which may make it sightly more accurate?

                  for example...

                  learn from the mistakes of others, you may not live long enough to make them all yourself...

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement