Skip to content

Commit cf8f1dd

Browse files
committedFeb 26, 2023
Simplify and finalise the Translation schema and service
1 parent 9fe55ed commit cf8f1dd

21 files changed

+90
-236
lines changed
 

‎app/controllers/v1/admin/translation_events_controller.rb

-48
This file was deleted.

‎app/controllers/v1/admin/translations_controller.rb

+2-9
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,14 @@ def serializer_class
4141
V1::Admin::TranslationSerializer
4242
end
4343

44-
def permitted_includes
45-
%i[
46-
translation_events
47-
]
48-
end
49-
5044
def creatable_attributes
5145
%i[
5246
location
5347
language
5448
native
5549
english
56-
needs_review
57-
review_code
58-
custom_review_message
50+
english_before
51+
status
5952
]
6053
end
6154

‎app/controllers/v1/public/translation_events_controller.rb

-48
This file was deleted.

‎app/controllers/v1/public/translations_controller.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def create
1414
end
1515

1616
def update
17-
forbidden
17+
allow_update
1818
end
1919

2020
def destroy
@@ -40,9 +40,11 @@ def permitted_filters
4040

4141
def creatable_attributes
4242
%i[
43-
location
4443
language
44+
location
4545
english
46+
english_before
47+
status
4648
]
4749
end
4850
end

‎app/jobs/translate_job.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def perform(record)
1717
puts response
1818
puts '======'
1919
first_translation = response[:translations].first
20-
record.update!(native: first_translation)
20+
record.update!(native: first_translation, status: 'to-review')
2121
puts 'translate_job end'
2222
puts '======'
2323
end

