diff --git a/packages/qwik-speak/src/log.ts b/packages/qwik-speak/src/log.ts index b3b8798..46486e7 100644 --- a/packages/qwik-speak/src/log.ts +++ b/packages/qwik-speak/src/log.ts @@ -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 + ); + } }; diff --git a/packages/qwik-speak/src/types.ts b/packages/qwik-speak/src/types.ts index 6b90590..f863673 100644 --- a/packages/qwik-speak/src/types.ts +++ b/packages/qwik-speak/src/types.ts @@ -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 { diff --git a/packages/qwik-speak/src/use-qwik-speak.tsx b/packages/qwik-speak/src/use-qwik-speak.tsx index 5be3c7e..e4238f5 100644 --- a/packages/qwik-speak/src/use-qwik-speak.tsx +++ b/packages/qwik-speak/src/use-qwik-speak.tsx @@ -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 { /** @@ -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, @@ -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) @@ -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 }); } diff --git a/packages/qwik-speak/src/use-speak.ts b/packages/qwik-speak/src/use-speak.ts index 03b0b05..74fb54f 100644 --- a/packages/qwik-speak/src/use-speak.ts +++ b/packages/qwik-speak/src/use-speak.ts @@ -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 { /** @@ -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) } }); };