-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
dd86a40
commit cb28f7d
Showing
3 changed files
with
61 additions
and
0 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
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,29 @@ | ||
import { usePanel } from "./panel"; | ||
|
||
/** | ||
* Returns translation utility functions. | ||
* | ||
* @remarks | ||
* In most cases, use `window.panel.t` for Kirby's built-in translation function. This composable is useful for custom translation objects, such as those returned by a section's `label` property. | ||
* | ||
* @example | ||
* const { t } = useI18n() | ||
* | ||
* // Simple string | ||
* t("Hello") // -> "Hello" | ||
* | ||
* // Translation object | ||
* t({ en: "Hello", de: "Hallo" }) // -> Returns value based on current Panel language | ||
*/ | ||
export function useI18n() { | ||
const panel = usePanel(); | ||
|
||
function t(value?: string | Record<string, string>) { | ||
if (!value || typeof value === "string") return value; | ||
return value[panel.translation.code] ?? Object.values(value)[0]; | ||
} | ||
|
||
return { | ||
t, | ||
}; | ||
} |
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