lightnet 2.16.0 → 2.16.2
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/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/i18n/translate.ts +7 -43
- package/src/i18n/translations.ts +69 -0
- package/src/i18n/translation-key.ts +0 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# lightnet
|
|
2
2
|
|
|
3
|
+
## 2.16.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#215](https://github.com/LightNetDev/LightNet/pull/215) [`6d963a7`](https://github.com/LightNetDev/LightNet/commit/6d963a76df071771bf96a616a4a45103fc068c98) Thanks [@smn-cds](https://github.com/smn-cds)! - Makes translation files imports more static
|
|
8
|
+
|
|
9
|
+
## 2.16.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#213](https://github.com/LightNetDev/LightNet/pull/213) [`d78e4fd`](https://github.com/LightNetDev/LightNet/commit/d78e4fd0c60cef710f57ede45c204ad839ba7f0f) Thanks [@smn-cds](https://github.com/smn-cds)! - Fix loading of built in languages.
|
|
14
|
+
|
|
3
15
|
## 2.16.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/package.json
CHANGED
package/src/i18n/translate.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { AstroError } from "astro/errors"
|
|
2
2
|
import i18next, { type TOptions } from "i18next"
|
|
3
3
|
import config from "virtual:lightnet/config"
|
|
4
|
-
import YAML from "yaml"
|
|
5
4
|
|
|
6
5
|
import { resolveDefaultLocale } from "./resolve-default-locale"
|
|
7
6
|
import { resolveLanguage } from "./resolve-language"
|
|
8
|
-
import type
|
|
9
|
-
|
|
10
|
-
type TranslationsByLocales = Record<string, Record<string, string>>
|
|
7
|
+
import { type LightNetTranslationKey, loadTranslations } from "./translations"
|
|
11
8
|
|
|
12
9
|
// We add (string & NonNullable<unknown>) to preserve typescript autocompletion for known keys
|
|
13
10
|
export type TranslationKey =
|
|
@@ -18,25 +15,20 @@ export type TranslateFn = (key: TranslationKey, options?: TOptions) => string
|
|
|
18
15
|
|
|
19
16
|
const languageCodes = [
|
|
20
17
|
...new Set(
|
|
21
|
-
config.languages
|
|
22
|
-
lng.
|
|
23
|
-
...lng.fallbackLanguages,
|
|
24
|
-
"en",
|
|
25
|
-
]),
|
|
18
|
+
config.languages
|
|
19
|
+
.filter((lng) => lng.isUILanguage)
|
|
20
|
+
.flatMap((lng) => [lng.code, ...lng.fallbackLanguages, "en"]),
|
|
26
21
|
),
|
|
27
22
|
]
|
|
28
23
|
const defaultLocale = resolveDefaultLocale(config)
|
|
29
24
|
|
|
30
|
-
const builtInTranslations = await loadTranslations("/i18n/translations")
|
|
31
|
-
const userTranslations = await loadTranslations("/src/translations")
|
|
32
|
-
|
|
33
25
|
await i18next.init({
|
|
34
26
|
lng: defaultLocale,
|
|
35
27
|
// don't use name spacing
|
|
36
28
|
nsSeparator: false,
|
|
37
29
|
// only use flat keys
|
|
38
30
|
keySeparator: false,
|
|
39
|
-
resources: prepareI18nextTranslations(),
|
|
31
|
+
resources: await prepareI18nextTranslations(),
|
|
40
32
|
})
|
|
41
33
|
|
|
42
34
|
export function useTranslate(bcp47: string | undefined): TranslateFn {
|
|
@@ -62,39 +54,11 @@ export function useTranslate(bcp47: string | undefined): TranslateFn {
|
|
|
62
54
|
}
|
|
63
55
|
}
|
|
64
56
|
|
|
65
|
-
async function
|
|
66
|
-
const translations: TranslationsByLocales = {}
|
|
67
|
-
const imports = Object.entries(
|
|
68
|
-
import.meta.glob(
|
|
69
|
-
["./translations/*.(yml|yaml)", "/src/translations/*.(yml|yaml)"],
|
|
70
|
-
{
|
|
71
|
-
query: "?raw",
|
|
72
|
-
import: "default",
|
|
73
|
-
},
|
|
74
|
-
),
|
|
75
|
-
)
|
|
76
|
-
const addTranslation = async (bcp47: string) => {
|
|
77
|
-
const translationImport = imports.find(([importPath]) =>
|
|
78
|
-
importPath.includes(`${path}/${bcp47}.`),
|
|
79
|
-
)?.[1]
|
|
80
|
-
if (!translationImport) {
|
|
81
|
-
return
|
|
82
|
-
}
|
|
83
|
-
const translationsYml = (await translationImport()) as string
|
|
84
|
-
translations[bcp47] = YAML.parse(translationsYml)
|
|
85
|
-
}
|
|
86
|
-
await Promise.all(languageCodes.map((lng) => addTranslation(lng)))
|
|
87
|
-
return translations
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function prepareI18nextTranslations() {
|
|
57
|
+
async function prepareI18nextTranslations() {
|
|
91
58
|
const result: Record<string, { translation: Record<string, string> }> = {}
|
|
92
59
|
for (const bcp47 of languageCodes) {
|
|
93
60
|
result[bcp47] = {
|
|
94
|
-
translation:
|
|
95
|
-
...builtInTranslations[bcp47],
|
|
96
|
-
...userTranslations[bcp47],
|
|
97
|
-
},
|
|
61
|
+
translation: await loadTranslations(bcp47),
|
|
98
62
|
}
|
|
99
63
|
}
|
|
100
64
|
return result
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import YAML from "yaml"
|
|
2
|
+
|
|
3
|
+
const builtInTranslations = {
|
|
4
|
+
en: () => import("./translations/en.yml?raw"),
|
|
5
|
+
de: () => import("./translations/de.yml?raw"),
|
|
6
|
+
} as const
|
|
7
|
+
|
|
8
|
+
type BuiltInLanguage = keyof typeof builtInTranslations
|
|
9
|
+
|
|
10
|
+
const userTranslations = Object.fromEntries(
|
|
11
|
+
Object.entries(
|
|
12
|
+
import.meta.glob(["/src/translations/*.(yml|yaml)"], {
|
|
13
|
+
query: "?raw",
|
|
14
|
+
import: "default",
|
|
15
|
+
}),
|
|
16
|
+
).map(([path, translationImport]) => {
|
|
17
|
+
const [fileName] = path.split("/").slice(-1)
|
|
18
|
+
const lang = fileName.replace(/\.ya?ml/, "")
|
|
19
|
+
return [lang, translationImport]
|
|
20
|
+
}),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
export const loadTranslations = async (bcp47: string) => ({
|
|
24
|
+
...(await loadBuiltInTranslations(bcp47)),
|
|
25
|
+
...(await loadUserTranslations(bcp47)),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
function isBuiltInLanguage(bcp47: string): bcp47 is BuiltInLanguage {
|
|
29
|
+
return Object.hasOwn(builtInTranslations, bcp47)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const loadBuiltInTranslations = async (bcp47: string) => {
|
|
33
|
+
if (!isBuiltInLanguage(bcp47)) {
|
|
34
|
+
return {}
|
|
35
|
+
}
|
|
36
|
+
const yml = (await builtInTranslations[bcp47]()).default
|
|
37
|
+
return YAML.parse(yml)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const loadUserTranslations = async (bcp47: string) => {
|
|
41
|
+
if (!userTranslations[bcp47]) {
|
|
42
|
+
return {}
|
|
43
|
+
}
|
|
44
|
+
const yml = (await userTranslations[bcp47]()) as string
|
|
45
|
+
return YAML.parse(yml)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type LightNetTranslationKey =
|
|
49
|
+
| "ln.404.go-to-the-home-page"
|
|
50
|
+
| "ln.404.page-not-found"
|
|
51
|
+
| "ln.category"
|
|
52
|
+
| "ln.language"
|
|
53
|
+
| "ln.external-link"
|
|
54
|
+
| "ln.type_one"
|
|
55
|
+
| "ln.details.open"
|
|
56
|
+
| "ln.details.share"
|
|
57
|
+
| "ln.details.part-of-collection"
|
|
58
|
+
| "ln.details.download"
|
|
59
|
+
| "ln.header.open-main-menu"
|
|
60
|
+
| "ln.header.select-language"
|
|
61
|
+
| "ln.home.title"
|
|
62
|
+
| "ln.search.all-categories"
|
|
63
|
+
| "ln.search.all-languages"
|
|
64
|
+
| "ln.search.all-types"
|
|
65
|
+
| "ln.search.more-results"
|
|
66
|
+
| "ln.search.no-results"
|
|
67
|
+
| "ln.search.placeholder"
|
|
68
|
+
| "ln.search.title"
|
|
69
|
+
| "ln.share.url-copied-to-clipboard"
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export type LightNetTranslationKey =
|
|
2
|
-
| "ln.404.go-to-the-home-page"
|
|
3
|
-
| "ln.404.page-not-found"
|
|
4
|
-
| "ln.category"
|
|
5
|
-
| "ln.language"
|
|
6
|
-
| "ln.external-link"
|
|
7
|
-
| "ln.type_one"
|
|
8
|
-
| "ln.details.open"
|
|
9
|
-
| "ln.details.share"
|
|
10
|
-
| "ln.details.part-of-collection"
|
|
11
|
-
| "ln.details.download"
|
|
12
|
-
| "ln.header.open-main-menu"
|
|
13
|
-
| "ln.header.select-language"
|
|
14
|
-
| "ln.home.title"
|
|
15
|
-
| "ln.search.all-categories"
|
|
16
|
-
| "ln.search.all-languages"
|
|
17
|
-
| "ln.search.all-types"
|
|
18
|
-
| "ln.search.more-results"
|
|
19
|
-
| "ln.search.no-results"
|
|
20
|
-
| "ln.search.placeholder"
|
|
21
|
-
| "ln.search.title"
|
|
22
|
-
| "ln.share.url-copied-to-clipboard"
|