i18nya 0.1.3 → 0.2.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.
@@ -36,13 +36,13 @@ export default defineConfig({
36
36
  });
37
37
  ```
38
38
 
39
- Then for all pages in `src/pages/[lang]/*.astro`:
39
+ Then for all pages in `src/pages/[...lang]/*.astro`:
40
40
 
41
41
  ```ts
42
42
  import i18nya, { makeT } from "../../i18n.ts";
43
43
  import { makeGetStaticPaths } from "astro-i18nya";
44
44
  const t = makeT(Astro.params.lang);
45
- // generate paths only for languages in your `langs/` folder!
45
+ // generate paths only for languages in your `[...langs]/` folder!
46
46
  export const getStaticPaths = makeGetStaticPaths(i18nya);
47
47
  ```
48
48
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-i18nya",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Astro integration for i18nya: i18n as small as a cat's paw",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -7,7 +7,10 @@ import type { I18Nya } from "i18nya";
7
7
  * @returns language name
8
8
  */
9
9
  export const getLangName = (lang: string, displayLang?: string) =>
10
- new Intl.DisplayNames([displayLang ?? lang], { type: "language", style: "narrow" }).of(lang);
10
+ new Intl.DisplayNames([displayLang ?? lang], {
11
+ type: "language",
12
+ style: "narrow",
13
+ }).of(lang);
11
14
 
12
15
  /**
13
16
  * List out available languages.
@@ -29,6 +32,8 @@ export const listLang = <T extends string | number | symbol>(
29
32
  export const makeGetStaticPaths =
30
33
  <T extends string | number | symbol>(i18nya: I18Nya<T>) =>
31
34
  async () =>
32
- Object.keys(i18nya.translations)
33
- .filter((lang) => lang !== i18nya.config.defaultLang)
34
- .map((lang) => ({ params: { lang: lang } }));
35
+ Object.keys(i18nya.translations).map((lang) =>
36
+ lang === i18nya.config.defaultLang
37
+ ? { params: { lang: undefined } }
38
+ : { params: { lang: lang } },
39
+ );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18nya",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "i18n as small as a cat's paw",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -12,7 +12,7 @@ export type I18NyaConfig<T extends I18NyaKey> = {
12
12
 
13
13
  export type I18Nya<T extends I18NyaKey> = {
14
14
  translations: Record<string, Record<T, string>>;
15
- makeT: (lang: string) => (key: T, its?: Interpolations) => string;
15
+ makeT: (lang?: string) => (key: T, its?: Interpolations) => string;
16
16
  config: I18NyaConfig<T>;
17
17
  };
18
18
 
@@ -25,17 +25,21 @@ export const init = async <T extends string | number | symbol = string>(
25
25
  const {
26
26
  langDir,
27
27
  defaultLang: rootLang = "en",
28
- fallbackLangs: fb = {},
28
+ fallbackLangs = {},
29
29
  viteImports = undefined,
30
30
  } = config;
31
31
  let i18nya: I18Nya<T> = {
32
32
  translations: {},
33
33
  makeT:
34
- (l) =>
35
- (k, its = {}) => {
34
+ (lang = rootLang) =>
35
+ (key, its = {}) => {
36
36
  let s: string | undefined;
37
- for (; !(s = i18nya.translations[l]?.[k]); l = fb[l] ?? rootLang)
38
- if (l === rootLang) return String(k);
37
+ for (
38
+ ;
39
+ !(s = i18nya.translations[lang]?.[key]);
40
+ lang = fallbackLangs[lang] ?? rootLang
41
+ )
42
+ if (lang === rootLang) return String(key);
39
43
  for (const [k, v] of Object.entries(its))
40
44
  s = s.replace(`{{${k}}}`, `${v}`);
41
45
  return s;