diff --git a/.changeset/ninety-spiders-return.md b/.changeset/ninety-spiders-return.md new file mode 100644 index 0000000000..05d2d910c7 --- /dev/null +++ b/.changeset/ninety-spiders-return.md @@ -0,0 +1,5 @@ +--- +"sit-onyx": minor +--- + +Custom translation function can now be static diff --git a/packages/sit-onyx/src/i18n/index.ts b/packages/sit-onyx/src/i18n/index.ts index a5af56cc55..08b7ce0c24 100644 --- a/packages/sit-onyx/src/i18n/index.ts +++ b/packages/sit-onyx/src/i18n/index.ts @@ -1,12 +1,4 @@ -import { - computed, - inject, - unref, - type App, - type ComputedRef, - type InjectionKey, - type MaybeRef, -} from "vue"; +import { computed, inject, readonly, toRef, type App, type InjectionKey, type MaybeRef } from "vue"; import type { FlattenedKeysOf, TranslationValue } from "../types/i18n"; import type { DeepPartial } from "../types/utils"; import enUS from "./locales/en-US.json"; @@ -68,7 +60,7 @@ export type ProvideI18nOptions = { * * Note: If a custom `t` function is used, passed messages will not be used. */ - t?: ComputedRef; + t?: MaybeRef; }; export type TranslationFunction = ( @@ -87,11 +79,13 @@ const createI18n = (options: ProvideI18nOptions = {}) => { * Current locale. * @default "en-US" */ - const locale = computed(() => unref(options?.locale) ?? "en-US"); + const locale = readonly(toRef(options?.locale ?? "en-US")); + const userT = options.t ? readonly(toRef(options.t)) : undefined; - // If the user provided a custom `t` function it should be used instead of the default one - if (options.t) { - return { t: options.t, locale }; + // If the user provided a custom `t` function, it is used instead of the default. + // Then we also skip the loading and applying of messages. + if (userT) { + return { t: userT, locale }; } const messages = computed(() => {