-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Show experimental language switcher for anon users (#198)
This commit allows anon users to select a language using a language switcher, this feature is behind an experimental site setting experimental_anon_language_switcher. The set of languages are defined by the same one available to existing users of the site, with the required allow_user_locale setting turned on: https://github.com/discourse/discourse/blob/de7e213052c850aab0258131c47da777ec679621/app/models/locale_site_setting.rb#L37-L50 In the near future, we may consider shortening this list of languages, and will also expand this feature into user-contributed content.
- Loading branch information
Showing
8 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class LanguageSwitcherSettingValidator | ||
def initialize(opts = {}) | ||
@opts = opts | ||
end | ||
|
||
def valid_value?(val) | ||
return true if val == "f" | ||
SiteSetting.set_locale_from_cookie | ||
end | ||
|
||
def error_message | ||
I18n.t("site_settings.errors.set_locale_cookie_requirements") | ||
end | ||
end |
64 changes: 64 additions & 0 deletions
64
assets/javascripts/discourse/components/language-switcher.gjs
This file contains 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,64 @@ | ||
import Component from "@glimmer/component"; | ||
import { fn } from "@ember/helper"; | ||
import { action } from "@ember/object"; | ||
import { service } from "@ember/service"; | ||
import DButton from "discourse/components/d-button"; | ||
import DropdownMenu from "discourse/components/dropdown-menu"; | ||
import cookie from "discourse/lib/cookie"; | ||
import DMenu from "float-kit/components/d-menu"; | ||
|
||
export default class LanguageSwitcher extends Component { | ||
@service site; | ||
@service siteSettings; | ||
@service router; | ||
|
||
get localeOptions() { | ||
return JSON.parse(this.siteSettings.available_locales).map( | ||
({ name, value }) => { | ||
return { | ||
label: name, | ||
value, | ||
}; | ||
} | ||
); | ||
} | ||
|
||
@action | ||
async changeLocale(locale) { | ||
cookie("locale", locale); | ||
this.dMenu.close(); | ||
// we need a hard refresh here for the locale to take effect | ||
window.location.reload(); | ||
} | ||
|
||
@action | ||
onRegisterApi(api) { | ||
this.dMenu = api; | ||
} | ||
|
||
<template> | ||
<DMenu | ||
@identifier="discourse-translator_language-switcher" | ||
title="Language switcher" | ||
@icon="language" | ||
class="btn-flat btn-icon icon" | ||
@onRegisterApi={{this.onRegisterApi}} | ||
> | ||
<:content> | ||
<DropdownMenu as |dropdown|> | ||
{{#each this.localeOptions as |option|}} | ||
<dropdown.item | ||
class="discourse-translator_locale-option" | ||
data-menu-option-id={{option.value}} | ||
> | ||
<DButton | ||
@translatedLabel={{option.label}} | ||
@action={{fn this.changeLocale option.value}} | ||
/> | ||
</dropdown.item> | ||
{{/each}} | ||
</DropdownMenu> | ||
</:content> | ||
</DMenu> | ||
</template> | ||
} |
19 changes: 15 additions & 4 deletions
19
assets/javascripts/discourse/initializers/extend-for-translate-button.js
This file contains 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 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,4 @@ | ||
[data-identifier="discourse-translator_language-switcher"] | ||
.fk-d-menu__inner-content { | ||
max-height: 50vh; | ||
} |
This file contains 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 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 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 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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
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']" | ||
|
||
visit("/") | ||
expect(page).not_to have_css(SWITCHER_SELECTOR) | ||
|
||
SiteSetting.translator_enabled = true | ||
SiteSetting.allow_user_locale = true | ||
SiteSetting.set_locale_from_cookie = true | ||
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 | ||
switcher.click_button("Español") | ||
expect(find(".nav-item_latest")).to have_content("Recientes") | ||
|
||
sign_in(japanese_user) | ||
visit("/") | ||
expect(find(".nav-item_latest")).to have_content("最新") | ||
end | ||
end |