feat: localizations
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -3,4 +3,6 @@
|
||||
*/
|
||||
|
||||
export { useClipboard } from './useClipboard'
|
||||
export { useL10n, t } from './useL10n'
|
||||
export type { L10N, TranslateParams } from './useL10n'
|
||||
export { useUser } from './useUser'
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Translation composable over the shared i18n runtime.
|
||||
* This is the only surface components and modules use; vue-i18n itself
|
||||
* stays confined to the plugin.
|
||||
*/
|
||||
import { i18n } from '@KTXC/l10n/runtime';
|
||||
|
||||
export type TranslateParams = Record<string, unknown>;
|
||||
|
||||
export interface L10N {
|
||||
/**
|
||||
* Translate a catalog key, rendering the inline English default when the
|
||||
* key is absent from every loaded catalog. Keys are namespaced with the
|
||||
* handle bound at useL10n(); the `en` catalog is generated from these
|
||||
* defaults by scripts/l10n-extract.mjs — keep both literal.
|
||||
*/
|
||||
t: (key: string, defaultText: string, params?: TranslateParams) => string;
|
||||
/** Locale-aware date/time formatting (Intl-backed). */
|
||||
d: typeof i18n.global.d;
|
||||
/** Locale-aware number formatting (Intl-backed). */
|
||||
n: typeof i18n.global.n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a fully-qualified catalog key (one that already carries its
|
||||
* namespace, e.g. an integration entry's `l10n` key, which the integration
|
||||
* store prefixes with the module handle). `defaultText` is rendered when the
|
||||
* key is undefined or absent from every loaded catalog. Such keys are
|
||||
* dynamic, so their catalog entries live in the module's hand-maintained
|
||||
* en.manual.json rather than the extracted catalog.
|
||||
*/
|
||||
export function t(key: string | undefined, defaultText?: string, params: TranslateParams = {}): string {
|
||||
if (!key) return defaultText ?? '';
|
||||
return i18n.global.t(key, params, { default: defaultText ?? key });
|
||||
}
|
||||
|
||||
export function useL10n(namespace: string): L10N {
|
||||
const scoped = (key: string, defaultText: string, params: TranslateParams = {}): string =>
|
||||
t(`${namespace}.${key}`, defaultText, params);
|
||||
|
||||
return { t: scoped, d: i18n.global.d, n: i18n.global.n };
|
||||
}
|
||||
Reference in New Issue
Block a user