-
Notifications
You must be signed in to change notification settings - Fork 52
FEATURE: Show experimental language switcher for anon users #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9dc24a4
FEATURE: Show language switcher for anon users
nattsw 368310e
Tests
nattsw 8e6303e
restrict height
nattsw 113acae
litn
nattsw e8614bd
Remove change from other branch
nattsw 70e269a
Remove test from other branch
nattsw a592111
Put back a removed button by accident
nattsw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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> | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yikes! It's not perfect, this was accidentally removed. Will add it back.