From c5b8ff0c1f0f62e7d5dd84aca32380b6af9333b7 Mon Sep 17 00:00:00 2001 From: syuilo <4439005+syuilo@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:32:23 +0900 Subject: [PATCH] wip --- packages/embed/src/components/I18n.vue | 51 ++++++++++++++++++++++++++ packages/embed/src/config.ts | 18 +++++++++ packages/embed/src/i18n.ts | 15 ++++++++ 3 files changed, 84 insertions(+) create mode 100644 packages/embed/src/components/I18n.vue create mode 100644 packages/embed/src/config.ts create mode 100644 packages/embed/src/i18n.ts diff --git a/packages/embed/src/components/I18n.vue b/packages/embed/src/components/I18n.vue new file mode 100644 index 0000000000..6b7723e6ac --- /dev/null +++ b/packages/embed/src/components/I18n.vue @@ -0,0 +1,51 @@ +<!-- +SPDX-FileCopyrightText: syuilo and misskey-project +SPDX-License-Identifier: AGPL-3.0-only +--> + +<template> +<render/> +</template> + +<script setup lang="ts" generic="T extends string | ParameterizedString"> +import { computed, h } from 'vue'; +import type { ParameterizedString } from '../../../../../locales/index.js'; + +const props = withDefaults(defineProps<{ + src: T; + tag?: string; + // eslint-disable-next-line vue/require-default-prop + textTag?: string; +}>(), { + tag: 'span', +}); + +const slots = defineSlots<T extends ParameterizedString<infer R> ? { [K in R]: () => unknown } : NonNullable<unknown>>(); + +const parsed = computed(() => { + let str = props.src as string; + const value: (string | { arg: string; })[] = []; + for (;;) { + const nextBracketOpen = str.indexOf('{'); + const nextBracketClose = str.indexOf('}'); + + if (nextBracketOpen === -1) { + value.push(str); + break; + } else { + if (nextBracketOpen > 0) value.push(str.substring(0, nextBracketOpen)); + value.push({ + arg: str.substring(nextBracketOpen + 1, nextBracketClose), + }); + } + + str = str.substring(nextBracketClose + 1); + } + + return value; +}); + +const render = () => { + return h(props.tag, parsed.value.map(x => typeof x === 'string' ? (props.textTag ? h(props.textTag, x) : x) : slots[x.arg]())); +}; +</script> diff --git a/packages/embed/src/config.ts b/packages/embed/src/config.ts new file mode 100644 index 0000000000..08f14dfdd8 --- /dev/null +++ b/packages/embed/src/config.ts @@ -0,0 +1,18 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +const address = new URL(document.querySelector<HTMLMetaElement>('meta[property="instance_url"]')?.content || location.href); +const siteName = document.querySelector<HTMLMetaElement>('meta[property="og:site_name"]')?.content; + +export const host = address.host; +export const hostname = address.hostname; +export const url = address.origin; +export const apiUrl = location.origin + '/api'; +export const langs = _LANGS_; +const preParseLocale = miLocalStorage.getItem('locale'); +export const locale = preParseLocale ? JSON.parse(preParseLocale) : null; +export const instanceName = siteName === 'Misskey' || siteName == null ? host : siteName; +export const ui = miLocalStorage.getItem('ui'); +export const debug = miLocalStorage.getItem('debug') === 'true'; diff --git a/packages/embed/src/i18n.ts b/packages/embed/src/i18n.ts new file mode 100644 index 0000000000..10d6adbcd0 --- /dev/null +++ b/packages/embed/src/i18n.ts @@ -0,0 +1,15 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { markRaw } from 'vue'; +import type { Locale } from '../../../locales/index.js'; +import { locale } from '@/config.js'; +import { I18n } from '@/scripts/i18n.js'; + +export const i18n = markRaw(new I18n<Locale>(locale)); + +export function updateI18n(newLocale: Locale) { + i18n.locale = newLocale; +}