-
Notifications
You must be signed in to change notification settings - Fork 9
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
26 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { AppContext } from "@primate/frontend/react"; | ||
import { useContext } from "react"; | ||
import locale from "./locale.js"; | ||
import reactive_locale from "./locale.js"; | ||
import resolve from "../shared/resolve.js"; | ||
|
||
export default (key, placeholders) => { | ||
locale.init(); | ||
const { i18n } = useContext(AppContext).context; | ||
return i18n.locales[i18n.locale][key] ?? key; | ||
reactive_locale.init(); | ||
const { locales, locale } = useContext(AppContext).context.i18n; | ||
return resolve(locales[locale], key, placeholders); | ||
}; | ||
|
||
export { locale }; | ||
export { reactive_locale as locale }; |
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,9 @@ | ||
export default (locale, key, placeholders = {}) => { | ||
if (locale[key] === undefined) { | ||
return key; | ||
} | ||
|
||
return Object.entries(placeholders).reduce((translated, [p_key, p_value]) => | ||
translated.replaceAll(new RegExp(`\\{${p_key}\\}`, "gu"), () => p_value), | ||
locale[key]); | ||
}; |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { AppContext } from "@primate/frontend/solid"; | ||
import { useContext } from "solid-js"; | ||
import locale from "./locale.js"; | ||
import reactive_locale from "./locale.js"; | ||
import resolve from "../shared/resolve.js"; | ||
|
||
export default (key, placeholders) => { | ||
locale.init(); | ||
const { i18n } = useContext(AppContext).context(); | ||
return i18n.locales[i18n.locale][key] ?? key; | ||
reactive_locale.init(); | ||
const { locales, locale } = useContext(AppContext).context().i18n; | ||
return resolve(locales[locale], key, placeholders); | ||
}; | ||
|
||
export { locale }; | ||
export { reactive_locale as locale }; |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { derived } from "svelte/store"; | ||
import { getContext } from "svelte"; | ||
import locale_store from "./locale.js"; | ||
import reactive_locale from "./locale.js"; | ||
import resolve from "../shared/resolve.js"; | ||
|
||
export default derived(locale_store, locale => (key, placeholders) => { | ||
export default derived(reactive_locale, locale => (key, placeholders) => { | ||
const { locales } = getContext("__primate__").i18n; | ||
return locales[locale][key] ?? key; | ||
return resolve(locales[locale], key, placeholders); | ||
}); | ||
|
||
export { locale_store as locale }; | ||
export { reactive_locale as locale }; |