Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n): Custom translation function can now be static #2347

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-spiders-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sit-onyx": minor
---

Custom translation function can now be static
22 changes: 8 additions & 14 deletions packages/sit-onyx/src/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -68,7 +60,7 @@ export type ProvideI18nOptions = {
*
* Note: If a custom `t` function is used, passed messages will not be used.
*/
t?: ComputedRef<TranslationFunction>;
t?: MaybeRef<TranslationFunction>;
};

export type TranslationFunction = (
Expand All @@ -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 };
}
JoCa96 marked this conversation as resolved.
Show resolved Hide resolved

const messages = computed(() => {
Expand Down
Loading