From 3ef433473a7314b580a63fc8c8794e219899ddc4 Mon Sep 17 00:00:00 2001 From: Diego Barreiro Date: Thu, 2 Jan 2020 04:02:05 +0100 Subject: [PATCH 01/10] Init Yandex Translator --- config/locales/server.en.yml | 1 + config/locales/server.es.yml | 1 + config/settings.yml | 3 + plugin.rb | 1 + services/discourse_translator/yandex.rb | 203 ++++++++++++++++++++++++ spec/services/yandex_spec.rb | 61 +++++++ 6 files changed, 270 insertions(+) create mode 100644 services/discourse_translator/yandex.rb create mode 100644 spec/services/yandex_spec.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ed91043..05b52a9 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -4,6 +4,7 @@ en: translator: "The provider of the translation service." translator_azure_subscription_key: "Azure Subscription Key" translator_google_api_key: "Google API Key" + translator_yandex_api_key: "Yandex API Key" translator: failed: "The translator is unable to translate this language." not_supported: "This language is not supported by the translator." diff --git a/config/locales/server.es.yml b/config/locales/server.es.yml index cfd00a9..622dff0 100644 --- a/config/locales/server.es.yml +++ b/config/locales/server.es.yml @@ -4,6 +4,7 @@ es: translator: "El proveedor del servicio de traducción." translator_azure_subscription_key: "Azure Subscription Key" translator_google_api_key: "Google Api Key" + translator_yandex_api_key: "Yandex API Key" translator: failed: "El traductor no fue capaz de traducir este idioma." not_supported: "Este idioma no está disponible en el traductor." diff --git a/config/settings.yml b/config/settings.yml index 2f42aad..d9a3c04 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -9,7 +9,10 @@ plugins: choices: - Microsoft - Google + - Yandex translator_azure_subscription_key: default: '' translator_google_api_key: default: '' + translator_yandex_api_key: + default: '' diff --git a/plugin.rb b/plugin.rb index 9636423..edb3bc5 100644 --- a/plugin.rb +++ b/plugin.rb @@ -17,6 +17,7 @@ module ::DiscourseTranslator autoload :Microsoft, "#{Rails.root}/plugins/discourse-translator/services/discourse_translator/microsoft" autoload :Google, "#{Rails.root}/plugins/discourse-translator/services/discourse_translator/google" + autoload :Yandex, "#{Rails.root}/plugins/discourse-translator/services/discourse_translator/yandex" class Engine < ::Rails::Engine engine_name PLUGIN_NAME diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb new file mode 100644 index 0000000..12a9a9b --- /dev/null +++ b/services/discourse_translator/yandex.rb @@ -0,0 +1,203 @@ +# frozen_string_literal: true + +require_relative 'base' + +module DiscourseTranslator + class Yandex < Base + TRANSLATE_URI = "https://translate.yandex.net/api/v1.5/tr.json/translate" + DETECT_URI = "https://translate.yandex.net/api/v1.5/tr.json/detect" + + SUPPORTED_LANG = { + pt_BR: 'pt', + pl_PL: 'pl', + no_NO: 'no', + fa_IR: 'fa', + zh_CN: 'zh', + zh_TW: 'zh', + tr_TR: 'tr', + az: 'az', + ml: 'ml', + sq: 'sq', + mt: 'mt', + am: 'am', + mk: 'mk', + en: 'en', + mi: 'mi', + ar: 'ar', + mr: 'mr', + hy: 'hy', + mhr: 'mhr', + af: 'af', + mn: 'mn', + eu: 'eu', + de: 'de', + ba: 'ba', + ne: 'ne', + be: 'be', + no: 'no', + bn: 'bn', + pa: 'pa', + my: 'my', + pap: 'pap', + bg: 'bg', + fa: 'fa', + bs: 'bs', + pl: 'pl', + cy: 'cy', + pt: 'pt', + hu: 'hu', + ro: 'ro', + vi: 'vi', + ru: 'ru', + ht: 'ht', + ceb: 'ceb', + gl: 'gl', + sr: 'sr', + nl: 'nl', + si: 'si', + mrj: 'mrj', + sk: 'sk', + el: 'el', + sl: 'sl', + ka: 'ka', + sw: 'sw', + gu: 'gu', + su: 'su', + da: 'da', + tg: 'tg', + he: 'he', + th: 'th', + yi: 'yi', + tl: 'tl', + id: 'id', + ta: 'ta', + ga: 'ga', + tt: 'tt', + it: 'it', + te: 'te', + is: 'is', + tr: 'tr', + es: 'es', + udm: 'udm', + kk: 'kk', + uz: 'uz', + kn: 'kn', + uk: 'uk', + ca: 'ca', + ur: 'ur', + ky: 'ky', + fi: 'fi', + zh: 'zh', + fr: 'fr', + ko: 'ko', + hi: 'hi', + xh: 'xh', + hr: 'hr', + km: 'km', + cs: 'cs', + lo: 'lo', + sv: 'sv', + la: 'la', + gd: 'gd', + lv: 'lv', + et: 'et', + lt: 'lt', + eo: 'eo', + lb: 'lb', + jv: 'jv', + mg: 'mg', + ja: 'ja', + ms: 'ms', + } + + def self.access_token_key + print "Hello21" + "yandex-translator" + end + + def self.access_token + SiteSetting.translator_yandex_api_key || (raise TranslatorError.new("NotFound: Yandex API Key not set.")) + end + + def self.detect(post) + print "Hello22" + post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= begin + body = [ + {:Text => post.raw} + ] + + uri = URI(DETECT_URI) + uri.query = URI.encode_www_form(default_query) + + response_body = result(uri.to_s, body, default_headers) + + response_body.first["lang"] + end + end + + def self.translate_supported?(source, target) + SUPPORTED_LANG.keys.include?(source) && SUPPORTED_LANG.values.include?(target) + end + + def self.translate(post) + detected_lang = detect(post) + + if !SUPPORTED_LANG.keys.include?(detected_lang.to_sym) && + !SUPPORTED_LANG.values.include?(detected_lang.to_s) + + raise TranslatorError.new(I18n.t('translator.failed')) + end + + body = [ + {:text => post.cooked} + ] + + translated_text = from_custom_fields(post) do + query = default_query.merge( + "lang" => "#{detected_lang}-#{locale}" + ) + + uri = URI(TRANSLATE_URI) + uri.query = URI.encode_www_form(query) + + response_body = result(uri.to_s, body, default_headers) + response_body.first["text"][0] + end + + [detected_lang, translated_text] + end + + private + + def self.locale + SUPPORTED_LANG[I18n.locale] || (raise I18n.t("translator.not_supported")) + end + + def self.post(uri, body, headers = {}) + Excon.post(uri, body: body, headers: headers) + end + + def self.result(uri, body, headers) + response = post(uri, body, headers) + response_body = JSON.parse(response.body) + + if response.status != 200 + raise TranslatorError.new(response_body) + else + response_body + end + end + + def self.default_headers + { + 'Content-Type' => 'application/x-www-form-urlencoded' + } + end + + def self.default_query + { + :key => "#{SiteSetting.translator_azure_subscription_key}" + } + end + end +end diff --git a/spec/services/yandex_spec.rb b/spec/services/yandex_spec.rb new file mode 100644 index 0000000..de5e98c --- /dev/null +++ b/spec/services/yandex_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe DiscourseTranslator::Yandex do + let(:mock_response) { Struct.new(:status, :body) } + + describe '.access_token' do + print "Hello11" + describe 'when set' do + api_key = '12345' + before { SiteSetting.translator_yandex_api_key = api_key } + it 'should return back translator_yandex_api_key' do + expect(described_class.access_token).to eq(api_key) + end + end + end + + describe '.detect' do + print "Hello12" + let(:post) { Fabricate(:post) } + + it 'should store the detected language in a custom field' do + detected_lang = 'en' + described_class.expects(:access_token).returns('12345') + Excon.expects(:get).returns(mock_response.new(200, %{ { "code": 200, "lang": "#{detected_lang}" } })).once + expect(described_class.detect(post)).to eq(detected_lang) + + 2.times do + expect( + post.custom_fields[::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] + ).to eq(detected_lang) + end + end + end + + describe '.translate_supported?' do + it 'should equate source language to target' do + source = 'en' + target = 'fr' + Excon.expects(:get).returns(mock_response.new(200, %{ { "data": { "languages": [ { "language": "#{source}" }] } } })) + expect(described_class.translate_supported?(source, target)).to be true + end + end + + describe '.translate' do + let(:post) { Fabricate(:post) } + + it 'raises an error on failure' do + described_class.expects(:access_token).returns('12345') + described_class.expects(:detect).returns('en') + + Excon.expects(:get).returns(mock_response.new( + 400, + { error: 'something went wrong', error_description: 'you passed in a wrong param' }.to_json + )) + + expect { described_class.translate(post) }.to raise_error DiscourseTranslator::TranslatorError + end + end +end From 5192a4f42a8e408e03369ee731ebce129d1d51e9 Mon Sep 17 00:00:00 2001 From: Diego Barreiro Date: Thu, 2 Jan 2020 17:02:07 +0100 Subject: [PATCH 02/10] Make it work --- services/discourse_translator/yandex.rb | 29 +++++++++++-------------- spec/services/yandex_spec.rb | 2 -- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index 12a9a9b..66c9c47 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -15,6 +15,8 @@ class Yandex < Base zh_CN: 'zh', zh_TW: 'zh', tr_TR: 'tr', + en_US: 'en', + en_GB: 'en', az: 'az', ml: 'ml', sq: 'sq', @@ -111,7 +113,6 @@ class Yandex < Base } def self.access_token_key - print "Hello21" "yandex-translator" end @@ -120,18 +121,17 @@ def self.access_token end def self.detect(post) - print "Hello22" post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= begin - body = [ - {:Text => post.raw} - ] + query = default_query.merge( + "text" => post.raw + ) uri = URI(DETECT_URI) - uri.query = URI.encode_www_form(default_query) + uri.query = URI.encode_www_form(query) - response_body = result(uri.to_s, body, default_headers) + response_body = result(uri.to_s, "", default_headers) - response_body.first["lang"] + response_body["lang"] end end @@ -148,20 +148,17 @@ def self.translate(post) raise TranslatorError.new(I18n.t('translator.failed')) end - body = [ - {:text => post.cooked} - ] - translated_text = from_custom_fields(post) do query = default_query.merge( - "lang" => "#{detected_lang}-#{locale}" + "lang" => "#{detected_lang}-#{locale}", + "text" => "#{post.cooked}" ) uri = URI(TRANSLATE_URI) uri.query = URI.encode_www_form(query) - response_body = result(uri.to_s, body, default_headers) - response_body.first["text"][0] + response_body = result(uri.to_s, "", default_headers) + response_body["text"][0] end [detected_lang, translated_text] @@ -196,7 +193,7 @@ def self.default_headers def self.default_query { - :key => "#{SiteSetting.translator_azure_subscription_key}" + :key => "#{SiteSetting.translator_yandex_api_key}" } end end diff --git a/spec/services/yandex_spec.rb b/spec/services/yandex_spec.rb index de5e98c..18acdb3 100644 --- a/spec/services/yandex_spec.rb +++ b/spec/services/yandex_spec.rb @@ -6,7 +6,6 @@ let(:mock_response) { Struct.new(:status, :body) } describe '.access_token' do - print "Hello11" describe 'when set' do api_key = '12345' before { SiteSetting.translator_yandex_api_key = api_key } @@ -17,7 +16,6 @@ end describe '.detect' do - print "Hello12" let(:post) { Fabricate(:post) } it 'should store the detected language in a custom field' do From 1afd8eeb389d0f30b56230066f9ea48e754794b3 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 7 Jan 2020 13:47:51 +0100 Subject: [PATCH 03/10] DEV: Use the modern hash syntax --- services/discourse_translator/yandex.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index 66c9c47..ad0e96c 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -193,7 +193,7 @@ def self.default_headers def self.default_query { - :key => "#{SiteSetting.translator_yandex_api_key}" + key: "#{SiteSetting.translator_yandex_api_key}" } end end From 204caf13f769bc42a6bb0bf6892a9fa3a49a920e Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 7 Jan 2020 13:48:45 +0100 Subject: [PATCH 04/10] DEV: Remove unnecessary string interpolations --- services/discourse_translator/yandex.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index ad0e96c..218330c 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -151,7 +151,7 @@ def self.translate(post) translated_text = from_custom_fields(post) do query = default_query.merge( "lang" => "#{detected_lang}-#{locale}", - "text" => "#{post.cooked}" + "text" => post.cooked ) uri = URI(TRANSLATE_URI) @@ -193,7 +193,7 @@ def self.default_headers def self.default_query { - key: "#{SiteSetting.translator_yandex_api_key}" + key: SiteSetting.translator_yandex_api_key } end end From da4173b83e4adcec52ab253c67aca0038519af6d Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 7 Jan 2020 13:56:36 +0100 Subject: [PATCH 05/10] DEV: Use a two-space indentation --- services/discourse_translator/yandex.rb | 230 ++++++++++++------------ spec/services/yandex_spec.rb | 1 + 2 files changed, 116 insertions(+), 115 deletions(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index 218330c..9c10b70 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -8,108 +8,108 @@ class Yandex < Base DETECT_URI = "https://translate.yandex.net/api/v1.5/tr.json/detect" SUPPORTED_LANG = { - pt_BR: 'pt', - pl_PL: 'pl', - no_NO: 'no', - fa_IR: 'fa', - zh_CN: 'zh', - zh_TW: 'zh', - tr_TR: 'tr', - en_US: 'en', - en_GB: 'en', - az: 'az', - ml: 'ml', - sq: 'sq', - mt: 'mt', - am: 'am', - mk: 'mk', - en: 'en', - mi: 'mi', - ar: 'ar', - mr: 'mr', - hy: 'hy', - mhr: 'mhr', - af: 'af', - mn: 'mn', - eu: 'eu', - de: 'de', - ba: 'ba', - ne: 'ne', - be: 'be', - no: 'no', - bn: 'bn', - pa: 'pa', - my: 'my', - pap: 'pap', - bg: 'bg', - fa: 'fa', - bs: 'bs', - pl: 'pl', - cy: 'cy', - pt: 'pt', - hu: 'hu', - ro: 'ro', - vi: 'vi', - ru: 'ru', - ht: 'ht', - ceb: 'ceb', - gl: 'gl', - sr: 'sr', - nl: 'nl', - si: 'si', - mrj: 'mrj', - sk: 'sk', - el: 'el', - sl: 'sl', - ka: 'ka', - sw: 'sw', - gu: 'gu', - su: 'su', - da: 'da', - tg: 'tg', - he: 'he', - th: 'th', - yi: 'yi', - tl: 'tl', - id: 'id', - ta: 'ta', - ga: 'ga', - tt: 'tt', - it: 'it', - te: 'te', - is: 'is', - tr: 'tr', - es: 'es', - udm: 'udm', - kk: 'kk', - uz: 'uz', - kn: 'kn', - uk: 'uk', - ca: 'ca', - ur: 'ur', - ky: 'ky', - fi: 'fi', - zh: 'zh', - fr: 'fr', - ko: 'ko', - hi: 'hi', - xh: 'xh', - hr: 'hr', - km: 'km', - cs: 'cs', - lo: 'lo', - sv: 'sv', - la: 'la', - gd: 'gd', - lv: 'lv', - et: 'et', - lt: 'lt', - eo: 'eo', - lb: 'lb', - jv: 'jv', - mg: 'mg', - ja: 'ja', - ms: 'ms', + pt_BR: 'pt', + pl_PL: 'pl', + no_NO: 'no', + fa_IR: 'fa', + zh_CN: 'zh', + zh_TW: 'zh', + tr_TR: 'tr', + en_US: 'en', + en_GB: 'en', + az: 'az', + ml: 'ml', + sq: 'sq', + mt: 'mt', + am: 'am', + mk: 'mk', + en: 'en', + mi: 'mi', + ar: 'ar', + mr: 'mr', + hy: 'hy', + mhr: 'mhr', + af: 'af', + mn: 'mn', + eu: 'eu', + de: 'de', + ba: 'ba', + ne: 'ne', + be: 'be', + no: 'no', + bn: 'bn', + pa: 'pa', + my: 'my', + pap: 'pap', + bg: 'bg', + fa: 'fa', + bs: 'bs', + pl: 'pl', + cy: 'cy', + pt: 'pt', + hu: 'hu', + ro: 'ro', + vi: 'vi', + ru: 'ru', + ht: 'ht', + ceb: 'ceb', + gl: 'gl', + sr: 'sr', + nl: 'nl', + si: 'si', + mrj: 'mrj', + sk: 'sk', + el: 'el', + sl: 'sl', + ka: 'ka', + sw: 'sw', + gu: 'gu', + su: 'su', + da: 'da', + tg: 'tg', + he: 'he', + th: 'th', + yi: 'yi', + tl: 'tl', + id: 'id', + ta: 'ta', + ga: 'ga', + tt: 'tt', + it: 'it', + te: 'te', + is: 'is', + tr: 'tr', + es: 'es', + udm: 'udm', + kk: 'kk', + uz: 'uz', + kn: 'kn', + uk: 'uk', + ca: 'ca', + ur: 'ur', + ky: 'ky', + fi: 'fi', + zh: 'zh', + fr: 'fr', + ko: 'ko', + hi: 'hi', + xh: 'xh', + hr: 'hr', + km: 'km', + cs: 'cs', + lo: 'lo', + sv: 'sv', + la: 'la', + gd: 'gd', + lv: 'lv', + et: 'et', + lt: 'lt', + eo: 'eo', + lb: 'lb', + jv: 'jv', + mg: 'mg', + ja: 'ja', + ms: 'ms', } def self.access_token_key @@ -122,17 +122,17 @@ def self.access_token def self.detect(post) post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= begin - query = default_query.merge( - "text" => post.raw - ) + query = default_query.merge( + "text" => post.raw + ) - uri = URI(DETECT_URI) - uri.query = URI.encode_www_form(query) + uri = URI(DETECT_URI) + uri.query = URI.encode_www_form(query) - response_body = result(uri.to_s, "", default_headers) + response_body = result(uri.to_s, "", default_headers) - response_body["lang"] - end + response_body["lang"] + end end def self.translate_supported?(source, target) @@ -143,15 +143,15 @@ def self.translate(post) detected_lang = detect(post) if !SUPPORTED_LANG.keys.include?(detected_lang.to_sym) && - !SUPPORTED_LANG.values.include?(detected_lang.to_s) + !SUPPORTED_LANG.values.include?(detected_lang.to_s) raise TranslatorError.new(I18n.t('translator.failed')) end translated_text = from_custom_fields(post) do query = default_query.merge( - "lang" => "#{detected_lang}-#{locale}", - "text" => post.cooked + "lang" => "#{detected_lang}-#{locale}", + "text" => post.cooked ) uri = URI(TRANSLATE_URI) @@ -187,13 +187,13 @@ def self.result(uri, body, headers) def self.default_headers { - 'Content-Type' => 'application/x-www-form-urlencoded' + 'Content-Type' => 'application/x-www-form-urlencoded' } end def self.default_query { - key: SiteSetting.translator_yandex_api_key + key: SiteSetting.translator_yandex_api_key } end end diff --git a/spec/services/yandex_spec.rb b/spec/services/yandex_spec.rb index 18acdb3..0d28c77 100644 --- a/spec/services/yandex_spec.rb +++ b/spec/services/yandex_spec.rb @@ -9,6 +9,7 @@ describe 'when set' do api_key = '12345' before { SiteSetting.translator_yandex_api_key = api_key } + it 'should return back translator_yandex_api_key' do expect(described_class.access_token).to eq(api_key) end From fd2b44772d024f1b571992e606a9c7dea9bfb081 Mon Sep 17 00:00:00 2001 From: Diego Barreiro Date: Tue, 7 Jan 2020 14:13:46 +0100 Subject: [PATCH 06/10] Update yandex_spec.rb --- spec/services/yandex_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/services/yandex_spec.rb b/spec/services/yandex_spec.rb index 0d28c77..e5feb99 100644 --- a/spec/services/yandex_spec.rb +++ b/spec/services/yandex_spec.rb @@ -22,7 +22,7 @@ it 'should store the detected language in a custom field' do detected_lang = 'en' described_class.expects(:access_token).returns('12345') - Excon.expects(:get).returns(mock_response.new(200, %{ { "code": 200, "lang": "#{detected_lang}" } })).once + Excon.expects(:post).returns(mock_response.new(200, %{ { "code": 200, "lang": "#{detected_lang}" } })).once expect(described_class.detect(post)).to eq(detected_lang) 2.times do @@ -37,7 +37,7 @@ it 'should equate source language to target' do source = 'en' target = 'fr' - Excon.expects(:get).returns(mock_response.new(200, %{ { "data": { "languages": [ { "language": "#{source}" }] } } })) + Excon.expects(:post).returns(mock_response.new(200, %{ { "data": { "languages": [ { "language": "#{source}" }] } } })) expect(described_class.translate_supported?(source, target)).to be true end end From 0a2b8929e88fe75d279a5bfed844873ef60e737e Mon Sep 17 00:00:00 2001 From: Diego Barreiro Date: Tue, 7 Jan 2020 16:10:33 +0100 Subject: [PATCH 07/10] Update yandex_spec.rb --- spec/services/yandex_spec.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/spec/services/yandex_spec.rb b/spec/services/yandex_spec.rb index e5feb99..38cea54 100644 --- a/spec/services/yandex_spec.rb +++ b/spec/services/yandex_spec.rb @@ -33,15 +33,6 @@ end end - describe '.translate_supported?' do - it 'should equate source language to target' do - source = 'en' - target = 'fr' - Excon.expects(:post).returns(mock_response.new(200, %{ { "data": { "languages": [ { "language": "#{source}" }] } } })) - expect(described_class.translate_supported?(source, target)).to be true - end - end - describe '.translate' do let(:post) { Fabricate(:post) } @@ -49,7 +40,7 @@ described_class.expects(:access_token).returns('12345') described_class.expects(:detect).returns('en') - Excon.expects(:get).returns(mock_response.new( + Excon.expects(:post).returns(mock_response.new( 400, { error: 'something went wrong', error_description: 'you passed in a wrong param' }.to_json )) From 46bedf894b30e9b9081022756dc7c1ee003377db Mon Sep 17 00:00:00 2001 From: Diego Barreiro Date: Tue, 7 Jan 2020 16:11:23 +0100 Subject: [PATCH 08/10] Update yandex.rb --- services/discourse_translator/yandex.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index 9c10b70..3b640d3 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -135,10 +135,6 @@ def self.detect(post) end end - def self.translate_supported?(source, target) - SUPPORTED_LANG.keys.include?(source) && SUPPORTED_LANG.values.include?(target) - end - def self.translate(post) detected_lang = detect(post) @@ -193,7 +189,7 @@ def self.default_headers def self.default_query { - key: SiteSetting.translator_yandex_api_key + key: access_token() } end end From 9c9e11edb0bda788f4b57a559bad56bde04e0ea1 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 7 Jan 2020 16:21:21 +0100 Subject: [PATCH 09/10] DEV: Remove unnecessary parens --- services/discourse_translator/yandex.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index 3b640d3..48b6a84 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -189,7 +189,7 @@ def self.default_headers def self.default_query { - key: access_token() + key: access_token } end end From 5571241bc9eeb12a06248e1ac51fe50a045ce27b Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 7 Jan 2020 16:59:08 +0100 Subject: [PATCH 10/10] FIX: Use the `html` format since we're sending cooked posts --- services/discourse_translator/yandex.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/discourse_translator/yandex.rb b/services/discourse_translator/yandex.rb index 48b6a84..966b1fb 100644 --- a/services/discourse_translator/yandex.rb +++ b/services/discourse_translator/yandex.rb @@ -147,7 +147,8 @@ def self.translate(post) translated_text = from_custom_fields(post) do query = default_query.merge( "lang" => "#{detected_lang}-#{locale}", - "text" => post.cooked + "text" => post.cooked, + "format" => "html" ) uri = URI(TRANSLATE_URI)