feat(quick-261016-0fm): language switcher in the user menu
- loadStrings sends the chosen language as ?locale= and keeps meta.locales as availableLocales - the user menu lists the admin locales, each named in its own language; choosing one saves it through PUT /account/preferences, caches it and reloads the page - boot loads the strings in the cached language; me() reloads them when the stored preference differs
This commit is contained in:
@@ -17,6 +17,11 @@ const locale = ref(typeof document === 'undefined' ? 'pl' : document.documentEle
|
||||
/** The locale the loaded bundle resolved to (meta.locale of GET /lang). */
|
||||
export const currentLocale = readonly(locale)
|
||||
|
||||
const locales = ref<string[]>([])
|
||||
|
||||
/** The supported admin locales (meta.locales of GET /lang). */
|
||||
export const availableLocales = readonly(locales)
|
||||
|
||||
export function setBundle(next: LangBundle, nextLocale?: string): void {
|
||||
bundle.value = next
|
||||
if (nextLocale) {
|
||||
@@ -27,12 +32,17 @@ export function setBundle(next: LangBundle, nextLocale?: string): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** Loads the backend::lang bundle for the admin's locale. Public endpoint. */
|
||||
export async function loadStrings(): Promise<void> {
|
||||
const { data, response } = await api.GET('/lang')
|
||||
/**
|
||||
* Loads the backend::lang bundle. Public endpoint. preferred (the admin's
|
||||
* chosen language) is sent as ?locale=; the server uses it only when it is
|
||||
* a supported admin locale, else the request locale.
|
||||
*/
|
||||
export async function loadStrings(preferred?: string): Promise<void> {
|
||||
const { data, response } = await api.GET('/lang', preferred ? { params: { query: { locale: preferred } } } : {})
|
||||
if (!response.ok || !data) {
|
||||
return
|
||||
}
|
||||
locales.value = Array.isArray(data.meta.locales) ? [...data.meta.locales] : []
|
||||
setBundle(data.data, data.meta.locale)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user