autotolk 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05a89151960c946d0ee91034a75dee13bb1472c4
4
- data.tar.gz: 15a5d1460cf1daf8267085ee324cfc3b80d4c82a
3
+ metadata.gz: 99ba99954c85978aed16ebf0967290cbbc67374c
4
+ data.tar.gz: f85a3b070fdbbf171d6190b2434ff6e0a4170b1f
5
5
  SHA512:
6
- metadata.gz: 891d279c77b0caa274e1bb1b83d46496c98f40fd1c56e6d24ac67bf95132b10155db39818b42212a40126abfeee0848d478dcc3d3987b0dc520b01d711147194
7
- data.tar.gz: b6920d50b0fc6e90b8cd592607d7d049bb59e141be8e8a5314af9e0acead1e8030de12050e25f68357ea153edc82f60d4347da2f3da8c8d28e568fa18781f384
6
+ metadata.gz: 7072bd5e147ca11b3d1ce68b74a06b63388f4a3c3ef6fa9fd8346523d3591c373d1f3e26287882149acacc3297bf4b6d9156bb78c68263c730f4bc38940060c4
7
+ data.tar.gz: 67b3a1afb4a1a07aed7659c7f20eb371b5d168e7c6b4364905ceab37b466c0e209adeb74539bfc9628489f4db44c49ca72e284f0dadb32877a34977811ec6868
data/README.md CHANGED
@@ -26,7 +26,7 @@ And follow the instructions on the screen (you will have to add your bing tokens
26
26
 
27
27
  ## Usage
28
28
 
29
- Now when you go to `/tolk/locales/xx` you will see an alert informing you that when you double click anywhere, the translation will be automatic. The text areas will fill out with the translated text, so you could change it if needed and save.
29
+ Now when you go to `/tolk/locales/xx` you will see a button `Start autotranslating!`. When you click on it, the translation will be automatic. The text areas will fill out with the translated text, so you could change it if needed and save.
30
30
 
31
31
  ## Issues
32
32
 
@@ -1,16 +1,19 @@
1
+ autotranslatingClicked = 0
2
+
1
3
  $(document).on 'ready page:load', ->
2
- if document.URL.includes("tolk/locales")
3
- alert "Double click anywhere to start autotranslating."
4
- autotranslatingClicked = 0
5
- $('#container').dblclick ->
6
- autotranslatingClicked += 1
7
- to_lang = document.URL.split('/').slice(-1).pop()
8
- $translation_rows = $('table.translations tbody tr:not(:first-child)')
9
- for $translation_row in $translation_rows
10
- original = $translation_row.querySelector('td.original textarea#translations__original_text').innerText
11
- $translationArea = $translation_row.querySelector('td.translation textarea')
12
- if $translationArea.innerText == '' && autotranslatingClicked == 1
13
- translate(original, to_lang, $translationArea)
4
+ $( "#container div.translations" ).prepend( "<div id='autotranslate' style='text-align: right;'><button> Start autotranslating!</button></div>" )
5
+ $('div#autotranslate').on 'click', 'button', ->
6
+ startTranslating()
7
+
8
+ startTranslating = () ->
9
+ autotranslatingClicked += 1
10
+ to_lang = document.URL.split('/').slice(-1).pop()
11
+ $translation_rows = $('table.translations tbody tr:not(:first-child)')
12
+ for $translation_row in $translation_rows
13
+ original = $translation_row.querySelector('td.original textarea#translations__original_text').innerText
14
+ $translationArea = $translation_row.querySelector('td.translation textarea')
15
+ if $translationArea.innerText == '' && autotranslatingClicked == 1
16
+ translate(original, to_lang, $translationArea)
14
17
 
15
18
  translate = (original, to, textarea) ->
16
19
  $.get '/translate', {original: original, to: to}, ((response) ->
@@ -1,12 +1,8 @@
1
- require 'bing_translator'
2
-
3
1
  class TranslationsController < ApplicationController
4
2
  skip_before_action :authenticate_user!, :validate_subdomain
5
3
 
6
4
  def translate
7
- translator = BingTranslator.new( Autotolk.bing_client_id, Autotolk.bing_client_secret,
8
- false, Autotolk.azure_account_key)
9
- translated = translator.translate params[:original], :to => params[:to]
5
+ translated = AutoTranslator.new(original: params[:original], to: params[:to]).perform
10
6
  render json: {status: 200, translated: translated}
11
7
  rescue Exception => e
12
8
  render json: {status: 500, error: e}
@@ -0,0 +1,35 @@
1
+ require 'bing_translator'
2
+
3
+ class AutoTranslator
4
+
5
+ def initialize parameters = { original: nil, to: nil }
6
+ @translator = nil
7
+ @translation = nil
8
+ @original = parameters[:original]
9
+ @to = parameters[:to]
10
+ end
11
+
12
+ def perform
13
+ set_translator
14
+ raw_translate
15
+ fix_special_characters
16
+ @translation
17
+ end
18
+
19
+ private
20
+
21
+ def set_translator
22
+ @translator = BingTranslator.new( Autotolk.bing_client_id, Autotolk.bing_client_secret,
23
+ false, Autotolk.azure_account_key)
24
+ end
25
+
26
+ def raw_translate
27
+ @translation = @translator.translate @original, to: @to
28
+ end
29
+
30
+ def fix_special_characters
31
+ @translation.gsub!('% {', '%{')
32
+ @translation.gsub!(/{(?<word>\w+)} %[^{]*/, '%{\k<word>}')
33
+ @translation.gsub!('---', '')
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Autotolk
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autotolk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joanna Rajewska
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,8 +70,8 @@ files:
70
70
  - app/assets/.DS_Store
71
71
  - app/assets/javascripts/.DS_Store
72
72
  - app/assets/javascripts/autotolk/locales.js.coffee
73
- - app/assets/javascripts/locales.js.coffee
74
73
  - app/controllers/translations_controller.rb
74
+ - app/services/auto_translator.rb
75
75
  - autotolk.gemspec
76
76
  - bin/console
77
77
  - bin/setup
@@ -1,19 +0,0 @@
1
- $(document).on 'ready page:load', ->
2
- $( "#container div.translations" ).prepend( "<div style='text-align: right;'><button onclick='startTranslating()'> Start autotranslating!</button></div>" )
3
- autotranslatingClicked = 0
4
-
5
- startTranslating = ->
6
- autotranslatingClicked += 1
7
- to_lang = document.URL.split('/').slice(-1).pop()
8
- $translation_rows = $('table.translations tbody tr:not(:first-child)')
9
- for $translation_row in $translation_rows
10
- original = $translation_row.querySelector('td.original textarea#translations__original_text').innerText
11
- $translationArea = $translation_row.querySelector('td.translation textarea')
12
- if $translationArea.innerText == '' && autotranslatingClicked == 1
13
- translate(original, to_lang, $translationArea)
14
-
15
- translate = (original, to, textarea) ->
16
- $.get '/translate', {original: original, to: to}, ((response) ->
17
- if response.status == 200
18
- textarea.innerText = response.translated
19
- ), 'json'