Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ en:
experimental_anon_language_switcher: "Enable experimental language switcher for anonymous users. This will allow anonymous users to switch between translated versions of Discourse and user-contributed content in topics."
errors:
set_locale_cookie_requirements: "The experimental language switcher for anonymous users requires the `set locale from cookie` site setting to be enabled."
needs_nonzero_backfill: "Automatic language translation requires the 'automatic_translation_backfill_maximum_translations_per_hour' hidden setting to be a non-zero value. Please approach your site administrator to increase this limit."
experimental_inline_translation: "Enable experimental inline translation feature. This replaces existing parallel translation, allowing site visitors with a non-default locale to view content in their language."
automatic_translation_target_languages: "The languages to automatically translate user content (posts, topics) to. If empty, no languages will be automatically translated."
translator:
Expand Down
4 changes: 3 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ discourse_translator:
validator: "DiscourseTranslator::TranslatorSelectionValidator"
automatic_translation_target_languages:
default: ""
client: true
type: list
list_type: named
choices: "DiscourseTranslator::TranslatableLanguagesSetting.values"
allow_any: false
choices: "DiscourseTranslator::TranslatableLanguagesSetting.values"
validator: "DiscourseTranslator::TranslatableLanguagesValidator"
automatic_translation_backfill_maximum_translations_per_hour:
default: 0
client: false
Expand Down
19 changes: 19 additions & 0 deletions lib/discourse_translator/translatable_languages_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module DiscourseTranslator
class TranslatableLanguagesValidator
def initialize(opts = {})
@opts = opts
end

def valid_value?(val)
return true if val.blank?

SiteSetting.automatic_translation_backfill_maximum_translations_per_hour > 0
end

def error_message
I18n.t("site_settings.errors.needs_nonzero_backfill")
end
end
end
7 changes: 4 additions & 3 deletions spec/jobs/automatic_translation_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,23 @@ def expect_google_translate(text)
end

it "does not backfill if backfill limit is set to 0" do
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
SiteSetting.automatic_translation_target_languages = "de"
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 0
expect_any_instance_of(Jobs::AutomaticTranslationBackfill).not_to receive(:process_batch)
end

it "does not backfill if backfill lock is not secure" do
SiteSetting.automatic_translation_target_languages = "de"
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
SiteSetting.automatic_translation_target_languages = "de"
Discourse.redis.set("discourse_translator_backfill_lock", "1")
expect_any_instance_of(Jobs::AutomaticTranslationBackfill).not_to receive(:translate_records)
end

describe "with two locales ['de', 'es']" do
before do
SiteSetting.automatic_translation_target_languages = "de|es"
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
SiteSetting.automatic_translation_target_languages = "de|es"
expect_google_check_language
end

Expand Down Expand Up @@ -107,8 +108,8 @@ def expect_google_translate(text)

describe "with just one locale ['de']" do
before do
SiteSetting.automatic_translation_target_languages = "de"
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 5 * 12
SiteSetting.automatic_translation_target_languages = "de"
expect_google_check_language
end

Expand Down
1 change: 1 addition & 0 deletions spec/jobs/translate_translatable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
before do
SiteSetting.translator_enabled = true
SiteSetting.translator_provider = "Google"
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
SiteSetting.automatic_translation_target_languages = "es|fr"
allow(DiscourseTranslator::Google).to receive(:translate)
end
Expand Down
4 changes: 3 additions & 1 deletion spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
fab!(:user)

it "enqueues translate_translatable job when post cooked" do
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 100
SiteSetting.automatic_translation_target_languages = "es"
post = Fabricate(:post, user: user)
CookedPostProcessor.new(post).post_process
Expand All @@ -152,7 +153,8 @@
)
end

it "does not enqueues translate_translatable job for bot posts" do
it "does not enqueue translate_translatable job for bot posts" do
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
SiteSetting.automatic_translation_target_languages = "es"
post = Fabricate(:post, user: Discourse.system_user)
CookedPostProcessor.new(post).post_process
Expand Down
27 changes: 27 additions & 0 deletions spec/system/site_settings_validation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

RSpec.describe "Translation settings", type: :system do
fab!(:admin)

before do
sign_in(admin)
SiteSetting.translator_enabled = true
end

it "warns when automatic_translation_target_languages is being set but backfill limit is 0" do
visit(
"/admin/plugins/discourse-translator/settings?filter=automatic%20translation%20target%20languages",
)

setting =
PageObjects::Components::SelectKit.new(
"[data-setting='automatic_translation_target_languages'] .select-kit",
)
setting.expand
setting.select_row_by_value("ja")

page.find(".setting-controls button.ok").click()

expect(page).to have_content(I18n.t("site_settings.errors.needs_nonzero_backfill"))
end
end
Loading