mirror of
https://ghfast.top/https://github.com/discourse/discourse-translator.git
synced 2026-07-16 11:46:33 +08:00
Currently, `Topic` and `Post` have detected_language and translations in custom fields, e.g.
```
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]
post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
topic.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]
topic.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
```
We are moving this into 4 tables/models
```
post has_one :content_locale, class_name: "DiscourseTranslator::PostLocale"
post has_many :translations, class_name: "DiscourseTranslator::PostTranslation"
topic has_one :content_locale, class_name: "DiscourseTranslator::TopicLocale"
topic has_many :translations, class_name: "DiscourseTranslator::TopicTranslation"
```
Since there are a lot of duplicates, this is implemented on the `Post` and `Topic` using a `Concern`, and any future translatable content can inherit this concern.
This PR also gets rid of the previous N+1 which happens when determining if the 🌐 translate button should appear for each post.
55 lines
1.5 KiB
Ruby
55 lines
1.5 KiB
Ruby
# 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)
|
|
|
|
expect(post.detected_locale).to eq(detected_lang)
|
|
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).at_least_once.returns("de")
|
|
|
|
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
|