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

add useI18n composable #328

Merged
merged 2 commits into from
Oct 2, 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
</template>

<script setup lang="ts">
import { provideI18n } from "@/composables/useI18n";
import { type FutureFlags, provideFutureFlags } from "../../composables/useFutureFlags";

type Props = {
const props = defineProps<{
future?: FutureFlags;
};

const props = defineProps<Props>();
locale?: string;
}>();

provideFutureFlags(props.future);
provideI18n(props.locale);
</script>
34 changes: 34 additions & 0 deletions packages/component-library/src/composables/useI18n.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { render, screen } from "@testing-library/vue";
import { useI18n } from "./useI18n";

describe("useI18n", () => {
it("returns the translation for the given path", () => {
render({
setup() {
const { t } = useI18n({
messages: { en: { greeting: "Hello!" } },
});

return { t };
},
template: "{{ t('greeting') }}",
});

expect(screen.getByText("Hello!")).toBeInTheDocument();
});

it("returns the path to the translation if no translation is found", () => {
render({
setup() {
const { t } = useI18n({
messages: { en: { greeting: "Hello!" } },
});

return { t };
},
template: "{{ t('path.to.translation') }}",
});

expect(screen.getByText("path.to.translation")).toBeInTheDocument();
});
});
53 changes: 53 additions & 0 deletions packages/component-library/src/composables/useI18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { provide, inject } from "vue";

function get(obj: Record<string, unknown>, path: string) {
if (typeof obj !== "object" || obj === null) return undefined;

const keys = path.split(".");
let result = obj;

for (const key of keys) {
if (result === undefined || result === null) return undefined;

// @ts-ignore
result = result[key];
}

return result;
}

interface TranslationDictionary {
[key: string]: string | TranslationDictionary;
}

type Options = { messages: TranslationDictionary };

const defaultI18nState = {
locale: "en",
defaultLocale: "en",
};

const i18nInjectionKey = Symbol("mt-i18n");

export function provideI18n(locale: string = "en") {
const state = {
...defaultI18nState,
locale: locale,
};

provide(i18nInjectionKey, state);
}

export function useI18n({ messages }: Options) {
const i18n = inject(i18nInjectionKey, defaultI18nState);

function translate(path: string): string {
const translation = get(messages, `${i18n.locale}.${path}`);
if (translation) return translation.toString();

const fallback = get(messages, `${i18n.defaultLocale}.${path}`);
return fallback ? fallback.toString() : path;
}

return { t: translate };
}
Loading