Skip to content

Commit f6ea0b4

Browse files
authored
UX: Filter only content-translated languages for the language switcher (#235)
The `experimental_anon_language_switcher` will now rely on values in `automatic_translation_target_languages`. The language switcher cannot be enabled, and will not show up if there are no languages set.
1 parent 02c03bf commit f6ea0b4

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

app/validators/language_switcher_setting_validator.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ def initialize(opts = {})
66
end
77

88
def valid_value?(val)
9-
return true if val == "f"
10-
SiteSetting.set_locale_from_cookie
9+
return true if val == "f" || val == "false"
10+
SiteSetting.set_locale_from_cookie &&
11+
SiteSetting.automatic_translation_target_languages.present?
1112
end
1213

1314
def error_message
14-
I18n.t("site_settings.errors.set_locale_cookie_requirements")
15+
I18n.t("site_settings.errors.experimental_anon_language_switcher_requirements")
1516
end
1617
end

assets/javascripts/discourse/components/language-switcher.gjs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ export default class LanguageSwitcher extends Component {
1313
@service router;
1414

1515
get localeOptions() {
16-
return JSON.parse(this.siteSettings.available_locales).map(
17-
({ name, value }) => {
16+
const targetLanguages = (
17+
this.siteSettings.automatic_translation_target_languages || ""
18+
).split("|");
19+
return JSON.parse(this.siteSettings.available_locales)
20+
.filter(({ value }) => targetLanguages.includes(value))
21+
.map(({ name, value }) => {
1822
return {
1923
label: name,
2024
value,
2125
};
22-
}
23-
);
26+
});
2427
}
2528

2629
@action

assets/javascripts/discourse/initializers/extend-for-translate-button.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ function initializeTranslation(api) {
1616

1717
const currentUser = api.getCurrentUser();
1818

19-
if (!currentUser && siteSettings.experimental_anon_language_switcher) {
19+
if (
20+
!currentUser &&
21+
siteSettings.experimental_anon_language_switcher &&
22+
siteSettings.automatic_translation_target_languages
23+
) {
2024
api.headerIcons.add(
2125
"discourse-translator_language-switcher",
2226
LanguageSwitcher,

config/locales/server.en.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
en:
22
site_settings:
3-
translator_enabled: "Allow inline translation of posts."
3+
translator_enabled: "Enable the translator plugin."
44
translator_provider: "The provider of the translation service."
55
translator_azure_subscription_key: "Azure Subscription Key"
66
translator_azure_region: "Azure Region"
@@ -17,10 +17,10 @@ en:
1717
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."
1818
restrict_translation_by_group: "Only allowed groups can translate"
1919
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."
20-
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."
20+
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."
2121
errors:
22-
set_locale_cookie_requirements: "The experimental language switcher for anonymous users requires the `set locale from cookie` site setting to be enabled."
2322
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."
23+
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."
2424
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."
2525
automatic_translation_target_languages: "The languages to automatically translate user content (posts, topics) to. If empty, no languages will be automatically translated."
2626
translator:

config/settings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ discourse_translator:
1616
validator: "DiscourseTranslator::TranslatorSelectionValidator"
1717
automatic_translation_target_languages:
1818
default: ""
19-
client: true
2019
type: list
20+
client: true
2121
list_type: named
2222
allow_any: false
2323
choices: "DiscourseTranslator::TranslatableLanguagesSetting.values"

spec/system/anon_language_switcher_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
RSpec.describe "Anonymous user language switcher", type: :system do
44
fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
5+
56
it "shows the correct language based on the selected language and login status" do
67
SWITCHER_SELECTOR = "button[data-identifier='discourse-translator_language-switcher']"
78

@@ -11,13 +12,23 @@
1112
SiteSetting.translator_enabled = true
1213
SiteSetting.allow_user_locale = true
1314
SiteSetting.set_locale_from_cookie = true
15+
SiteSetting.automatic_translation_backfill_maximum_translations_per_hour = 1
16+
SiteSetting.automatic_translation_target_languages = "es|ja"
1417
SiteSetting.experimental_anon_language_switcher = true
1518
visit("/")
1619
expect(page).to have_css(SWITCHER_SELECTOR)
1720
expect(find(".nav-item_latest")).to have_content("Latest")
1821

1922
switcher = PageObjects::Components::DMenu.new(SWITCHER_SELECTOR)
2023
switcher.expand
24+
expect(switcher).to have_content("日本語")
25+
26+
SiteSetting.automatic_translation_target_languages = "es"
27+
SiteSetting.experimental_anon_language_switcher = true
28+
visit("/")
29+
30+
switcher.expand
31+
expect(switcher).not_to have_content("日本語")
2132
switcher.click_button("Español")
2233
expect(find(".nav-item_latest")).to have_content("Recientes")
2334

spec/system/full_page_translation_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
SiteSetting.allow_user_locale = true
3434
SiteSetting.set_locale_from_cookie = true
3535
SiteSetting.set_locale_from_param = true
36-
SiteSetting.experimental_anon_language_switcher = true
3736
SiteSetting.experimental_inline_translation = true
3837
end
3938

0 commit comments

Comments
 (0)