Skip to content

Commit

Permalink
Added lazy load example and updated types for better experience
Browse files Browse the repository at this point in the history
  • Loading branch information
m10rten committed Aug 25, 2024
1 parent 2b1ba21 commit 57c766f
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-bats-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"i4n": patch
---

Updated types to allow string to be put in loaded and switch
2 changes: 1 addition & 1 deletion examples/i4n-json/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "i4n-node",
"name": "i4n-json",
"version": "1.0.0",
"description": "",
"main": "main.ts",
Expand Down
11 changes: 11 additions & 0 deletions examples/i4n-lazy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `i4n`-lazy

This example demonstrates how to use the `i4n` package with lazy loading.

Run it with:

```bash
npm run start
```

This will start the `main.ts` file, present in this same directory.
6 changes: 6 additions & 0 deletions examples/i4n-lazy/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fr": {
"hello": "Bonjour",
"world": "Terre"
}
}
47 changes: 47 additions & 0 deletions examples/i4n-lazy/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { I4n } from "i4n";

interface LanguageData {
hello: string;
key?: string;
}
type Language = "en" | "nl" | "fr";
type TranslationSet = {
[lang in Language]?: LanguageData;
};

async function loadFrench(): Promise<TranslationSet> {
return await import("./fr.json");
}

export const translations = {
en: {
hello: "Hello World!",
key: "Somehow we do",
},
nl: {
hello: "Hallo Wereld!",
},
} satisfies TranslationSet;

const i4n = new I4n({
translations,
language: "en",
fallbackLanguage: "en",
});

const main = async () => {
console.log(i4n.t("hello")); // "Hello"

console.log("Swapping", i4n.switch("nl"));

console.log(i4n.t("hello"));

console.log(i4n.t("key"));

i4n.lazy({ loader: loadFrench });
await i4n.loaded({ lang: "fr" });
i4n.switch("fr");
console.log(i4n.t("hello"));
};

main();
20 changes: 20 additions & 0 deletions examples/i4n-lazy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "i4n-lazy",
"version": "1.0.0",
"description": "",
"main": "main.ts",
"scripts": {
"start": "tsx main.ts"
},
"keywords": [],
"author": "",
"license": "",
"devDependencies": {
"@types/node": "^22.4.0",
"tsx": "^4.17.0",
"typescript": "^5.5.4"
},
"dependencies": {
"i4n": "^0.5.0"
}
}
Loading

0 comments on commit 57c766f

Please sign in to comment.