Skip to content

Commit 6922f56

Browse files
committed
feature: example app
1 parent 3bcbd37 commit 6922f56

File tree

7 files changed

+443
-0
lines changed

7 files changed

+443
-0
lines changed

example/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# i18n Example
2+
3+
This directory contains a very simple web application that renders a welcome message
4+
based on messages loaded from JSON files via `fetch`.
5+
6+
The example demonstrates how to bootstrap a SPA after messages have been loaded.
7+
8+
The file [`i18n.js`](./i18n.js) contains a typical setup. It exports a function
9+
`m` which simply delegates to `MessageResolver.m` (in a real world setup also create
10+
a function `mpl` that delegates to `MessageResolver.mpl`). It also exports a
11+
`Promise` which is used to delay the application initialization to ensure, that
12+
all messages have been loaded.

example/i18n.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as i18n from "../weccoframework-i18n.es5.js"
2+
3+
let messageResolver
4+
5+
export const loadingPromise = i18n.MessageResolver.create(new i18n.JsonMessageLoader(i18n.fetchJsonSource("./messages")))
6+
.then(mr => messageResolver = mr)
7+
8+
export const m = (key, ...args) => messageResolver.m(key, ...args)

example/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>i18n example</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="index.js"></script>
11+
</body>
12+
</html>

example/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict"
2+
3+
import { m, loadingPromise } from "./i18n.js"
4+
5+
document.addEventListener("DOMContentLoaded", async () => {
6+
await loadingPromise
7+
8+
const h1 = document.querySelector("#app").appendChild(document.createElement("h1"))
9+
h1.innerText = m("greeting")
10+
})

example/messages/de.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"greeting": "hallo, Welt"
3+
}

example/messages/default.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"greeting": "hello, world"
3+
}

0 commit comments

Comments
 (0)