Skip to content

Commit 59bd084

Browse files
committed
fix: remove useless defaults
1 parent 4bef392 commit 59bd084

File tree

6 files changed

+26
-89
lines changed

6 files changed

+26
-89
lines changed

cmp/compiler/src/dev-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
import type { LocaleCode } from "lingo.dev/spec";
2+
13
export const serverUrl = "__SERVER_URL__";
24
export const cacheDir = "__CACHE_DIR__";
5+
export const sourceLocale = "__SOURCE_LOCALE__" as LocaleCode;

cmp/compiler/src/plugin/virtual-modules-code-generator.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import { getCacheDir } from "../utils/path-helpers";
1414
* Exports serverUrl and cacheDir constants
1515
*/
1616
export function generateDevConfigModule(config: LingoConfig): string {
17-
const serverUrl = config.dev.translationServerUrl || "http://127.0.0.1:60000";
17+
const serverUrl = config.dev.translationServerUrl;
1818
const cacheDir = getCacheDir(config);
1919

2020
return `export const serverUrl = ${JSON.stringify(serverUrl)};
2121
export const cacheDir = ${JSON.stringify(cacheDir)};
22+
export const sourceLocale = ${JSON.stringify(config.sourceLocale)};
2223
`;
2324
}
2425

@@ -28,17 +29,8 @@ export const cacheDir = ${JSON.stringify(cacheDir)};
2829
*/
2930
export function generateServerLocaleModule(config: LingoConfig): string {
3031
return `
31-
export async function getServerLocale() {
32-
try {
33-
const { cookies } = await import('next/headers');
34-
const cookieStore = await cookies();
35-
const locale = cookieStore.get(${JSON.stringify(config.cookieConfig.name)})?.value;
36-
return locale || ${JSON.stringify(config.sourceLocale)};
37-
} catch (error) {
38-
// Fallback if cookies are not available
39-
return ${JSON.stringify(config.sourceLocale)};
40-
}
41-
}
32+
import { createNextCookieLocaleResolver } from '@lingo.dev/compiler/react/next';
33+
export const getServerLocale = createNextCookieLocaleResolver({ cookieConfig: ${JSON.stringify(config.cookieConfig)}, defaultLocale: ${JSON.stringify(config.sourceLocale)} });
4234
`;
4335
}
4436

cmp/compiler/src/react/next/cookie-locale-resolver.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
import type { CookieConfig } from "../../types";
99
import { cookies } from "next/headers";
10+
import { logger } from "../../utils/logger";
11+
import type { LocaleCode } from "lingo.dev/spec";
1012

1113
/**
1214
* Configuration for Next.js locale resolver
@@ -16,13 +18,13 @@ export interface NextLocaleResolverConfig {
1618
* Cookie configuration (name and maxAge)
1719
* @default { name: 'locale', maxAge: 31536000 }
1820
*/
19-
cookieConfig?: CookieConfig;
21+
cookieConfig: CookieConfig;
2022

2123
/**
2224
* Default locale if cookie is not set
2325
* @default 'en'
2426
*/
25-
defaultLocale?: string;
27+
defaultLocale: LocaleCode;
2628
}
2729

