mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-06 22:58:19 -05:00
cleanup in frontend localization system
This commit is contained in:
@@ -2135,7 +2135,7 @@ class LocalizationViewSet(viewsets.GenericViewSet):
|
|||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
langs = []
|
langs = []
|
||||||
for l in settings.LANGUAGES:
|
for l in settings.LANGUAGES:
|
||||||
langs.append({'code': l[0], 'language': l[1]})
|
langs.append({'code': l[0], 'language': f'{l[1]} ({l[0]})'})
|
||||||
return Response(LocalizationSerializer(langs, many=True).data)
|
return Response(LocalizationSerializer(langs, many=True).data)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
import {nextTick, isRef} from 'vue'
|
import {nextTick} from 'vue'
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
I18n,
|
I18n,
|
||||||
Locale,
|
Locale,
|
||||||
VueI18n,
|
|
||||||
Composer,
|
|
||||||
I18nMode
|
|
||||||
} from 'vue-i18n'
|
} from 'vue-i18n'
|
||||||
|
|
||||||
import {createI18n} from "vue-i18n";
|
import {createI18n} from "vue-i18n";
|
||||||
@@ -22,7 +19,6 @@ import en from "../../vue3/src/locales/en.json";
|
|||||||
*/
|
*/
|
||||||
export const SUPPORT_LOCALES = getSupportedLocales()
|
export const SUPPORT_LOCALES = getSupportedLocales()
|
||||||
|
|
||||||
|
|
||||||
export function setupI18n() {
|
export function setupI18n() {
|
||||||
let locale = document.querySelector('html')!.getAttribute('lang')
|
let locale = document.querySelector('html')!.getAttribute('lang')
|
||||||
if (locale == null || !SUPPORT_LOCALES.includes(locale)) {
|
if (locale == null || !SUPPORT_LOCALES.includes(locale)) {
|
||||||
@@ -32,7 +28,6 @@ export function setupI18n() {
|
|||||||
|
|
||||||
// load i18n as with locale en by default
|
// load i18n as with locale en by default
|
||||||
const i18n = createI18n({
|
const i18n = createI18n({
|
||||||
legacy: false,
|
|
||||||
locale: 'en',
|
locale: 'en',
|
||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
messages: {
|
messages: {
|
||||||
@@ -41,12 +36,11 @@ export function setupI18n() {
|
|||||||
}) as I18n
|
}) as I18n
|
||||||
|
|
||||||
// async load user locale into existing i18n instance
|
// async load user locale into existing i18n instance
|
||||||
loadLocaleMessages(i18n, locale).then(r => {})
|
loadLocaleMessages(i18n, locale).then()
|
||||||
|
|
||||||
return i18n
|
return i18n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* load specified locale messages from server and set the locale as active
|
* load specified locale messages from server and set the locale as active
|
||||||
* @param i18n instance of Vue i18n
|
* @param i18n instance of Vue i18n
|
||||||
@@ -55,11 +49,10 @@ export function setupI18n() {
|
|||||||
export async function loadLocaleMessages(i18n: I18n, locale: Locale) {
|
export async function loadLocaleMessages(i18n: I18n, locale: Locale) {
|
||||||
// load locale messages
|
// load locale messages
|
||||||
const messages = await import(`./locales/${locale}.json`).then(
|
const messages = await import(`./locales/${locale}.json`).then(
|
||||||
getResourceMessages
|
(r: any) => r.default || r
|
||||||
)
|
)
|
||||||
|
|
||||||
// remove empty strings
|
// remove empty strings
|
||||||
|
|
||||||
Object.entries(messages).forEach(([key, value]) => {
|
Object.entries(messages).forEach(([key, value]) => {
|
||||||
if (value === '') {
|
if (value === '') {
|
||||||
delete messages[key]
|
delete messages[key]
|
||||||
@@ -71,11 +64,8 @@ export async function loadLocaleMessages(i18n: I18n, locale: Locale) {
|
|||||||
|
|
||||||
// switch to given locale
|
// switch to given locale
|
||||||
setLocale(i18n, locale)
|
setLocale(i18n, locale)
|
||||||
|
|
||||||
return nextTick()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* loop trough translation files to determine for which locales a translation is available
|
* loop trough translation files to determine for which locales a translation is available
|
||||||
* @return string[] of supported locales
|
* @return string[] of supported locales
|
||||||
@@ -89,39 +79,11 @@ function getSupportedLocales() {
|
|||||||
return supportedLocales
|
return supportedLocales
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* determines something about the way Vue i18n is installed/setup?
|
|
||||||
* @param instance
|
|
||||||
* @param mode
|
|
||||||
*/
|
|
||||||
function isComposer(instance: VueI18n | Composer, mode: I18nMode): instance is Composer {
|
|
||||||
return mode === 'composition' && isRef(instance.locale)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get the currently active locale of Vue i18n
|
|
||||||
* @param i18n instance of Vue i18n
|
|
||||||
*/
|
|
||||||
export function getLocale(i18n: I18n): string {
|
|
||||||
if (isComposer(i18n.global, i18n.mode)) {
|
|
||||||
return i18n.global.locale.value
|
|
||||||
} else {
|
|
||||||
return i18n.global.locale
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set the active locale (determining which messages to show) for Vue i18n
|
* set the active locale (determining which messages to show) for Vue i18n
|
||||||
* @param i18n instance of Vue i18n
|
* @param i18n instance of Vue i18n
|
||||||
* @param locale string locale code to set (should be in SUPPORT_LOCALES)
|
* @param locale string locale code to set (should be in SUPPORT_LOCALES)
|
||||||
*/
|
*/
|
||||||
export function setLocale(i18n: I18n, locale: Locale): void {
|
export function setLocale(i18n: I18n, locale: Locale): void {
|
||||||
if (isComposer(i18n.global, i18n.mode)) {
|
|
||||||
i18n.global.locale.value = locale
|
|
||||||
} else {
|
|
||||||
i18n.global.locale = locale
|
i18n.global.locale = locale
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
const getResourceMessages = (r: any) => r.default || r
|
|
||||||
Reference in New Issue
Block a user