Skip to content

Commit

Permalink
Update docs & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed May 16, 2024
1 parent becaf18 commit a7f0c20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ stateDiagram-v2
- `keyValueSeparator` Key-value separator. Default is `@@`
- `rewriteRoutes` Rewrite routes as specified in Vite config for `qwikCity` plugin
- `domainBasedRouting` Domain-based routing options
- `showDebugMessagesLocally` Whether to enable local debugging mode. Default is true

### SpeakLocale
The `SpeakLocale` object contains the `lang`, in the format `language[-script][-region]`, where:
Expand Down
26 changes: 14 additions & 12 deletions packages/qwik-speak/src/use-qwik-speak.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export const useQwikSpeak = (props: QwikSpeakProps) => {
};

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

if (isDev) logWarn(`Locale not resolved. Fallback to default locale: ${props.config.defaultLocale.lang}`);
if (isDev) logWarn(`Locale not resolved. Fallback to default locale: ${resolvedConfig.defaultLocale.lang}`);
} else if (isDev) {
logDebug(resolvedConfig.showDebugMessagesLocally, `Resolved locale: ${resolvedLocale.lang}`);
}
Expand Down Expand Up @@ -116,9 +116,10 @@ export const useQwikSpeak = (props: QwikSpeakProps) => {

// In dev mode, send lang from client to the server
if (isDev) {
logDebugInline(resolvedConfig.showDebugMessagesLocally, 'Ready')
if (import.meta.hot) {
import.meta.hot.send('qwik-speak:lang', { msg: locale.lang });

logDebugInline(resolvedConfig.showDebugMessagesLocally, 'Ready')
}
}
});
Expand All @@ -133,12 +134,6 @@ export const useQwikSpeak = (props: QwikSpeakProps) => {
export const QwikSpeakMockProvider = component$<QwikSpeakMockProps>(props => {
const lang = props.locale?.lang;

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

// Resolve config
const resolvedConfig: SpeakConfig = {
rewriteRoutes: props.config.rewriteRoutes,
Expand All @@ -148,9 +143,16 @@ export const QwikSpeakMockProvider = component$<QwikSpeakMockProps>(props => {
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 = resolvedConfig.supportedLocales.find(value => value.lang === lang);
if (!resolvedLocale) {
resolvedLocale = resolvedConfig.defaultLocale;
}

// Resolve functions
const resolvedTranslationFn: TranslationFn = {
loadTranslation$: props.translationFn?.loadTranslation$ ?? $(() => null)
Expand All @@ -159,7 +161,7 @@ export const QwikSpeakMockProvider = component$<QwikSpeakMockProps>(props => {
// Set initial state as object (no reactive)
const state: SpeakState = {
locale: Object.assign({}, resolvedLocale),
translation: Object.fromEntries(props.config.supportedLocales.map(value => [value.lang, {}])),
translation: Object.fromEntries(resolvedConfig.supportedLocales.map(value => [value.lang, {}])),
config: Object.assign({}, resolvedConfig),
translationFn: resolvedTranslationFn
};
Expand Down

0 comments on commit a7f0c20

Please sign in to comment.