-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
packages/component-library/src/components/form/my-i18n/my-i18n.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<template> | ||
{{ t("foo.bar") }} | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { useI18n } from "@/utils/i18n"; | ||
const { t } = useI18n({ | ||
messages: { | ||
de: { | ||
foo: { | ||
bar: "Hallo Welt", | ||
}, | ||
}, | ||
en: { | ||
foo: { | ||
bar: "Hello World", | ||
}, | ||
}, | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/component-library/src/composables/useI18n.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |