lightnet 2.16.1 → 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 CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 2.16.1
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "lightnet",
3
3
  "type": "module",
4
4
  "license": "MIT",
5
- "version": "2.16.1",
5
+ "version": "2.16.2",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/LightNetDev/lightnet",
@@ -1,15 +1,13 @@
1
1
  import YAML from "yaml"
2
2
 
3
- const builtInTranslations = ["en", "de"] as const
3
+ const builtInTranslations = {
4
+ en: () => import("./translations/en.yml?raw"),
5
+ de: () => import("./translations/de.yml?raw"),
6
+ } as const
4
7
 
5
- const builtInImports = Object.fromEntries(
6
- builtInTranslations.map((lng) => [
7
- lng,
8
- () => import(`./translations/${lng}.yml?raw`),
9
- ]),
10
- )
8
+ type BuiltInLanguage = keyof typeof builtInTranslations
11
9
 
12
- const userImports = Object.fromEntries(
10
+ const userTranslations = Object.fromEntries(
13
11
  Object.entries(
14
12
  import.meta.glob(["/src/translations/*.(yml|yaml)"], {
15
13
  query: "?raw",
@@ -17,7 +15,7 @@ const userImports = Object.fromEntries(
17
15
  }),
18
16
  ).map(([path, translationImport]) => {
19
17
  const [fileName] = path.split("/").slice(-1)
20
- const lang = fileName.replace(/\.ya?ml/i, "")
18
+ const lang = fileName.replace(/\.ya?ml/, "")
21
19
  return [lang, translationImport]
22
20
  }),
23
21
  )
@@ -27,19 +25,23 @@ export const loadTranslations = async (bcp47: string) => ({
27
25
  ...(await loadUserTranslations(bcp47)),
28
26
  })
29
27
 
28
+ function isBuiltInLanguage(bcp47: string): bcp47 is BuiltInLanguage {
29
+ return Object.hasOwn(builtInTranslations, bcp47)
30
+ }
31
+
30
32
  export const loadBuiltInTranslations = async (bcp47: string) => {
31
- if (!builtInImports[bcp47]) {
33
+ if (!isBuiltInLanguage(bcp47)) {
32
34
  return {}
33
35
  }
34
- const yml = (await builtInImports[bcp47]()).default
36
+ const yml = (await builtInTranslations[bcp47]()).default
35
37
  return YAML.parse(yml)
36
38
  }
37
39
 
38
40
  export const loadUserTranslations = async (bcp47: string) => {
39
- if (!userImports[bcp47]) {
41
+ if (!userTranslations[bcp47]) {
40
42
  return {}
41
43
  }
42
- const yml = (await userImports[bcp47]()) as string
44
+ const yml = (await userTranslations[bcp47]()) as string
43
45
  return YAML.parse(yml)
44
46
  }
45
47