i18nya 0.1.0 → 0.1.1

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/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # i18nya
2
+
3
+ **i18n as small as a cat's paw** — `i18nya` is a minimal, fast, and easy-to-use internationalization library for Node.js and TypeScript projects.
4
+ Its tiny footprint and simple API make it perfect for projects that want localization without the bloat.
5
+
6
+ ## Features
7
+
8
+ - 🐾 **Tiny**: Less than 100 lines of TypeScript, 0 dependencies.
9
+ - ⏬️ **Flexible fallback**: Supports fallback languages out of the box.
10
+ - 🧩 **Extensible**: Can be easily extended to integrate into other frameworks (e.g. `astro-i18nya`).
11
+ - 🔨 **Typed**: Optional translation key completions for `t()`.
12
+
13
+ ## Installation
14
+
15
+ ```sh
16
+ npm install i18nya
17
+ ```
18
+
19
+ ## Getting Started
20
+
21
+ Project structure:
22
+
23
+ ```
24
+ package.json
25
+ src/
26
+ └╴i18n.ts (or js)
27
+ langs/
28
+ ├╴ja_JP.json
29
+ └╴en.json
30
+ ```
31
+
32
+ ```ts
33
+ // src/i18n.ts
34
+ import { init } from "i18nya";
35
+
36
+ const i18nya = await init({
37
+ langDir: "../langs", // only this is required
38
+ defaultLang: "en",
39
+ fallbackLangs: {
40
+ // all languages fallback to English, specify special fallbacks here
41
+ zh_HK: "zh_TW",
42
+ },
43
+ // if you use vite, you need to import manually using this:
44
+ viteImports: import.meta.glob("../langs/*.json", { eager: true }),
45
+ // otherwise, i18nya will search & import the json files using node modules (which vite does not ship)
46
+ });
47
+
48
+ export default i18nya;
49
+ ```
50
+
51
+ ```json
52
+ // langs/en.json
53
+ {
54
+ "i18nya.description": "{{emoji}} i18n as small as a cat's paw"
55
+ }
56
+ ```
57
+
58
+ ```json
59
+ // langs/ja_JP.json
60
+ {
61
+ "i18nya.description": "{{emoji}} 猫の足くらいちっちゃい国際化ツール"
62
+ }
63
+ ```
64
+
65
+ ```ts
66
+ // usage
67
+ import { makeT } from "i18n";
68
+ const t = makeT("ja_JP");
69
+ console.log(t("i18nya.description", { emoji: "🐾" })); // 🐾 猫の足くらいちっちゃい国際化ツール
70
+ ```
71
+
72
+ ### Typed Keys & Completions
73
+
74
+ The type for the keys can be manually specified:
75
+
76
+ ```ts
77
+ import { init } from "i18nya";
78
+ // requires tsconfig `resolveJsonModule`
79
+ import { defaultTranslations } from "../langs/en.json";
80
+
81
+ const i18nya = init<keyof typeof defaultTranslations>(...);
82
+ ```
83
+
84
+ Your editor will now show the translation keys when invoking `t()`.
@@ -1,15 +1,53 @@
1
1
  # astro-i18nya
2
2
 
3
- To install dependencies:
3
+ I18n support for astro.
4
4
 
5
- ```bash
6
- bun install
5
+ ## Features
6
+
7
+ - Integration with `i18nya` and `astro`
8
+ - A `<Trans />` component that works better than the one in `react-i18next`!
9
+
10
+ ## Installation
11
+
12
+ ```sh
13
+ npx astro add astro-i18nya
14
+ # or
15
+ npm install astro-i18nya
7
16
  ```
8
17
 
9
- To run:
18
+ Make sure you called `init()` from `i18nya` in `src/i18n.ts`:
19
+
20
+ ```ts
21
+ import { init } from 'i18nya';
10
22
 
11
- ```bash
12
- bun run index.ts
23
+ const i18nya = await init({ ... });
24
+ export default i18nya;
13
25
  ```
14
26
 
15
- This project was created using `bun init` in bun v1.3.2. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
27
+ Then in `astro.config.mjs`:
28
+
29
+ ```ts
30
+ import i18nya from "./i18n";
31
+ import astro_i18nya from "astro-i18nya";
32
+
33
+ export default defineConfig({
34
+ integrations: [astro_i18nya(i18nya)],
35
+ // `i18n:` is not needed
36
+ });
37
+ ```
38
+
39
+ ## Trans
40
+
41
+ ```tsx
42
+ <Trans t={t("test", { user: "John" })}>
43
+ <b />
44
+ <a href="https://example.com" />
45
+ </Trans>
46
+ ```
47
+
48
+ With `"test": "Hello <1>{{user}}</1>, welcome to <2><1>my site</1></2>."`, the above element will become:
49
+
50
+ ```html
51
+ <b>Hello John</b>, welcome to <a href="https://example.com"><b>my site</b></a
52
+ >.
53
+ ```
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ presets: [
3
+ ['@babel/preset-env', {targets: {node: 'current'}}],
4
+ '@babel/preset-typescript',
5
+
6
+ ['@babel/preset-react', {runtime: 'automatic'}],
7
+ ],
8
+ };
@@ -0,0 +1,7 @@
1
+ import type {Config} from 'jest';
2
+
3
+ const config: Config = {
4
+ "testEnvironment": "jsdom"
5
+ };
6
+
7
+ export default config;