‎app/models/translation.rb

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
class Translation < ApplicationRecord
2-
has_many :translation_events, dependent: :destroy
3-
4-
validates :english, uniqueness: { scope: %i[language] }
5-
validates :review_code, inclusion: {
6-
in: [
7-
nil,
8-
'untranslated',
9-
'robot-translated',
10-
'source-changed',
11-
'custom'
2+
validates :location, uniqueness: { scope: %i[language] }
3+
validates :status, inclusion: {
4+
in: %w[
5+
to-translate
6+
to-update
7+
to-review
8+
done
129
]
1310
}
1411

15-
after_create :translate
12+
# after_create :translate
1613

17-
def translate
18-
puts '------'
19-
puts 'after_create'
20-
puts "language: #{language}"
21-
puts "phrase: #{english}"
22-
puts '------'
23-
TranslateJob.perform_later(self) if native.nil?
24-
end
14+
# def translate
15+
# puts '------'
16+
# puts 'after_create'
17+
# puts "language: #{language}"
18+
# puts "location: #{location}"
19+
# puts "english: #{english}"
20+
# puts '------'
21+
# TranslateJob.perform_later(self) if native.nil?
22+
# end
2523
end

‎app/models/translation_event.rb

-12
This file was deleted.

‎app/serializers/v1/admin/translation_event_serializer.rb

-11
This file was deleted.

‎app/serializers/v1/admin/translation_serializer.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ class TranslationSerializer < ApplicationSerializer
55
:language,
66
:native,
77
:english,
8-
:needs_review,
9-
:review_code,
10-
:custom_review_message
11-
12-
has_many :translation_events, if: requested?('translation_events')
8+
:english_before,
9+
:status
1310
end
1411
end
1512
end

‎app/serializers/v1/public/translation_event_serializer.rb

-7
This file was deleted.

‎app/services/translate_service.rb

+21-20
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,33 @@ def translation
4343
return puts 'FAIL: target_lang not supported' unless supported_languages.include?(@target_lang.to_sym)
4444
return puts 'FAIL: source_lang not supported' unless supported_languages.include?(@source_lang.to_sym)
4545

46-
{
47-
success: true,
48-
translations: ['Ich bin foo bar', 'Meine Name is foo bar', 'Ich heise foo bar']
49-
}
50-
51-
# response = Faraday.post(url) do |req|
52-
# req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
53-
# req.headers['Authorization'] = "DeepL-Auth-Key #{api_key}"
54-
# req.body = URI.encode_www_form(data)
55-
# end
46+
response = Faraday.post(url) do |req|
47+
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
48+
req.headers['Authorization'] = "DeepL-Auth-Key #{api_key}"
49+
req.body = URI.encode_www_form(data)
50+
end
5651

57-
# return puts 'FAIL: request failed' unless response.status == 200
52+
return puts 'FAIL: request failed' unless response.status == 200
5853

59-
# body = JSON.parse(response.body)
54+
body = JSON.parse(response.body)
6055

61-
# return puts 'FAIL: no translations key' unless body.key?('translations')
56+
# The response body will look something like:
57+
# {"translations"=>[{"detected_source_language"=>"EN", "text"=>"Sie sind schwul"}]}
58+
# Note how the translations key is an array which may return multiple translations.
6259

63-
# translations = body['translations']
60+
return puts 'FAIL: no translations key' unless body.key?('translations')
6461

65-
# return puts 'FAIL: no translations' if translations.empty?
62+
translations = body['translations'].map { |t| t['text'] }
6663

67-
# translation = translations.first['text']
64+
# Here we map all suggested translations to a flat array like:
65+
# ['Sie sind schwul', 'Suggestion 2']
6866

69-
# puts "translations: #{translation}"
70-
# puts '---'
67+
puts "translations: #{translations}"
7168

72-
# translation
69+
{
70+
success: true,
71+
translations: translations
72+
}
7373
end
7474

7575
def url
@@ -80,7 +80,8 @@ def data
8080
{
8181
text: @phrase,
8282
target_lang: @target_lang,
83-
source_lang: @source_lang
83+
source_lang: @source_lang,
84+
formality: 'prefer_more'
8485
}
8586
end
8687

‎config/routes.rb

-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
resources :sessions
3636
resources :simulation_requests, path: '/simulation-requests'
3737
resources :translations
38-
resources :translation_events, path: '/translation-events'
3938
resources :uses
4039
resources :webinar_attendees, path: '/webinar-attendees'
4140
resources :webinars
@@ -80,7 +79,6 @@
8079
resources :qualities
8180
resources :sessions
8281
resources :translations
83-
resources :translation_events, path: '/translation-events'
8482
resources :use_images, path: '/use-images'
8583
resources :users
8684
resources :uses
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class MakeLocationUniqueOnTranslationsPerLanguage < ActiveRecord::Migration[6.1]
2+
def change
3+
remove_column :translations, :needs_review
4+
remove_column :translations, :review_code
5+
remove_column :translations, :custom_review_message
6+
7+
add_column :translations, :status, :string
8+
add_column :translations, :history, :text
9+
add_column :translations, :last_english_approved, :text
10+
11+
remove_index :translations, name: 'unique_translation_index'
12+
13+
add_index :translations,
14+
%i[language location],
15+
unique: true,
16+
name: 'unique_location_per_language_index'
17+
end
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class RenameAndRemoveFromTranslation < ActiveRecord::Migration[6.1]
2+
def change
3+
remove_column :translations, :history
4+
rename_column :translations, :last_english_approved, :english_before
5+
end
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DropTranslationEvents < ActiveRecord::Migration[6.1]
2+
def change
3+
drop_table :translation_events
4+
end
5+
end

‎db/schema.rb

+4-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2023_02_07_063955) do
13+
ActiveRecord::Schema.define(version: 2023_02_25_230018) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "pgcrypto"
@@ -489,25 +489,16 @@
489489
t.index ["slug"], name: "index_tags_on_slug", unique: true
490490
end
491491

492-
create_table "translation_events", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
493-
t.uuid "translation_id"
494-
t.string "code"
495-
t.string "person"
496-
t.datetime "created_at", precision: 6, null: false
497-
t.datetime "updated_at", precision: 6, null: false
498-
end
499-
500492
create_table "translations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
501493
t.string "location"
502494
t.string "language"
503495
t.string "native"
504496
t.string "english"
505-
t.boolean "needs_review"
506-
t.string "review_code"
507-
t.string "custom_review_message"
508497
t.datetime "created_at", null: false
509498
t.datetime "updated_at", null: false
510-
t.index ["language", "english"], name: "unique_translation_index", unique: true
499+
t.string "status"
500+
t.text "english_before"
501+
t.index ["language", "location"], name: "unique_location_per_language_index", unique: true
511502
t.index ["language"], name: "index_translations_on_language"
512503
t.index ["location"], name: "index_translations_on_location"
513504
end

0 commit comments

Comments
 (0)
Please sign in to comment.