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
7 changes: 4 additions & 3 deletions app/validators/language_switcher_setting_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ def initialize(opts = {})
end

def valid_value?(val)
return true if val == "f"
SiteSetting.set_locale_from_cookie
return true if val == "f" || val == "false"
SiteSetting.set_locale_from_cookie &&
SiteSetting.automatic_translation_target_languages.present?
end

def error_message
I18n.t("site_settings.errors.set_locale_cookie_requirements")
I18n.t("site_settings.errors.experimental_anon_language_switcher_requirements")
end
end
11 changes: 7 additions & 4 deletions assets/javascripts/discourse/components/language-switcher.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ export default class LanguageSwitcher extends Component {
@service router;

get localeOptions() {
return JSON.parse(this.siteSettings.available_locales).map(
({ name, value }) => {
const targetLanguages = (
this.siteSettings.automatic_translation_target_languages || ""
).split("|");
return JSON.parse(this.siteSettings.available_locales)
.filter(({ value }) => targetLanguages.includes(value))
.map(({ name, value }) => {
return {
label: name,
value,
};
}
);
});
}

@action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ function initializeTranslation(api) {

const currentUser = api.getCurrentUser();

if (!currentUser && siteSettings.experimental_anon_language_switcher) {
if (
!currentUser &&
siteSettings.experimental_anon_language_switcher &&
siteSettings.automatic_translation_target_languages
) {
api.headerIcons.add(
"discourse-translator_language-switcher",
LanguageSwitcher,
Expand Down
6 changes: 3 additions & 3 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
site_settings:
translator_enabled: "Allow inline translation of posts."
translator_enabled: "Enable the translator plugin."
translator_provider: "The provider of the translation service."
translator_azure_subscription_key: "Azure Subscription Key"
translator_azure_region: "Azure Region"
Expand All @@ -17,10 +17,10 @@ en:
translator_azure_custom_subdomain: "Required if using a Virtual Network or Firewall for Azure Cognitive Services. Note: Only enter the custom subdomain not the full custom endpoint."
restrict_translation_by_group: "Only allowed groups can translate"
restrict_translation_by_poster_group: "Only allow translation of posts made by users in allowed groups. If empty, allow translations of posts from all users."
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."
experimental_anon_language_switcher: "Enable experimental language switcher. This will allow site visitors who are not logged in 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_anon_language_switcher_requirements: "The experimental language switcher requires the `set locale from cookie` site setting to be enabled, and the `automatic translation target languages` to have at least one language."
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
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ discourse_translator:
validator: "DiscourseTranslator::TranslatorSelectionValidator"
automatic_translation_target_languages:
default: ""
client: true
type: list
client: true
list_type: named
allow_any: false
choices: "DiscourseTranslator::TranslatableLanguagesSetting.values"
Expand Down
11 changes: 11 additions & 0 deletions spec/system/anon_language_switcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

RSpec.describe "Anonymous user language switcher", type: :system do
fab!(:japanese_user) { Fabricate(:user, locale: "ja") }

it "shows the correct language based on the selected language and login status" do
SWITCHER_SELECTOR = "button[data-identifier='discourse-translator_language-switcher']"

Expand All @@ -11,13 +12,23 @@
SiteSetting.translator_enabled = true
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_cookie = true
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
SiteSetting.automatic_translation_target_languages = "es|ja"
SiteSetting.experimental_anon_language_switcher = true
visit("/")
expect(page).to have_css(SWITCHER_SELECTOR)
expect(find(".nav-item_latest")).to have_content("Latest")

switcher = PageObjects::Components::DMenu.new(SWITCHER_SELECTOR)
switcher.expand
expect(switcher).to have_content("日本語")

SiteSetting.automatic_translation_target_languages = "es"
SiteSetting.experimental_anon_language_switcher = true
visit("/")

switcher.expand
expect(switcher).not_to have_content("日本語")
switcher.click_button("Español")
expect(find(".nav-item_latest")).to have_content("Recientes")

Expand Down
1 change: 0 additions & 1 deletion spec/system/full_page_translation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_cookie = true
SiteSetting.set_locale_from_param = true
SiteSetting.experimental_anon_language_switcher = true
SiteSetting.experimental_inline_translation = true
end

Expand Down
Loading