@hamur/i18n 0.0.1 → 0.0.3
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/dist/src/cli.js +86 -6
- package/dist/src/index.js +88 -10
- package/dist/types/src/adapters/angular.d.ts +3 -0
- package/dist/types/src/build/access.d.ts +8 -0
- package/dist/types/src/types.d.ts +1 -1
- package/package.json +53 -20
- package/src/adapters/angular.ts +49 -56
- package/src/adapters/test/preact-client.ts +8 -4
- package/src/build/access.ts +81 -0
- package/src/build/messages.ts +1 -0
- package/src/build/routes.ts +8 -0
- package/src/types.ts +2 -1
package/dist/src/cli.js
CHANGED
|
@@ -961,6 +961,7 @@ import { relative } from "path";
|
|
|
961
961
|
import {
|
|
962
962
|
DiagnosticError as DiagnosticError4,
|
|
963
963
|
addAppPlugin,
|
|
964
|
+
addRouteRules,
|
|
964
965
|
addRouteMiddleware,
|
|
965
966
|
addTemplate,
|
|
966
967
|
createResolver,
|
|
@@ -968,6 +969,77 @@ import {
|
|
|
968
969
|
extendPages,
|
|
969
970
|
useLogger
|
|
970
971
|
} from "hamur/kit";
|
|
972
|
+
|
|
973
|
+
// packages/i18n/src/build/access.ts
|
|
974
|
+
import { createRouteRules } from "hamur/kit";
|
|
975
|
+
var KEYS = ["basicAuth", "ipRestriction"];
|
|
976
|
+
function accessOf(value) {
|
|
977
|
+
const access = {};
|
|
978
|
+
for (const key of KEYS) {
|
|
979
|
+
const found = Array.isArray(value) ? value.find((entry) => entry[key] !== undefined)?.[key] : value?.[key];
|
|
980
|
+
if (found !== undefined)
|
|
981
|
+
Object.assign(access, { [key]: found });
|
|
982
|
+
}
|
|
983
|
+
return access;
|
|
984
|
+
}
|
|
985
|
+
var prefixed = (code, path) => `/${code}${path === "/" ? "" : path}`;
|
|
986
|
+
var flatten2 = (records) => records.flatMap((record) => [record, ...flatten2(record.children)]);
|
|
987
|
+
function localeAccessRules(rules, originals, table, defaultLocale) {
|
|
988
|
+
const picked = Object.entries(rules).flatMap(([key, value]) => {
|
|
989
|
+
const access = accessOf(value);
|
|
990
|
+
return Object.keys(access).length ? [[key, access]] : [];
|
|
991
|
+
});
|
|
992
|
+
const out = {};
|
|
993
|
+
if (!picked.length)
|
|
994
|
+
return out;
|
|
995
|
+
const add = (key, access) => {
|
|
996
|
+
const own = accessOf(rules[key]);
|
|
997
|
+
for (const name of KEYS)
|
|
998
|
+
if (access[name] !== undefined && own[name] === undefined && out[key]?.[name] === undefined)
|
|
999
|
+
Object.assign(out[key] ??= {}, { [name]: access[name] });
|
|
1000
|
+
};
|
|
1001
|
+
const split = (key) => ({ method: key.slice(0, key.indexOf("/")), path: key.slice(key.indexOf("/")) });
|
|
1002
|
+
for (const [key, access] of picked) {
|
|
1003
|
+
const { method, path } = split(key);
|
|
1004
|
+
for (const code of table.copies)
|
|
1005
|
+
add(`${method}${prefixed(code, path)}`, access);
|
|
1006
|
+
}
|
|
1007
|
+
const records = flatten2(originals);
|
|
1008
|
+
const own = (path, code, localized) => localized === path || localized === prefixed(code, path);
|
|
1009
|
+
const lists = [table.routes, table.aliases ?? {}];
|
|
1010
|
+
const compiled = createRouteRules(Object.fromEntries(picked));
|
|
1011
|
+
for (const record of records) {
|
|
1012
|
+
const access = accessOf(compiled.match(record.path).rule);
|
|
1013
|
+
if (!Object.keys(access).length)
|
|
1014
|
+
continue;
|
|
1015
|
+
for (const list of lists)
|
|
1016
|
+
for (const [code, localized] of Object.entries(list[record.name] ?? {}))
|
|
1017
|
+
if (!table.missing?.[record.name]?.includes(code) && !own(record.path, code, localized))
|
|
1018
|
+
add(localized, access);
|
|
1019
|
+
}
|
|
1020
|
+
const source = createMatcher({
|
|
1021
|
+
copies: [],
|
|
1022
|
+
routes: Object.fromEntries(records.map((record) => [record.name, { [defaultLocale]: record.path }]))
|
|
1023
|
+
});
|
|
1024
|
+
const builders = lists.map((routes) => createMatcher({ copies: [], routes }));
|
|
1025
|
+
for (const [key, access] of picked) {
|
|
1026
|
+
const { method, path } = split(key);
|
|
1027
|
+
const found = /[:*]/.test(path) ? undefined : source.matchPath(path);
|
|
1028
|
+
if (!found)
|
|
1029
|
+
continue;
|
|
1030
|
+
for (const [index, builder] of builders.entries())
|
|
1031
|
+
for (const code of Object.keys(lists[index][found.name] ?? {})) {
|
|
1032
|
+
if (table.missing?.[found.name]?.includes(code))
|
|
1033
|
+
continue;
|
|
1034
|
+
const localized = builder.buildPath(found.name, code, found.params);
|
|
1035
|
+
if (!own(path, code, localized))
|
|
1036
|
+
add(`${method}${localized}`, access);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
return out;
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
// packages/i18n/src/build/routes.ts
|
|
971
1043
|
var SEPARATOR = "___";
|
|
972
1044
|
function paramsOf(segments) {
|
|
973
1045
|
return segments.flatMap((segment) => {
|
|
@@ -989,12 +1061,12 @@ function shapeOf(path) {
|
|
|
989
1061
|
});
|
|
990
1062
|
return `/${parts.join("/")}`;
|
|
991
1063
|
}
|
|
992
|
-
function
|
|
993
|
-
return records.flatMap((record) => [...
|
|
1064
|
+
function flatten3(records, ancestors = []) {
|
|
1065
|
+
return records.flatMap((record) => [...flatten3(record.children, [...ancestors, record.name]), { record, ancestors }]);
|
|
994
1066
|
}
|
|
995
1067
|
function tableOf(records, options, copies) {
|
|
996
1068
|
const table = { copies, routes: {}, aliases: {} };
|
|
997
|
-
for (const { record } of
|
|
1069
|
+
for (const { record } of flatten3(records)) {
|
|
998
1070
|
const index = record.name.indexOf(SEPARATOR);
|
|
999
1071
|
const base = index === -1 ? record.name : record.name.slice(0, index);
|
|
1000
1072
|
const code = index === -1 ? options.defaultLocale : record.name.slice(index + SEPARATOR.length);
|
|
@@ -1007,7 +1079,7 @@ function localizeRoutes(routes, options, rootDir = "") {
|
|
|
1007
1079
|
if (options.strategy === "no_prefix")
|
|
1008
1080
|
return { routes, table: tableOf(routes, options, []) };
|
|
1009
1081
|
const shown = (file) => rootDir ? relative(rootDir, file) : file;
|
|
1010
|
-
const originals =
|
|
1082
|
+
const originals = flatten3(routes);
|
|
1011
1083
|
for (const { record } of originals) {
|
|
1012
1084
|
if (record.name.includes(SEPARATOR))
|
|
1013
1085
|
throw new DiagnosticError4("HAMUR_E5816", `i18n: route name "${record.name}" (${shown(record.file)}) contains "___", which route copies use`);
|
|
@@ -1068,7 +1140,7 @@ function localizeRoutes(routes, options, rootDir = "") {
|
|
|
1068
1140
|
return true;
|
|
1069
1141
|
if (!differs(record, code))
|
|
1070
1142
|
return false;
|
|
1071
|
-
const paths =
|
|
1143
|
+
const paths = flatten3([copy(record, code)]).map((entry) => entry.record.path).join(`
|
|
1072
1144
|
`);
|
|
1073
1145
|
let seen = made.get(record);
|
|
1074
1146
|
if (!seen)
|
|
@@ -1099,7 +1171,7 @@ function localizeRoutes(routes, options, rootDir = "") {
|
|
|
1099
1171
|
}
|
|
1100
1172
|
}
|
|
1101
1173
|
}
|
|
1102
|
-
const all =
|
|
1174
|
+
const all = flatten3(result);
|
|
1103
1175
|
const seen = new Map;
|
|
1104
1176
|
for (const entry of [
|
|
1105
1177
|
...all.filter((item) => !item.record.name.includes(SEPARATOR)),
|
|
@@ -1123,11 +1195,18 @@ var PRERENDER_WARNING = 'i18n: "/" is prerendered, so detect.redirect "root" nev
|
|
|
1123
1195
|
function setupRoutes(options, hamur) {
|
|
1124
1196
|
const r = createResolver(new URL("../../src/build/routes.ts", import.meta.url).href);
|
|
1125
1197
|
let table = { copies: [], routes: {} };
|
|
1198
|
+
let made = new Set;
|
|
1126
1199
|
extendPages((routes) => {
|
|
1200
|
+
const originals = [...routes];
|
|
1127
1201
|
const localized = localizeRoutes(routes, options, hamur.options.rootDir);
|
|
1128
1202
|
routes.splice(0, routes.length, ...localized.routes);
|
|
1129
1203
|
table = localized.table;
|
|
1130
1204
|
rememberRouteTable(hamur, table);
|
|
1205
|
+
const own = Object.fromEntries(Object.entries(hamur.routeRules).filter(([key]) => !made.has(key)));
|
|
1206
|
+
const access = localeAccessRules(own, originals, table, options.defaultLocale);
|
|
1207
|
+
made = new Set(Object.keys(access));
|
|
1208
|
+
if (made.size)
|
|
1209
|
+
addRouteRules(access);
|
|
1131
1210
|
});
|
|
1132
1211
|
const exports = (values) => Object.entries(values).map(([name, value]) => `export const ${name} = ${JSON.stringify(value) ?? "undefined"};
|
|
1133
1212
|
`).join("");
|
|
@@ -1406,6 +1485,7 @@ export const routes = import.meta.server ? {} : ` + routes + `;
|
|
|
1406
1485
|
throw: false
|
|
1407
1486
|
};
|
|
1408
1487
|
delete probe.outdir;
|
|
1488
|
+
delete probe.minChunkSize;
|
|
1409
1489
|
const result = await Bun.build(probe);
|
|
1410
1490
|
if (!result.success)
|
|
1411
1491
|
return;
|
package/dist/src/index.js
CHANGED
|
@@ -1524,6 +1524,7 @@ import { relative as relative2 } from "node:path";
|
|
|
1524
1524
|
import {
|
|
1525
1525
|
DiagnosticError as DiagnosticError5,
|
|
1526
1526
|
addAppPlugin,
|
|
1527
|
+
addRouteRules,
|
|
1527
1528
|
addRouteMiddleware,
|
|
1528
1529
|
addTemplate as addTemplate2,
|
|
1529
1530
|
createResolver as createResolver2,
|
|
@@ -1817,6 +1818,75 @@ function createMatcher(table) {
|
|
|
1817
1818
|
};
|
|
1818
1819
|
}
|
|
1819
1820
|
|
|
1821
|
+
// packages/i18n/src/build/access.ts
|
|
1822
|
+
import { createRouteRules } from "hamur/kit";
|
|
1823
|
+
var KEYS = ["basicAuth", "ipRestriction"];
|
|
1824
|
+
function accessOf(value) {
|
|
1825
|
+
const access = {};
|
|
1826
|
+
for (const key of KEYS) {
|
|
1827
|
+
const found = Array.isArray(value) ? value.find((entry) => entry[key] !== undefined)?.[key] : value?.[key];
|
|
1828
|
+
if (found !== undefined)
|
|
1829
|
+
Object.assign(access, { [key]: found });
|
|
1830
|
+
}
|
|
1831
|
+
return access;
|
|
1832
|
+
}
|
|
1833
|
+
var prefixed = (code, path) => `/${code}${path === "/" ? "" : path}`;
|
|
1834
|
+
var flatten = (records) => records.flatMap((record) => [record, ...flatten(record.children)]);
|
|
1835
|
+
function localeAccessRules(rules, originals, table, defaultLocale) {
|
|
1836
|
+
const picked = Object.entries(rules).flatMap(([key, value]) => {
|
|
1837
|
+
const access = accessOf(value);
|
|
1838
|
+
return Object.keys(access).length ? [[key, access]] : [];
|
|
1839
|
+
});
|
|
1840
|
+
const out = {};
|
|
1841
|
+
if (!picked.length)
|
|
1842
|
+
return out;
|
|
1843
|
+
const add = (key, access) => {
|
|
1844
|
+
const own = accessOf(rules[key]);
|
|
1845
|
+
for (const name of KEYS)
|
|
1846
|
+
if (access[name] !== undefined && own[name] === undefined && out[key]?.[name] === undefined)
|
|
1847
|
+
Object.assign(out[key] ??= {}, { [name]: access[name] });
|
|
1848
|
+
};
|
|
1849
|
+
const split = (key) => ({ method: key.slice(0, key.indexOf("/")), path: key.slice(key.indexOf("/")) });
|
|
1850
|
+
for (const [key, access] of picked) {
|
|
1851
|
+
const { method, path } = split(key);
|
|
1852
|
+
for (const code of table.copies)
|
|
1853
|
+
add(`${method}${prefixed(code, path)}`, access);
|
|
1854
|
+
}
|
|
1855
|
+
const records = flatten(originals);
|
|
1856
|
+
const own = (path, code, localized) => localized === path || localized === prefixed(code, path);
|
|
1857
|
+
const lists = [table.routes, table.aliases ?? {}];
|
|
1858
|
+
const compiled = createRouteRules(Object.fromEntries(picked));
|
|
1859
|
+
for (const record of records) {
|
|
1860
|
+
const access = accessOf(compiled.match(record.path).rule);
|
|
1861
|
+
if (!Object.keys(access).length)
|
|
1862
|
+
continue;
|
|
1863
|
+
for (const list of lists)
|
|
1864
|
+
for (const [code, localized] of Object.entries(list[record.name] ?? {}))
|
|
1865
|
+
if (!table.missing?.[record.name]?.includes(code) && !own(record.path, code, localized))
|
|
1866
|
+
add(localized, access);
|
|
1867
|
+
}
|
|
1868
|
+
const source = createMatcher({
|
|
1869
|
+
copies: [],
|
|
1870
|
+
routes: Object.fromEntries(records.map((record) => [record.name, { [defaultLocale]: record.path }]))
|
|
1871
|
+
});
|
|
1872
|
+
const builders = lists.map((routes) => createMatcher({ copies: [], routes }));
|
|
1873
|
+
for (const [key, access] of picked) {
|
|
1874
|
+
const { method, path } = split(key);
|
|
1875
|
+
const found = /[:*]/.test(path) ? undefined : source.matchPath(path);
|
|
1876
|
+
if (!found)
|
|
1877
|
+
continue;
|
|
1878
|
+
for (const [index, builder] of builders.entries())
|
|
1879
|
+
for (const code of Object.keys(lists[index][found.name] ?? {})) {
|
|
1880
|
+
if (table.missing?.[found.name]?.includes(code))
|
|
1881
|
+
continue;
|
|
1882
|
+
const localized = builder.buildPath(found.name, code, found.params);
|
|
1883
|
+
if (!own(path, code, localized))
|
|
1884
|
+
add(`${method}${localized}`, access);
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
return out;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1820
1890
|
// packages/i18n/src/build/routes.ts
|
|
1821
1891
|
var SEPARATOR = "___";
|
|
1822
1892
|
function paramsOf2(segments) {
|
|
@@ -1839,12 +1909,12 @@ function shapeOf(path) {
|
|
|
1839
1909
|
});
|
|
1840
1910
|
return `/${parts.join("/")}`;
|
|
1841
1911
|
}
|
|
1842
|
-
function
|
|
1843
|
-
return records.flatMap((record) => [...
|
|
1912
|
+
function flatten2(records, ancestors = []) {
|
|
1913
|
+
return records.flatMap((record) => [...flatten2(record.children, [...ancestors, record.name]), { record, ancestors }]);
|
|
1844
1914
|
}
|
|
1845
1915
|
function tableOf(records, options, copies) {
|
|
1846
1916
|
const table = { copies, routes: {}, aliases: {} };
|
|
1847
|
-
for (const { record } of
|
|
1917
|
+
for (const { record } of flatten2(records)) {
|
|
1848
1918
|
const index = record.name.indexOf(SEPARATOR);
|
|
1849
1919
|
const base = index === -1 ? record.name : record.name.slice(0, index);
|
|
1850
1920
|
const code = index === -1 ? options.defaultLocale : record.name.slice(index + SEPARATOR.length);
|
|
@@ -1857,7 +1927,7 @@ function localizeRoutes(routes, options, rootDir = "") {
|
|
|
1857
1927
|
if (options.strategy === "no_prefix")
|
|
1858
1928
|
return { routes, table: tableOf(routes, options, []) };
|
|
1859
1929
|
const shown = (file) => rootDir ? relative2(rootDir, file) : file;
|
|
1860
|
-
const originals =
|
|
1930
|
+
const originals = flatten2(routes);
|
|
1861
1931
|
for (const { record } of originals) {
|
|
1862
1932
|
if (record.name.includes(SEPARATOR))
|
|
1863
1933
|
throw new DiagnosticError5("HAMUR_E5816", `i18n: route name "${record.name}" (${shown(record.file)}) contains "___", which route copies use`);
|
|
@@ -1918,7 +1988,7 @@ function localizeRoutes(routes, options, rootDir = "") {
|
|
|
1918
1988
|
return true;
|
|
1919
1989
|
if (!differs(record, code))
|
|
1920
1990
|
return false;
|
|
1921
|
-
const paths =
|
|
1991
|
+
const paths = flatten2([copy(record, code)]).map((entry) => entry.record.path).join(`
|
|
1922
1992
|
`);
|
|
1923
1993
|
let seen = made.get(record);
|
|
1924
1994
|
if (!seen)
|
|
@@ -1949,7 +2019,7 @@ function localizeRoutes(routes, options, rootDir = "") {
|
|
|
1949
2019
|
}
|
|
1950
2020
|
}
|
|
1951
2021
|
}
|
|
1952
|
-
const all =
|
|
2022
|
+
const all = flatten2(result);
|
|
1953
2023
|
const seen = new Map;
|
|
1954
2024
|
for (const entry of [
|
|
1955
2025
|
...all.filter((item) => !item.record.name.includes(SEPARATOR)),
|
|
@@ -1973,11 +2043,18 @@ var PRERENDER_WARNING = 'i18n: "/" is prerendered, so detect.redirect "root" nev
|
|
|
1973
2043
|
function setupRoutes(options, hamur) {
|
|
1974
2044
|
const r = createResolver2(new URL("../../src/build/routes.ts", import.meta.url).href);
|
|
1975
2045
|
let table = { copies: [], routes: {} };
|
|
2046
|
+
let made = new Set;
|
|
1976
2047
|
extendPages((routes) => {
|
|
2048
|
+
const originals = [...routes];
|
|
1977
2049
|
const localized = localizeRoutes(routes, options, hamur.options.rootDir);
|
|
1978
2050
|
routes.splice(0, routes.length, ...localized.routes);
|
|
1979
2051
|
table = localized.table;
|
|
1980
2052
|
rememberRouteTable(hamur, table);
|
|
2053
|
+
const own = Object.fromEntries(Object.entries(hamur.routeRules).filter(([key]) => !made.has(key)));
|
|
2054
|
+
const access = localeAccessRules(own, originals, table, options.defaultLocale);
|
|
2055
|
+
made = new Set(Object.keys(access));
|
|
2056
|
+
if (made.size)
|
|
2057
|
+
addRouteRules(access);
|
|
1981
2058
|
});
|
|
1982
2059
|
const exports = (values) => Object.entries(values).map(([name, value]) => `export const ${name} = ${JSON.stringify(value) ?? "undefined"};
|
|
1983
2060
|
`).join("");
|
|
@@ -2172,15 +2249,15 @@ function createSplitReader(roots) {
|
|
|
2172
2249
|
}
|
|
2173
2250
|
};
|
|
2174
2251
|
}
|
|
2175
|
-
function
|
|
2252
|
+
function flatten3(records, ancestors = []) {
|
|
2176
2253
|
return records.flatMap((record) => [
|
|
2177
2254
|
{ record, chain: [...ancestors, record] },
|
|
2178
|
-
...
|
|
2255
|
+
...flatten3(record.children, [...ancestors, record])
|
|
2179
2256
|
]);
|
|
2180
2257
|
}
|
|
2181
2258
|
var slotFiles = (record) => Object.values(record.slots ?? {}).flatMap((slot) => [
|
|
2182
2259
|
...slot.default ? [slot.default] : [],
|
|
2183
|
-
...
|
|
2260
|
+
...flatten3(slot.routes).map((entry) => entry.record.file)
|
|
2184
2261
|
]);
|
|
2185
2262
|
var filesOf = (chain) => chain.flatMap((record) => [
|
|
2186
2263
|
record.file,
|
|
@@ -2208,7 +2285,7 @@ async function planSplit(hamur) {
|
|
|
2208
2285
|
];
|
|
2209
2286
|
const shared = await reader.read(sharedFiles);
|
|
2210
2287
|
const routes = new Map;
|
|
2211
|
-
for (const { record, chain } of
|
|
2288
|
+
for (const { record, chain } of flatten3(hamur.routes)) {
|
|
2212
2289
|
const base = baseOf(record.name);
|
|
2213
2290
|
if (routes.has(base))
|
|
2214
2291
|
continue;
|
|
@@ -2482,6 +2559,7 @@ export const routes = import.meta.server ? {} : ` + routes + `;
|
|
|
2482
2559
|
throw: false
|
|
2483
2560
|
};
|
|
2484
2561
|
delete probe.outdir;
|
|
2562
|
+
delete probe.minChunkSize;
|
|
2485
2563
|
const result = await Bun.build(probe);
|
|
2486
2564
|
if (!result.success)
|
|
2487
2565
|
return;
|
|
@@ -2,6 +2,7 @@ import { type PipeTransform } from "@angular/core";
|
|
|
2
2
|
import type { Signal } from "@angular/core";
|
|
3
3
|
import type { CurrentLocale, I18nArgsTuple, I18nKey } from "../types.ts";
|
|
4
4
|
import { type I18nFunctions } from "./bind.ts";
|
|
5
|
+
import * as i0 from "@angular/core";
|
|
5
6
|
export type { CurrentLocale };
|
|
6
7
|
export declare function injectLocale(): Signal<CurrentLocale>;
|
|
7
8
|
export declare function injectI18n(): I18nFunctions<string> & {
|
|
@@ -14,4 +15,6 @@ export declare function injectI18n(): I18nFunctions<string> & {
|
|
|
14
15
|
export declare class TPipe implements PipeTransform {
|
|
15
16
|
private readonly translate;
|
|
16
17
|
transform<K extends I18nKey>(key: K, ...args: I18nArgsTuple<K>): string;
|
|
18
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TPipe, never>;
|
|
19
|
+
static ɵpipe: i0.ɵɵPipeDeclaration<TPipe, "t", true>;
|
|
17
20
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type HamurContext, type RouteRecord, type RouteRule } from "hamur/kit";
|
|
2
|
+
import { type RouteTable } from "../runtime/match.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The `basicAuth` and `ipRestriction` rules of each locale's address of a page, from the rules of its own path: a rule on `/admin`
|
|
5
|
+
* also holds for `/de/admin`, the default locale's `/en/admin` and a path of `i18n.pages` such as `/de/verwaltung`. A key that
|
|
6
|
+
* sets the rule itself keeps its own.
|
|
7
|
+
*/
|
|
8
|
+
export declare function localeAccessRules(rules: HamurContext["routeRules"], originals: RouteRecord[], table: RouteTable, defaultLocale: string): Record<string, RouteRule>;
|
|
@@ -158,6 +158,6 @@ declare module "hamur" {
|
|
|
158
158
|
declare module "hamur/app" {
|
|
159
159
|
interface HamurLinkProps {
|
|
160
160
|
/** The locale of the link's path, the page's by default; `false` keeps `to` as written. */
|
|
161
|
-
locale?: HamurLocale | false;
|
|
161
|
+
locale?: import("@hamur/i18n").HamurLocale | false;
|
|
162
162
|
}
|
|
163
163
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/i18n",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -11,47 +11,80 @@
|
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
13
|
"types": "./dist/types/src/index.d.ts",
|
|
14
|
-
"
|
|
14
|
+
"bun": "./src/index.ts",
|
|
15
|
+
"hamur-source": "./src/index.ts",
|
|
16
|
+
"node": "./dist/src/index.js",
|
|
17
|
+
"default": "./src/index.ts"
|
|
15
18
|
},
|
|
16
19
|
"./server": {
|
|
17
20
|
"types": "./dist/types/src/server/index.d.ts",
|
|
18
|
-
"
|
|
21
|
+
"bun": "./src/server/index.ts",
|
|
22
|
+
"hamur-source": "./src/server/index.ts",
|
|
23
|
+
"node": "./dist/src/server/index.js",
|
|
24
|
+
"default": "./src/server/index.ts"
|
|
19
25
|
},
|
|
20
26
|
"./load": {
|
|
21
27
|
"types": "./dist/types/src/load.d.ts",
|
|
22
|
-
"
|
|
28
|
+
"bun": "./src/load.ts",
|
|
29
|
+
"hamur-source": "./src/load.ts",
|
|
30
|
+
"node": "./dist/src/load.js",
|
|
31
|
+
"default": "./src/load.ts"
|
|
23
32
|
},
|
|
24
33
|
"./cli": {
|
|
25
34
|
"types": "./dist/types/src/cli.d.ts",
|
|
26
|
-
"
|
|
35
|
+
"bun": "./src/cli.ts",
|
|
36
|
+
"hamur-source": "./src/cli.ts",
|
|
37
|
+
"node": "./dist/src/cli.js",
|
|
38
|
+
"default": "./src/cli.ts"
|
|
27
39
|
},
|
|
28
40
|
"./react": {
|
|
29
41
|
"types": "./dist/types/src/adapters/react.d.ts",
|
|
30
|
-
"
|
|
42
|
+
"bun": "./src/adapters/react.ts",
|
|
43
|
+
"hamur-source": "./src/adapters/react.ts",
|
|
44
|
+
"node": "./dist/src/adapters/react.js",
|
|
45
|
+
"default": "./src/adapters/react.ts"
|
|
31
46
|
},
|
|
32
47
|
"./vue": {
|
|
33
48
|
"types": "./dist/types/src/adapters/vue.d.ts",
|
|
34
|
-
"
|
|
49
|
+
"bun": "./src/adapters/vue.ts",
|
|
50
|
+
"hamur-source": "./src/adapters/vue.ts",
|
|
51
|
+
"node": "./dist/src/adapters/vue.js",
|
|
52
|
+
"default": "./src/adapters/vue.ts"
|
|
35
53
|
},
|
|
36
54
|
"./preact": {
|
|
37
55
|
"types": "./dist/types/src/adapters/preact.d.ts",
|
|
38
|
-
"
|
|
56
|
+
"bun": "./src/adapters/preact.ts",
|
|
57
|
+
"hamur-source": "./src/adapters/preact.ts",
|
|
58
|
+
"node": "./dist/src/adapters/preact.js",
|
|
59
|
+
"default": "./src/adapters/preact.ts"
|
|
39
60
|
},
|
|
40
61
|
"./svelte": {
|
|
41
62
|
"types": "./dist/types/src/adapters/svelte.d.ts",
|
|
42
|
-
"
|
|
63
|
+
"bun": "./src/adapters/svelte.ts",
|
|
64
|
+
"hamur-source": "./src/adapters/svelte.ts",
|
|
65
|
+
"node": "./dist/src/adapters/svelte.js",
|
|
66
|
+
"default": "./src/adapters/svelte.ts"
|
|
43
67
|
},
|
|
44
68
|
"./solid": {
|
|
45
69
|
"types": "./dist/types/src/adapters/solid.d.ts",
|
|
46
|
-
"
|
|
70
|
+
"bun": "./src/adapters/solid.ts",
|
|
71
|
+
"hamur-source": "./src/adapters/solid.ts",
|
|
72
|
+
"node": "./dist/src/adapters/solid.js",
|
|
73
|
+
"default": "./src/adapters/solid.ts"
|
|
47
74
|
},
|
|
48
75
|
"./html": {
|
|
49
76
|
"types": "./dist/types/src/adapters/html.d.ts",
|
|
50
|
-
"
|
|
77
|
+
"bun": "./src/adapters/html.ts",
|
|
78
|
+
"hamur-source": "./src/adapters/html.ts",
|
|
79
|
+
"node": "./dist/src/adapters/html.js",
|
|
80
|
+
"default": "./src/adapters/html.ts"
|
|
51
81
|
},
|
|
52
82
|
"./angular": {
|
|
53
83
|
"types": "./dist/types/src/adapters/angular.d.ts",
|
|
54
|
-
"
|
|
84
|
+
"bun": "./src/adapters/angular.ts",
|
|
85
|
+
"hamur-source": "./src/adapters/angular.ts",
|
|
86
|
+
"node": "./dist/src/adapters/angular.js",
|
|
87
|
+
"default": "./src/adapters/angular.ts"
|
|
55
88
|
},
|
|
56
89
|
"./package.json": "./package.json"
|
|
57
90
|
},
|
|
@@ -61,14 +94,14 @@
|
|
|
61
94
|
],
|
|
62
95
|
"dependencies": {},
|
|
63
96
|
"peerDependencies": {
|
|
64
|
-
"hamur": "^0.0.
|
|
65
|
-
"@hamur/adapter-react": "^0.0.
|
|
66
|
-
"@hamur/adapter-vue": "^0.0.
|
|
67
|
-
"@hamur/adapter-preact": "^0.0.
|
|
68
|
-
"@hamur/adapter-svelte": "^0.0.
|
|
69
|
-
"@hamur/adapter-solid": "^0.0.
|
|
70
|
-
"@hamur/adapter-html": "^0.0.
|
|
71
|
-
"@hamur/adapter-angular": "^0.0.
|
|
97
|
+
"hamur": "^0.0.3",
|
|
98
|
+
"@hamur/adapter-react": "^0.0.3",
|
|
99
|
+
"@hamur/adapter-vue": "^0.0.3",
|
|
100
|
+
"@hamur/adapter-preact": "^0.0.3",
|
|
101
|
+
"@hamur/adapter-svelte": "^0.0.3",
|
|
102
|
+
"@hamur/adapter-solid": "^0.0.3",
|
|
103
|
+
"@hamur/adapter-html": "^0.0.3",
|
|
104
|
+
"@hamur/adapter-angular": "^0.0.3",
|
|
72
105
|
"react": "^19.3.1",
|
|
73
106
|
"preact": "^11.0.2",
|
|
74
107
|
"vue": "^3.5.44",
|
package/src/adapters/angular.ts
CHANGED
|
@@ -1,65 +1,58 @@
|
|
|
1
|
-
import { Pipe, assertInInjectionContext, computed, signal
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export type { CurrentLocale }
|
|
8
|
-
|
|
9
|
-
const ESCAPES: Record<string, string> = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }
|
|
10
|
-
|
|
11
|
-
const escapeHtml = (text: string): string => text.replace(/[&<>"']/g, (char) => ESCAPES[char]!)
|
|
12
|
-
|
|
1
|
+
import { Pipe, assertInInjectionContext, computed, signal } from "@angular/core";
|
|
2
|
+
import { injectApp, injectRoute } from "@hamur/adapter-angular";
|
|
3
|
+
import { bindFunctions, currentLocale, i18nOf, onSetLocale } from "./bind.ts";
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
const ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
6
|
+
const escapeHtml = (text) => text.replace(/[&<>"']/g, (char) => ESCAPES[char]);
|
|
13
7
|
/** `t.rich` gives HTML for `[innerHTML]`: the message text escaped, each handler's string as written. */
|
|
14
|
-
const join
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return holder
|
|
8
|
+
const join = (parts) => parts.map((part) => (typeof part === "string" ? escapeHtml(part) : part.node)).join("");
|
|
9
|
+
const holders = new WeakMap();
|
|
10
|
+
function holderOf(i18n) {
|
|
11
|
+
let holder = holders.get(i18n);
|
|
12
|
+
if (!holder) {
|
|
13
|
+
const created = signal(i18n.locale, /* @ts-ignore */
|
|
14
|
+
...(ngDevMode ? [{ debugName: "created" }] : /* istanbul ignore next */ []));
|
|
15
|
+
holders.set(i18n, (holder = created));
|
|
16
|
+
onSetLocale(i18n, () => created.set(i18n.locale));
|
|
17
|
+
}
|
|
18
|
+
return holder;
|
|
27
19
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
})
|
|
20
|
+
export function injectLocale() {
|
|
21
|
+
assertInInjectionContext(injectLocale);
|
|
22
|
+
const i18n = i18nOf(injectApp());
|
|
23
|
+
const route = injectRoute();
|
|
24
|
+
const holder = holderOf(i18n);
|
|
25
|
+
return computed(() => {
|
|
26
|
+
holder();
|
|
27
|
+
return currentLocale(i18n, route().path);
|
|
28
|
+
});
|
|
38
29
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
}
|
|
30
|
+
export function injectI18n() {
|
|
31
|
+
assertInInjectionContext(injectI18n);
|
|
32
|
+
const i18n = i18nOf(injectApp());
|
|
33
|
+
const route = injectRoute();
|
|
34
|
+
const holder = holderOf(i18n);
|
|
35
|
+
return {
|
|
36
|
+
locale: injectLocale(),
|
|
37
|
+
...bindFunctions(() => {
|
|
38
|
+
holder();
|
|
39
|
+
return { i18n, route: route() };
|
|
40
|
+
}, join),
|
|
41
|
+
};
|
|
52
42
|
}
|
|
53
|
-
|
|
54
43
|
/**
|
|
55
44
|
* `{{ "home.title" | t: { name } }}`. Not `pure`: Angular would call a pure pipe again only when the key or the arguments
|
|
56
45
|
* object change, never when the locale does or a field of the arguments changes in place; the pipe translates on every check.
|
|
57
46
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
47
|
+
export class TPipe {
|
|
48
|
+
translate = injectI18n().t;
|
|
49
|
+
transform(key, ...args) {
|
|
50
|
+
return this.translate(key, ...args);
|
|
51
|
+
}
|
|
52
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: TPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
|
|
53
|
+
static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "22.3.2", ngImport: i0, type: TPipe, isStandalone: true, name: "t", pure: false });
|
|
65
54
|
}
|
|
55
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: TPipe, decorators: [{
|
|
56
|
+
type: Pipe,
|
|
57
|
+
args: [{ name: "t", pure: false }]
|
|
58
|
+
}] });
|
|
@@ -14,6 +14,10 @@ const container = {
|
|
|
14
14
|
childNodes: [],
|
|
15
15
|
}
|
|
16
16
|
const settle = () => new Promise((resolve) => setTimeout(resolve, 30))
|
|
17
|
+
const rendered = async (count: number) => {
|
|
18
|
+
for (const end = Date.now() + 5000; texts.length < count && Date.now() < end;) await settle()
|
|
19
|
+
await settle()
|
|
20
|
+
}
|
|
17
21
|
const context = (route: typeof routes.fr, runtime: unknown = i18n) =>
|
|
18
22
|
({
|
|
19
23
|
url: new URL(`http://localhost${route.path}`),
|
|
@@ -31,13 +35,13 @@ if (process.argv[2] === "set-locale") {
|
|
|
31
35
|
async () => void (await update(context(routes.about, runtime))),
|
|
32
36
|
)
|
|
33
37
|
await mount(context(routes.about, runtime))
|
|
34
|
-
await
|
|
38
|
+
await rendered(1)
|
|
35
39
|
await runtime.setLocale("fr")
|
|
36
|
-
await
|
|
40
|
+
await rendered(2)
|
|
37
41
|
} else {
|
|
38
42
|
await mount(context(routes.fr))
|
|
39
|
-
await
|
|
43
|
+
await rendered(1)
|
|
40
44
|
await update(context(routes.home))
|
|
41
|
-
await
|
|
45
|
+
await rendered(2)
|
|
42
46
|
}
|
|
43
47
|
console.log(JSON.stringify({ mounts: mounts.length, texts }))
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createRouteRules, type HamurContext, type RouteRecord, type RouteRule } from "hamur/kit"
|
|
2
|
+
import { createMatcher, type RouteTable } from "../runtime/match.ts"
|
|
3
|
+
|
|
4
|
+
const KEYS = ["basicAuth", "ipRestriction"] as const
|
|
5
|
+
|
|
6
|
+
type Access = Pick<RouteRule, (typeof KEYS)[number]>
|
|
7
|
+
|
|
8
|
+
function accessOf(value: HamurContext["routeRules"][string] | undefined): Access {
|
|
9
|
+
const access: Access = {}
|
|
10
|
+
for (const key of KEYS) {
|
|
11
|
+
const found = Array.isArray(value) ? value.find((entry) => entry[key] !== undefined)?.[key] : value?.[key]
|
|
12
|
+
if (found !== undefined) Object.assign(access, { [key]: found })
|
|
13
|
+
}
|
|
14
|
+
return access
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const prefixed = (code: string, path: string) => `/${code}${path === "/" ? "" : path}`
|
|
18
|
+
|
|
19
|
+
const flatten = (records: RouteRecord[]): RouteRecord[] =>
|
|
20
|
+
records.flatMap((record) => [record, ...flatten(record.children)])
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The `basicAuth` and `ipRestriction` rules of each locale's address of a page, from the rules of its own path: a rule on `/admin`
|
|
24
|
+
* also holds for `/de/admin`, the default locale's `/en/admin` and a path of `i18n.pages` such as `/de/verwaltung`. A key that
|
|
25
|
+
* sets the rule itself keeps its own.
|
|
26
|
+
*/
|
|
27
|
+
export function localeAccessRules(
|
|
28
|
+
rules: HamurContext["routeRules"],
|
|
29
|
+
originals: RouteRecord[],
|
|
30
|
+
table: RouteTable,
|
|
31
|
+
defaultLocale: string,
|
|
32
|
+
): Record<string, RouteRule> {
|
|
33
|
+
const picked = Object.entries(rules).flatMap(([key, value]) => {
|
|
34
|
+
const access = accessOf(value)
|
|
35
|
+
return Object.keys(access).length ? [[key, access] as const] : []
|
|
36
|
+
})
|
|
37
|
+
const out: Record<string, RouteRule> = {}
|
|
38
|
+
if (!picked.length) return out
|
|
39
|
+
const add = (key: string, access: Access) => {
|
|
40
|
+
const own = accessOf(rules[key])
|
|
41
|
+
for (const name of KEYS)
|
|
42
|
+
if (access[name] !== undefined && own[name] === undefined && out[key]?.[name] === undefined)
|
|
43
|
+
Object.assign((out[key] ??= {}), { [name]: access[name] })
|
|
44
|
+
}
|
|
45
|
+
const split = (key: string) => ({ method: key.slice(0, key.indexOf("/")), path: key.slice(key.indexOf("/")) })
|
|
46
|
+
for (const [key, access] of picked) {
|
|
47
|
+
const { method, path } = split(key)
|
|
48
|
+
for (const code of table.copies) add(`${method}${prefixed(code, path)}`, access)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const records = flatten(originals)
|
|
52
|
+
const own = (path: string, code: string, localized: string) =>
|
|
53
|
+
localized === path || localized === prefixed(code, path)
|
|
54
|
+
const lists = [table.routes, table.aliases ?? {}]
|
|
55
|
+
const compiled = createRouteRules(Object.fromEntries(picked))
|
|
56
|
+
for (const record of records) {
|
|
57
|
+
const access = accessOf(compiled.match(record.path).rule)
|
|
58
|
+
if (!Object.keys(access).length) continue
|
|
59
|
+
for (const list of lists)
|
|
60
|
+
for (const [code, localized] of Object.entries(list[record.name] ?? {}))
|
|
61
|
+
if (!table.missing?.[record.name]?.includes(code) && !own(record.path, code, localized)) add(localized, access)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const source = createMatcher({
|
|
65
|
+
copies: [],
|
|
66
|
+
routes: Object.fromEntries(records.map((record) => [record.name, { [defaultLocale]: record.path }])),
|
|
67
|
+
})
|
|
68
|
+
const builders = lists.map((routes) => createMatcher({ copies: [], routes }))
|
|
69
|
+
for (const [key, access] of picked) {
|
|
70
|
+
const { method, path } = split(key)
|
|
71
|
+
const found = /[:*]/.test(path) ? undefined : source.matchPath(path)
|
|
72
|
+
if (!found) continue
|
|
73
|
+
for (const [index, builder] of builders.entries())
|
|
74
|
+
for (const code of Object.keys(lists[index]![found.name] ?? {})) {
|
|
75
|
+
if (table.missing?.[found.name]?.includes(code)) continue
|
|
76
|
+
const localized = builder.buildPath(found.name, code, found.params)
|
|
77
|
+
if (!own(path, code, localized)) add(`${method}${localized}`, access)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return out
|
|
81
|
+
}
|
package/src/build/messages.ts
CHANGED
|
@@ -292,6 +292,7 @@ export const setupMessages: I18nSetup = async (options: ResolvedI18nOptions, ham
|
|
|
292
292
|
throw: false,
|
|
293
293
|
}
|
|
294
294
|
delete probe.outdir
|
|
295
|
+
delete probe.minChunkSize
|
|
295
296
|
const result = await Bun.build(probe)
|
|
296
297
|
if (!result.success) return
|
|
297
298
|
inputs = Object.keys(result.metafile?.inputs ?? {})
|
package/src/build/routes.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { relative } from "node:path"
|
|
|
2
2
|
import {
|
|
3
3
|
DiagnosticError,
|
|
4
4
|
addAppPlugin,
|
|
5
|
+
addRouteRules,
|
|
5
6
|
addRouteMiddleware,
|
|
6
7
|
addTemplate,
|
|
7
8
|
createResolver,
|
|
@@ -13,6 +14,7 @@ import {
|
|
|
13
14
|
} from "hamur/kit"
|
|
14
15
|
import { parsePattern, type RouteTable, type Segment } from "../runtime/match.ts"
|
|
15
16
|
import type { ResolvedI18nOptions } from "../types.ts"
|
|
17
|
+
import { localeAccessRules } from "./access.ts"
|
|
16
18
|
|
|
17
19
|
const SEPARATOR = "___"
|
|
18
20
|
|
|
@@ -215,11 +217,17 @@ const PRERENDER_WARNING = 'i18n: "/" is prerendered, so detect.redirect "root" n
|
|
|
215
217
|
export function setupRoutes(options: ResolvedI18nOptions, hamur: HamurContext): void {
|
|
216
218
|
const r = createResolver(import.meta.url)
|
|
217
219
|
let table: RouteTable = { copies: [], routes: {} }
|
|
220
|
+
let made = new Set<string>()
|
|
218
221
|
extendPages((routes) => {
|
|
222
|
+
const originals = [...routes]
|
|
219
223
|
const localized = localizeRoutes(routes, options, hamur.options.rootDir)
|
|
220
224
|
routes.splice(0, routes.length, ...localized.routes)
|
|
221
225
|
table = localized.table
|
|
222
226
|
rememberRouteTable(hamur, table)
|
|
227
|
+
const own = Object.fromEntries(Object.entries(hamur.routeRules).filter(([key]) => !made.has(key)))
|
|
228
|
+
const access = localeAccessRules(own, originals, table, options.defaultLocale)
|
|
229
|
+
made = new Set(Object.keys(access))
|
|
230
|
+
if (made.size) addRouteRules(access)
|
|
223
231
|
})
|
|
224
232
|
const exports = (values: Record<string, unknown>) =>
|
|
225
233
|
Object.entries(values)
|
package/src/types.ts
CHANGED
|
@@ -176,7 +176,8 @@ declare module "hamur" {
|
|
|
176
176
|
|
|
177
177
|
declare module "hamur/app" {
|
|
178
178
|
interface HamurLinkProps {
|
|
179
|
+
// By the package's name: an installed app's program has this file and its declaration, which must declare one type.
|
|
179
180
|
/** The locale of the link's path, the page's by default; `false` keeps `to` as written. */
|
|
180
|
-
locale?: HamurLocale | false
|
|
181
|
+
locale?: import("@hamur/i18n").HamurLocale | false
|
|
181
182
|
}
|
|
182
183
|
}
|