Skip to content

Commit

Permalink
chore(i18n): 🏷️ fix t type
Browse files Browse the repository at this point in the history
  • Loading branch information
willin committed Dec 11, 2023
1 parent 40d5a71 commit a50a15d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/i18n/src/lib/i18n/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import dlv from 'dlv';
import templite from 'templite';
import { derived } from 'svelte/store';
import { derived, type Readable } from 'svelte/store';
import { $dictionary, getMessageFromDictionary } from './dictionary.js';
import { $locale, getCurrentLocale } from './locale.js';
import type { MessageFormatter } from './types.js';

const formatMessage = (key: string, params?: any, lang?: string): string => {
const formatMessage: MessageFormatter = (key: string, params?: any, lang?: string): string => {
const dict = getMessageFromDictionary(lang || (getCurrentLocale() as string));
const val = dlv(dict as any, key, key);
if (typeof val === 'function') return val(params) as string;
Expand All @@ -14,4 +15,4 @@ const formatMessage = (key: string, params?: any, lang?: string): string => {
return val as string;
};

export const $t = derived([$locale, $dictionary], () => formatMessage);
export const $t: Readable<MessageFormatter> = derived([$locale, $dictionary], () => formatMessage);
6 changes: 6 additions & 0 deletions packages/i18n/src/lib/i18n/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ export type Fn = (...args: any[]) => string;
export interface I18nDict {
[key: string]: string | number | Fn | I18nDict;
}

export type MessageFormatter = (
key: string,
params?: Record<string, unknown>,
lang?: string
) => string;

0 comments on commit a50a15d

Please sign in to comment.