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

Debug messages in config #128

Merged
merged 4 commits into from
May 14, 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
16 changes: 14 additions & 2 deletions packages/qwik-speak/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ export const logWarn = (message: string) => {
console.warn('\x1b[33mQwik Speak warn\x1b[0m %s', message);
};

export const logDebug = (message: string) => {
console.debug('\x1b[36mQwik Speak\x1b[0m %s', message);
export const logDebug = (enabled: boolean | undefined, message: string) => {
if (enabled) {
console.debug('\x1b[36mQwik Speak\x1b[0m %s', message);
}
};

export const logDebugInline = (enabled: boolean | undefined, ...data: any) => {
if (enabled) {
console.debug(
'%cQwik Speak Inline',
'background: #0c75d2; color: white; padding: 2px 3px; border-radius: 2px; font-size: 0.8em;',
...data
);
}
};
4 changes: 4 additions & 0 deletions packages/qwik-speak/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export interface SpeakConfig {
* Domain-based routing options
*/
domainBasedRouting?: DomainBasedRoutingOption
/**
* Whether to enable local debugging mode. Default is true
*/
showDebugMessagesLocally?: boolean;
}

export interface SpeakState {
Expand Down
42 changes: 17 additions & 25 deletions packages/qwik-speak/src/use-qwik-speak.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isDev, isServer } from '@builder.io/qwik/build';
import type { SpeakConfig, SpeakLocale, SpeakState, TranslationFn } from './types';
import { getSpeakContext, setGetLangFn, setSpeakClientContext, setSpeakServerContext, SpeakContext } from './context';
import { loadTranslations } from './core';
import { logDebug, logWarn } from './log';
import { logDebug, logDebugInline, logWarn } from './log';

export interface QwikSpeakProps {
/**
Expand Down Expand Up @@ -44,18 +44,6 @@ export const useQwikSpeak = (props: QwikSpeakProps) => {
// Get Qwik locale
const lang = getLocale('');

// Resolve locale
let resolvedLocale = props.config.supportedLocales.find(value => value.lang === lang);
if (!resolvedLocale) {
resolvedLocale = props.config.defaultLocale;

if (isDev) logWarn(`Locale not resolved. Fallback to default locale: ${props.config.defaultLocale.lang}`);
} else if (isDev) {
logDebug(`Resolved locale: ${resolvedLocale.lang}`);
}
if (props.currency) resolvedLocale.currency = props.currency;
if (props.timeZone) resolvedLocale.timeZone = props.timeZone;

// Resolve config
const resolvedConfig: SpeakConfig = {
rewriteRoutes: props.config.rewriteRoutes,
Expand All @@ -65,9 +53,22 @@ export const useQwikSpeak = (props: QwikSpeakProps) => {
runtimeAssets: props.config.runtimeAssets,
keySeparator: props.config.keySeparator || '.',
keyValueSeparator: props.config.keyValueSeparator || '@@',
domainBasedRouting: props.config.domainBasedRouting
domainBasedRouting: props.config.domainBasedRouting,
showDebugMessagesLocally: props.config.showDebugMessagesLocally ?? true
};

// Resolve locale
let resolvedLocale = props.config.supportedLocales.find(value => value.lang === lang);
if (!resolvedLocale) {
resolvedLocale = props.config.defaultLocale;

if (isDev) logWarn(`Locale not resolved. Fallback to default locale: ${props.config.defaultLocale.lang}`);
} else if (isDev) {
logDebug(resolvedConfig.showDebugMessagesLocally, `Resolved locale: ${resolvedLocale.lang}`);
}
if (props.currency) resolvedLocale.currency = props.currency;
if (props.timeZone) resolvedLocale.timeZone = props.timeZone;

// Resolve functions
const resolvedTranslationFn: TranslationFn = {
loadTranslation$: props.translationFn?.loadTranslation$ ?? $(() => null)
Expand Down Expand Up @@ -110,21 +111,12 @@ export const useQwikSpeak = (props: QwikSpeakProps) => {

if (isDev) {
const _speakContext = getSpeakContext();
console.debug(
'%cQwik Speak Inline',
'background: #0c75d2; color: white; padding: 2px 3px; border-radius: 2px; font-size: 0.8em;',
'Client context',
_speakContext
);
logDebugInline(resolvedConfig.showDebugMessagesLocally, 'Client context', _speakContext)
}

// In dev mode, send lang from client to the server
if (isDev) {
console.debug(
'%cQwik Speak Inline',
'background: #0c75d2; color: white; padding: 2px 3px; border-radius: 2px; font-size: 0.8em;',
'Ready'
);
logDebugInline(resolvedConfig.showDebugMessagesLocally, 'Ready')
if (import.meta.hot) {
import.meta.hot.send('qwik-speak:lang', { msg: locale.lang });
}
Expand Down
9 changes: 2 additions & 7 deletions packages/qwik-speak/src/use-speak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isBrowser, isDev } from '@builder.io/qwik/build';
import { useSpeakContext } from './use-functions';
import { loadTranslations } from './core';
import { getSpeakContext } from './context';
import { logWarn } from './log';
import { logDebugInline, logWarn } from './log';

export interface SpeakProps {
/**
Expand Down Expand Up @@ -48,12 +48,7 @@ export const useSpeak = (props: SpeakProps) => {

if (isDev && isBrowser) {
const _speakContext = getSpeakContext();
console.debug(
'%cQwik Speak Inline',
'background: #0c75d2; color: white; padding: 2px 3px; border-radius: 2px; font-size: 0.8em;',
'Client context',
_speakContext
);
logDebugInline(config.showDebugMessagesLocally, 'Client context', _speakContext)
}
});
};
Loading