-
Notifications
You must be signed in to change notification settings - Fork 52
Add Yandex Translator #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3ef4334
Init Yandex Translator
barreeeiroo 5192a4f
Make it work
barreeeiroo 1afd8ee
DEV: Use the modern hash syntax
CvX 204caf1
DEV: Remove unnecessary string interpolations
CvX da4173b
DEV: Use a two-space indentation
CvX fd2b447
Update yandex_spec.rb
barreeeiroo 0a2b892
Update yandex_spec.rb
barreeeiroo 46bedf8
Update yandex.rb
barreeeiroo 9c9e11e
DEV: Remove unnecessary parens
CvX 5571241
FIX: Use the `html` format since we're sending cooked posts
CvX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
# 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', | ||
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 | ||
"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) | ||
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= begin | ||
query = default_query.merge( | ||
"text" => post.raw | ||
) | ||
|
||
uri = URI(DETECT_URI) | ||
uri.query = URI.encode_www_form(query) | ||
|
||
response_body = result(uri.to_s, "", default_headers) | ||
|
||
response_body["lang"] | ||
end | ||
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 | ||
|
||
translated_text = from_custom_fields(post) do | ||
query = default_query.merge( | ||
"lang" => "#{detected_lang}-#{locale}", | ||
"text" => post.cooked, | ||
"format" => "html" | ||
) | ||
|
||
uri = URI(TRANSLATE_URI) | ||
uri.query = URI.encode_www_form(query) | ||
|
||
response_body = result(uri.to_s, "", default_headers) | ||
response_body["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) | ||
CvX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: access_token | ||
} | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe DiscourseTranslator::Yandex do | ||
let(:mock_response) { Struct.new(:status, :body) } | ||
|
||
describe '.access_token' do | ||
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 | ||
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(:post).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' 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(:post).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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.