2830
/**
@@ -33,27 +35,12 @@ export interface NextLocaleResolverConfig {
3335
*
3436
* @param config - Configuration options
3537
* @returns Locale resolver function
36-
*
37-
* @example
38-
* ```typescript
39-
* import { setLocaleResolver } from '@lingo.dev/compiler/react';
40-
* import { createNextCookieLocaleResolver } from '@lingo.dev/compiler/react/next';
41-
*
42-
* // Use default config (cookie name: 'locale', default: 'en')
43-
* setLocaleResolver(createNextCookieLocaleResolver());
44-
*
45-
* // Or customize with cookie config
46-
* setLocaleResolver(createNextCookieLocaleResolver({
47-
* cookieConfig: { name: 'user_language', maxAge: 2592000 },
48-
* defaultLocale: 'de'
49-
* }));
50-
* ```
5138
*/
5239
export function createNextCookieLocaleResolver(
53-
config: NextLocaleResolverConfig = {},
40+
config: NextLocaleResolverConfig,
5441
): () => Promise<string> {
55-
const cookieName = config.cookieConfig?.name || "locale";
56-
const defaultLocale = config.defaultLocale || "en";
42+
const cookieName = config.cookieConfig.name;
43+
const defaultLocale = config.defaultLocale;
5744

5845
return async () => {
5946
try {
@@ -62,9 +49,7 @@ export function createNextCookieLocaleResolver(
6249
} catch (error) {
6350
// Fallback on error (e.g., not in Next.js environment, or cookies() failed)
6451
if (process.env.DEBUG) {
65-
process.stderr.write(
66-
`[lingo.dev] Error reading locale from Next.js cookies: ${error}\n`,
67-
);
52+
logger.error(`Error reading locale from Next.js cookies: ${error}\n`);
6853
}
6954
return defaultLocale;
7055
}

cmp/compiler/src/react/shared/LingoProvider.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import {
4-
type ReactNode,
4+
type PropsWithChildren,
55
useCallback,
66
useEffect,
77
useRef,
@@ -10,7 +10,7 @@ import {
1010
import { logger } from "../../utils/logger";
1111
import type { LingoDevState } from "../../widget/types";
1212
import { fetchTranslations } from "./utils";
13-
import { serverUrl } from "@lingo.dev/compiler/dev-config";
13+
import { serverUrl, sourceLocale } from "@lingo.dev/compiler/dev-config";
1414
import {
1515
getClientLocale,
1616
persistLocale,
@@ -23,17 +23,12 @@ const noop = () => {};
2323
/**
2424
* Translation provider props
2525
*/
26-
export interface LingoProviderProps {
26+
export type LingoProviderProps = PropsWithChildren<{
2727
/**
2828
* Initial locale to use
2929
*/
3030
initialLocale?: LocaleCode;
3131

32-
/**
33-
* Source locale (default language)
34-
*/
35-
sourceLocale?: LocaleCode;
36-
3732
/**
3833
* Initial translations (pre-loaded)
3934
*/
@@ -62,9 +57,7 @@ export interface LingoProviderProps {
6257
*/
6358
position?: "bottom-left" | "bottom-right" | "top-left" | "top-right";
6459
};
65-
66-
children: ReactNode;
67-
}
60+
}>;
6861

6962
const IS_DEV = process.env.NODE_ENV === "development";
7063
const BATCH_DELAY = 200;
@@ -98,8 +91,6 @@ export const LingoProvider = IS_DEV ? LingoProvider__Dev : LingoProvider__Prod;
9891

9992
function LingoProvider__Prod({
10093
initialLocale,
101-
// TODO (AleksandrSl 14/12/2025): Remove sourceLocale here.
102-
sourceLocale = "en",
10394
initialTranslations = {},
10495
router,
10596
children,
@@ -228,7 +219,6 @@ function LingoProvider__Prod({
228219

229220
function LingoProvider__Dev({
230221
initialLocale,
231-
sourceLocale = "en",
232222
initialTranslations = {},
233223
router,
234224
devWidget,
@@ -376,7 +366,7 @@ function LingoProvider__Dev({
376366
setIsLoading(false);
377367
}
378368
}, BATCH_DELAY);
379-
}, [allSeenHashes, locale, sourceLocale, translations]);
369+
}, [allSeenHashes, locale, translations]);
380370

381371
/**
382372
* Clear batch timer on unmount

cmp/compiler/src/react/shared/LocaleSwitcher.tsx

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,8 @@ import type { CSSProperties } from "react";
44
import { useLingoContext } from "./LingoContext";
55
import type { LocaleCode } from "lingo.dev/spec";
66

7-
/**
8-
* Locale configuration
9-
*/
107
export interface LocaleConfig {
11-
/**
12-
* Locale code (e.g., 'en', 'de', 'fr')
13-
*/
148
code: LocaleCode;
15-
16-
/**
17-
* Display name (e.g., 'English', 'Deutsch', 'Français')
18-
*/
199
label: string;
2010
}
2111

@@ -24,10 +14,9 @@ export interface LocaleConfig {
2414
*/
2515
export interface LocaleSwitcherProps {
2616
/**
27-
* Available locales
28-
* @default [{ code: 'en', label: 'English' }]
17+
* Available locales, e.g. [{ code: 'en', label: 'English' }]
2918
*/
30-
locales?: LocaleConfig[];
19+
locales: LocaleConfig[];
3120

3221
/**
3322
* Custom styles for the select element
@@ -78,12 +67,7 @@ export interface LocaleSwitcherProps {
7867
* ```
7968
*/
8069
export function LocaleSwitcher({
81-
locales = [
82-
{
83-
code: "en",
84-
label: "English",
85-
},
86-
],
70+
locales,
8771
style,
8872
className = "lingo-locale-switcher",
8973
showLoadingState = true,

cmp/compiler/src/react/shared/render-rich-text.tsx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Children, Fragment, isValidElement, type ReactNode } from "react";
1+
import { Fragment, type ReactNode } from "react";
22
import IntlMessageFormat, { type FormatXMLElementFn } from "intl-messageformat";
33
import { logger } from "../../utils/logger";
4+
import { sourceLocale } from "@lingo.dev/compiler/dev-config";
45

56
/**
67
* Component renderer function for rich text translation
@@ -12,31 +13,13 @@ export type ComponentRenderer = FormatXMLElementFn<ReactNode>;
1213
*/
1314
export type RichTextParams = Record<string, ReactNode | ComponentRenderer>;
1415

15-
//
16-
/**
17-
* Adds keys to React elements in an array
18-
*/
19-
function toKeyedReactNodeArray(children: Array<ReactNode>): Array<ReactNode> {
20-
return (
21-
// TODO (AleksandrSl 06/12/2025): Seems like map is adding it's own keys anyway, so we can skip the clone?
22-
Children.map(children, (child, index) => {
23-
if (isValidElement(child)) {
24-
return child;
25-
// return cloneElement(child, { key: child.key ?? index });
26-
}
27-
return child;
28-
}) ?? []
29-
);
30-
}
31-
3216
/**
3317
* Wraps a FormatXMLElementFn to automatically assign keys to parts
3418
*/
3519
function assignUniqueKeysToParts(
3620
formatXMLElementFn: FormatXMLElementFn<ReactNode>,
3721
): FormatXMLElementFn<ReactNode> {
3822
return function (parts: Array<ReactNode>) {
39-
// return formatXMLElementFn(toKeyedReactNodeArray(parts));
4023
return formatXMLElementFn(parts);
4124
};
4225
}
@@ -77,7 +60,7 @@ export function renderRichText(
7760
params: RichTextParams,
7861
): ReactNode {
7962
try {
80-
const formatter = new IntlMessageFormat(text, "en");
63+
const formatter = new IntlMessageFormat(text, sourceLocale);
8164
const keyedParams = assignUniqueKeysToParams(params);
8265
const result = formatter.format<ReactNode>(keyedParams);
8366

0 commit comments

Comments
 (0)