From 8eeda61ba9e72d3ba2e168e6ce68b562f1d4f871 Mon Sep 17 00:00:00 2001 From: Jonathan Carle Date: Mon, 16 Dec 2024 17:30:02 +0100 Subject: [PATCH 1/4] open up i18n t option type --- packages/sit-onyx/src/i18n/index.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/sit-onyx/src/i18n/index.ts b/packages/sit-onyx/src/i18n/index.ts index a5af56cc55..fce1eb99a7 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, 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 = toRef(options?.locale ?? "en-US"); + const userT = options.t ? toRef(options.t) : undefined; - // If the user provided a custom `t` function it should be used instead of the default one + // 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 (options.t) { - return { t: options.t, locale }; + return { t: userT, locale }; } const messages = computed(() => { From 79d6841143c00e2be3b8cd3a67673d5a6d8bfdef Mon Sep 17 00:00:00 2001 From: Jonathan Carle Date: Mon, 16 Dec 2024 17:31:21 +0100 Subject: [PATCH 2/4] docs(changeset): Custom translation function can now be static --- .changeset/ninety-spiders-return.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/ninety-spiders-return.md 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 From 71bbd915d70566203d96b7f04c8e008b75eb4ab8 Mon Sep 17 00:00:00 2001 From: Jonathan Carle Date: Mon, 16 Dec 2024 17:40:02 +0100 Subject: [PATCH 3/4] fix type error --- packages/sit-onyx/src/i18n/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sit-onyx/src/i18n/index.ts b/packages/sit-onyx/src/i18n/index.ts index fce1eb99a7..de3d4420b5 100644 --- a/packages/sit-onyx/src/i18n/index.ts +++ b/packages/sit-onyx/src/i18n/index.ts @@ -84,7 +84,7 @@ const createI18n = (options: ProvideI18nOptions = {}) => { // 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 (options.t) { + if (userT) { return { t: userT, locale }; } From 85b24d8fd6dd2b2bd341cc0c939a5c5b1a83dd59 Mon Sep 17 00:00:00 2001 From: Jonathan Carle Date: Wed, 18 Dec 2024 10:04:28 +0100 Subject: [PATCH 4/4] make i18n elements readonly --- packages/sit-onyx/src/i18n/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/sit-onyx/src/i18n/index.ts b/packages/sit-onyx/src/i18n/index.ts index de3d4420b5..08b7ce0c24 100644 --- a/packages/sit-onyx/src/i18n/index.ts +++ b/packages/sit-onyx/src/i18n/index.ts @@ -1,4 +1,4 @@ -import { computed, inject, toRef, type App, 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"; @@ -79,8 +79,8 @@ const createI18n = (options: ProvideI18nOptions = {}) => { * Current locale. * @default "en-US" */ - const locale = toRef(options?.locale ?? "en-US"); - const userT = options.t ? toRef(options.t) : undefined; + 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 is used instead of the default. // Then we also skip the loading and applying of messages.