discourse-translator/assets/javascripts/discourse/components/language-switcher.gjs
Natalie Tay 48682c26c6
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:

de7e213052/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.
2025-01-24 12:01:31 +08:00

64 lines
1.7 KiB
Text

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>
}