Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support interpolation in log callbacks #76

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions types/Lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export type GenericLanguageObject = {
export type LanguageObject = typeof lang;

export type LangKey = Leaves<LanguageObject>;

export type InterpolationData = {
[identifier: string]: string | number;
};
4 changes: 3 additions & 1 deletion types/LogCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { InterpolationData } from './Lang';

export type LogCallbacks<T extends string> = {
[key in T]?: () => void;
[key in T]?: (interpolationData?: InterpolationData) => void;
};

export type LogCallbacksArg<T extends readonly string[]> = {
Expand Down
11 changes: 6 additions & 5 deletions utils/lang.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import en from '../lang/en.json';
import { LanguageObject, GenericLanguageObject, LangKey } from '../types/Lang';
import {
LanguageObject,
GenericLanguageObject,
LangKey,
InterpolationData,
} from '../types/Lang';

const LANGUAGES: { [language: string]: LanguageObject } = {
en,
Expand Down Expand Up @@ -69,10 +74,6 @@ function generateReplaceFn(
};
}

type InterpolationData = {
[identifier: string]: string | number;
};

export function interpolate(
stringValue: string,
interpolationData: InterpolationData
Expand Down
16 changes: 8 additions & 8 deletions utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { i18n } from './lang';
import { logger } from '../lib/logging/logger';
import { LogCallbacks } from '../types/LogCallbacks';
import { LangKey } from '../types/Lang';
import { InterpolationData, LangKey } from '../types/Lang';

export function log<T extends string>(
key: T,
callbacks?: LogCallbacks<T>,
debugKey?: LangKey,
debugInterpolation?: { [key: string]: string | number }
interpolationData?: InterpolationData
): void {
if (callbacks && callbacks[key]) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callbacks[key]!();
callbacks[key]!(interpolationData);
} else if (debugKey) {
debug(debugKey, debugInterpolation);
debug(debugKey, interpolationData);
}
}

Expand All @@ -25,13 +25,13 @@ export function makeTypedLogger<T extends readonly string[]>(
return (
key: ValidateCallbackKeys,
debugKey?: LangKey,
debugInterpolation?: { [key: string]: string | number }
) => log<ValidateCallbackKeys>(key, callbacks, debugKey, debugInterpolation);
interpolationData?: InterpolationData
) => log<ValidateCallbackKeys>(key, callbacks, debugKey, interpolationData);
}

export function debug(
identifier: LangKey,
interpolation?: { [key: string]: string | number }
interpolationData?: InterpolationData
): void {
logger.debug(i18n(identifier, interpolation));
logger.debug(i18n(identifier, interpolationData));
}
Loading