@ultimat3/i18n 22.2.1 → 22.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/context.ts +18 -0
- package/src/index.ts +8 -0
- package/src/locale-path.ts +37 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/i18n",
|
|
3
|
-
"version": "22.
|
|
3
|
+
"version": "22.3.0",
|
|
4
4
|
"description": "Dependency-free translator, catalog flattening, locale negotiation and loud missing-key rendering",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,6 +35,6 @@
|
|
|
35
35
|
"test": "bun test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ultimat3/core": "22.
|
|
38
|
+
"@ultimat3/core": "22.3.0"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/src/context.ts
CHANGED
|
@@ -102,6 +102,24 @@ export function configureLocales(partial: Partial<LocaleConfig>): LocaleConfig {
|
|
|
102
102
|
return config;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
const isShippedSet = (supported: readonly Locale[]): boolean =>
|
|
106
|
+
supported.length === SUPPORTED_LOCALES.length &&
|
|
107
|
+
supported.every((locale, index) => locale === SUPPORTED_LOCALES[index]);
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The locales a URL may name, the default first: the app's declared set, or the fallback alone
|
|
111
|
+
* when it declared none. What prefix routing, the per-locale prerender, hreflang
|
|
112
|
+
* and the sitemap all enumerate — one list, so none of them can name a page another never made.
|
|
113
|
+
*/
|
|
114
|
+
export function routedLocales(): readonly Locale[] {
|
|
115
|
+
// The shipped `supported` set is the thirty tags the framework can NEGOTIATE, not the ones an app
|
|
116
|
+
// translated: routing it would publish thirty copies of an English-only site. Compared by value,
|
|
117
|
+
// never tracked by a flag — `@ultimat3/testing`'s snapshot restores the config through
|
|
118
|
+
// `configureLocales`, and a flag set by that call would read the defaults as a declaration.
|
|
119
|
+
if (isShippedSet(config.supported)) return [config.fallback];
|
|
120
|
+
return [config.fallback, ...config.supported.filter((locale) => locale !== config.fallback)];
|
|
121
|
+
}
|
|
122
|
+
|
|
105
123
|
export function localeConfig(): LocaleConfig {
|
|
106
124
|
return config;
|
|
107
125
|
}
|
package/src/index.ts
CHANGED
|
@@ -36,6 +36,7 @@ export {
|
|
|
36
36
|
resetCatalogs,
|
|
37
37
|
resetLocaleConfig,
|
|
38
38
|
resolveLocale,
|
|
39
|
+
routedLocales,
|
|
39
40
|
t,
|
|
40
41
|
translatorFor,
|
|
41
42
|
useI18n,
|
|
@@ -83,6 +84,13 @@ export {
|
|
|
83
84
|
pluralVariantsOf,
|
|
84
85
|
selectPluralKey,
|
|
85
86
|
} from './interpolate';
|
|
87
|
+
export {
|
|
88
|
+
type LocalePrefix,
|
|
89
|
+
localeSegment,
|
|
90
|
+
localizedPath,
|
|
91
|
+
splitLocalePrefix,
|
|
92
|
+
unlocalizedPath,
|
|
93
|
+
} from './locale-path';
|
|
86
94
|
export {
|
|
87
95
|
assertSupportedLocale,
|
|
88
96
|
DEFAULT_LOCALE,
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A locale in the URL, against the APP's configured locales: how a leading `/<locale>/` segment is
|
|
3
|
+
* split off a pathname, and how a path is spelled in a locale. The arithmetic is core's
|
|
4
|
+
* (`locale-path.ts`, tier 0 so a browser chunk can spell a link without this barrel); this file
|
|
5
|
+
* supplies the routed locales and the default, so `@ultimat3/http` routes, `@ultimat3/render` hands
|
|
6
|
+
* `meta` and the prerender writes files from one answer.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { type LocalePathSplit, localeSegment, localizePath, splitLocalePath } from '@ultimat3/core';
|
|
10
|
+
import { localeConfig, routedLocales } from './context';
|
|
11
|
+
import type { Locale } from './locales';
|
|
12
|
+
|
|
13
|
+
export { localeSegment };
|
|
14
|
+
export type LocalePrefix = LocalePathSplit;
|
|
15
|
+
|
|
16
|
+
/** `/en/precios` → `{ locale: 'en', path: '/precios', isDefault: false }`, or `undefined`. */
|
|
17
|
+
export function splitLocalePrefix(
|
|
18
|
+
pathname: string,
|
|
19
|
+
locales: readonly Locale[] = routedLocales(),
|
|
20
|
+
fallback: Locale = localeConfig().fallback,
|
|
21
|
+
): LocalePrefix | undefined {
|
|
22
|
+
return splitLocalePath(pathname, locales, fallback);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** `path` as `locale` spells it — unprefixed for the default locale, `/en/…` otherwise. */
|
|
26
|
+
export function localizedPath(
|
|
27
|
+
path: string,
|
|
28
|
+
locale: Locale,
|
|
29
|
+
fallback: Locale = localeConfig().fallback,
|
|
30
|
+
): string {
|
|
31
|
+
return localizePath(path, locale, routedLocales(), fallback);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The pathname with any routed locale prefix removed — the path the route table matches. */
|
|
35
|
+
export function unlocalizedPath(pathname: string): string {
|
|
36
|
+
return splitLocalePrefix(pathname)?.path ?? pathname;
|
|
37
|
+
}
|