Skip to content

Commit 5a511e5

Browse files
committed
type improvements
1 parent f2d0bba commit 5a511e5

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

  • examples/with-paraglide/src/lang

examples/with-paraglide/src/lang/core.tsx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// TYPES AND INTERFACES
33
// ============================================
44

5+
import { A, AnchorProps, useLocation, useNavigate, useParams } from '@solidjs/router';
6+
// @ts-expect-error - Bug of SolidStart
7+
import { PageEvent } from '@solidjs/start/dist/server';
8+
59
import {
610
Accessor,
711
Component,
@@ -11,11 +15,9 @@ import {
1115
ParentProps,
1216
useContext,
1317
} from 'solid-js';
18+
1419
import { m } from '~/paraglide/messages.js';
1520
import { baseLocale, locales } from '~/paraglide/runtime';
16-
import { A, AnchorProps, useLocation, useNavigate, useParams } from '@solidjs/router';
17-
// @ts-ignore
18-
import { PageEvent } from "@solidjs/start/dist/server";
1921

2022
// Exported Types
2123
export type AvailableLanguage = (typeof locales)[number];
@@ -49,7 +51,8 @@ export interface LangContextValue {
4951
// ============================================
5052

5153
const AVAILABLE_LANGUAGES = new Set(locales as unknown as string[]);
52-
const STATIC_ASSET_REGEX = /\.(?:css|js|map|json|ico|svg|png|jpg|jpeg|gif|webp|woff|woff2|ttf|eot)$/i;
54+
const STATIC_ASSET_REGEX =
55+
/\.(?:css|js|map|json|ico|svg|png|jpg|jpeg|gif|webp|woff|woff2|ttf|eot)$/i;
5356
const EXTERNAL_URL_REGEX = /^https?:\/\//;
5457

5558
// ============================================
@@ -106,7 +109,7 @@ function parsePathname(pathname: string): ParsedPath {
106109
}
107110

108111
function buildPath(parts: string[], preserveTrailingSlash: boolean, originalPath: string): string {
109-
if (!parts.length) return '/';
112+
if (parts.length === 0) return '/';
110113

111114
let newPath = `/${parts.join('/')}`;
112115

@@ -154,12 +157,7 @@ export function addLanguageToPath(
154157
}
155158

156159
// Add language preserving format
157-
let newPath: string;
158-
if (path === '/' && preserveRootSlash) {
159-
newPath = `/${lang}/`;
160-
} else {
161-
newPath = `/${lang}${path}`;
162-
}
160+
const newPath = path === '/' && preserveRootSlash ? `/${lang}/` : `/${lang}${path}`;
163161

164162
return newPath + queryAndHash;
165163
}
@@ -190,7 +188,9 @@ export function langServerMiddleware(event: PageEvent): AvailableLanguage {
190188
// ============================================
191189
// PROVIDER COMPONENT
192190
// ============================================
193-
191+
/**
192+
* @returns JSX.Element
193+
*/
194194
export const LangProvider: Component<ParentProps> = (props) => {
195195
const params = useParams();
196196
const navigate = useNavigate();
@@ -199,7 +199,9 @@ export const LangProvider: Component<ParentProps> = (props) => {
199199
// Get current locale from URL params
200200
const locale = createMemo(() => {
201201
const langParam = params.lang;
202-
return (langParam && isAvailableLanguage(langParam) ? langParam : baseLocale) as AvailableLanguage;
202+
return (
203+
langParam && isAvailableLanguage(langParam) ? langParam : baseLocale
204+
) as AvailableLanguage;
203205
});
204206

205207
// Handle client-side language detection and redirection
@@ -214,7 +216,10 @@ export const LangProvider: Component<ParentProps> = (props) => {
214216
};
215217

216218
// Create localized messages with caching
217-
const messageCache = new Map<string, Map<MessageKey, Function>>();
219+
const messageCache = new Map<
220+
string,
221+
Map<MessageKey, (input: object, options: object) => string>
222+
>();
218223

219224
const messages = createMemo(() => {
220225
const currentLocale = locale();
@@ -224,22 +229,26 @@ export const LangProvider: Component<ParentProps> = (props) => {
224229
messageCache.set(currentLocale, new Map());
225230
}
226231

227-
const localeCache = messageCache.get(currentLocale)!;
228-
const localizedMessages = {} as Record<MessageKey, Function>;
232+
const localeCache = messageCache.get(currentLocale) as Map<
233+
MessageKey,
234+
(input: object, options: object) => string
235+
>;
236+
const localizedMessages = {} as Partial<
237+
Record<MessageKey, (input: object, options: object) => string>
238+
>;
229239

230240
// Wrap each message function with the current locale
231241
for (const key in m) {
232242
const messageKey = key as MessageKey;
233243

234244
if (localeCache.has(messageKey)) {
235-
localizedMessages[messageKey] = localeCache.get(messageKey)!;
245+
localizedMessages[messageKey] = localeCache.get(messageKey);
236246
} else {
237247
const originalFn = m[messageKey];
238248

239249
// Create wrapped function that injects locale
240-
const wrappedFn = (inputArgs: {}, options: {}) => {
241-
// @ts-ignore - ignored to pass signature correctly
242-
return originalFn(inputArgs, { locale: currentLocale, ...options });
250+
const wrappedFn = (input: object, options: object) => {
251+
return originalFn(input, { locale: currentLocale, ...options });
243252
};
244253

245254
localeCache.set(messageKey, wrappedFn);
@@ -264,7 +273,7 @@ export const LangProvider: Component<ParentProps> = (props) => {
264273

265274
// Initialize client language on mount
266275
createEffect(() => {
267-
if (typeof window !== 'undefined') {
276+
if (globalThis.window != undefined) {
268277
initializeClientLanguage();
269278
}
270279
});
@@ -276,11 +285,7 @@ export const LangProvider: Component<ParentProps> = (props) => {
276285
setLocale,
277286
};
278287

279-
return (
280-
<LangContext.Provider value={contextValue}>
281-
{props.children}
282-
</LangContext.Provider>
283-
);
288+
return <LangContext.Provider value={contextValue}>{props.children}</LangContext.Provider>;
284289
};
285290

286291
// ============================================
@@ -298,7 +303,9 @@ export function useLang(): LangContextValue {
298303
// ============================================
299304
// COMPONENTS
300305
// ============================================
301-
306+
/**
307+
* @returns JSX.Element
308+
*/
302309
export const LangLink: Component<AnchorProps> = (props) => {
303310
const { locale } = useLang();
304311
// Destructure props to separate href from other props

0 commit comments

Comments
 (0)