fix: typings for getMessage i18n

pull/3206/head
Audric Ackermann 8 months ago
parent 07b9b37abb
commit 38e7d9b922

@ -116,7 +116,7 @@ export type I18nMethods = {
/** @see {@link window.i18n.stripped} */
stripped: <T extends LocalizerToken, R extends LocalizerDictionary[T]>(
...[token, args]: GetMessageArgs<T>
) => R;
) => R | T;
/** @see {@link window.i18n.inEnglish} */
inEnglish: <T extends LocalizerToken, R extends LocalizerDictionary[T]>(
...[token, args]: GetMessageArgs<T>

@ -6,10 +6,13 @@ import {
GetMessageArgs,
ArgsRecord,
DictionaryWithoutPluralStrings,
SetupI18nReturnType,
} from '../../../types/Localizer';
import { i18nLog } from '../shared';
import { formatMessageWithArgs } from './formatMessageWithArgs';
import { getRawMessage } from './getRawMessage';
import { inEnglish } from './inEnglish';
import { stripped } from './stripped';
/**
* Checks if a string contains a dynamic variable.
@ -39,7 +42,7 @@ const isStringWithArgs = <R extends DictionaryWithoutPluralStrings[LocalizerToke
* window.i18n('search', { count: 1, found_count: 1 });
* // => '1 of 1 match'
*/
export function getMessage<T extends LocalizerToken, R extends LocalizerDictionary[T]>(
function getMessageDefault<T extends LocalizerToken, R extends LocalizerDictionary[T]>(
...[token, args]: GetMessageArgs<T>
): R | T {
try {
@ -56,3 +59,10 @@ export function getMessage<T extends LocalizerToken, R extends LocalizerDictiona
return token as R;
}
}
getMessageDefault.inEnglish = inEnglish;
getMessageDefault.stripped = stripped;
getMessageDefault.getRawMessage = getRawMessage;
getMessageDefault.formatMessageWithArgs = formatMessageWithArgs;
export const getMessage: SetupI18nReturnType = getMessageDefault as SetupI18nReturnType;

@ -2,11 +2,7 @@
import { isEmpty } from 'lodash';
import { LocalizerDictionary, SetupI18nReturnType } from '../../types/Localizer';
import { formatMessageWithArgs } from './functions/formatMessageWithArgs';
import { getMessage } from './functions/getMessage';
import { getRawMessage } from './functions/getRawMessage';
import { inEnglish } from './functions/inEnglish';
import { stripped } from './functions/stripped';
import { i18nLog, Locale, setInitialLocale } from './shared';
import { setTranslationDictionary } from './translationDictionaries';
@ -41,14 +37,7 @@ export const setupI18n = ({
setTranslationDictionary(translationDictionary);
setInitialLocale(locale);
const getMessageWithFunctions = getMessage;
// TODO - fix those `any`
(getMessageWithFunctions as any).inEnglish = inEnglish;
(getMessageWithFunctions as any).stripped = stripped;
(getMessageWithFunctions as any).getRawMessage = getRawMessage;
(getMessageWithFunctions as any).formatMessageWithArgs = formatMessageWithArgs;
i18nLog(`Setup Complete with locale: ${locale}`);
return getMessageWithFunctions as SetupI18nReturnType;
return getMessage;
};

Loading…
Cancel
Save