Permalink
Browse files

Integrated lingua compiler documentation.

  • Loading branch information...
1 parent 85ea043 commit 7c2117b62916a6bc31885eb0c998acbe5299c30a @akoenig committed May 26, 2012
Showing with 41 additions and 8 deletions.
  1. +35 −4 README.md
  2. +4 −2 example/app.js
  3. +1 −1 example/i18n/de-de.json
  4. +1 −1 example/i18n/en.json
View
39 README.md
@@ -63,13 +63,44 @@ Using lingua comes down with four simple steps:
}
```
-3. **Use lingua in your views** - Note that the syntax depends on your template engine. In this example it is: [jqtpl](https://github.com/kof/node-jqtpl) and the request comes from a browser which sends 'en' with the HTTP request header.
+3. a) **Use lingua in your views - Static output** - Note that the syntax depends on your template engine. In this example it is: [ejs](http://embeddedjs.com/) and the request comes from a browser which sends 'en' with the HTTP request header.
```html
- <h1>${lingua.title}</h1> <!-- out: <h1>Hello World</h1> -->
- <p>${lingua.content.description}</h1> <!-- out: <p>A little description.</p> -->
+ <h1><%= lingua.title %></h1> <!-- out: <h1>Hello World</h1> -->
+ <p><%= lingua.content.description %></h1> <!-- out: <p>A little description.</p> -->
```
+3. b) **Use lingua in your views - Dynamic output** - Sometimes it is necessary to handle dynamic data within your express route and to pass it to the template. What if your text i18n resource is able to keep placeholders within a string where you can include your dynamic data? Well, it is possible.
+
+First of all, look at this i18n resource file:
+
+ ```javascript
+ // de.json
+ {
+ "greeting": "Hallo {name}. Dieser Schlüssel {code} wurde für Dich generiert."
+ }
+
+Now it is possible to transfer an object from your route into your template:
+
+ ```javascript
+ app.get('/', function(req, res) {
+ var names = ['Valentina', 'Sarah', 'Thomas', 'Claudia'];
+
+ res.render('index', {
+ person: {
+ name: names[Math.floor(Math.random()*names.length)],
+ code: Math.round(Math.random()*100)
+ }
+ });
+ });
+
+And finally in your template you can use the "lingua compiler" -> "linguac"
+
+ ```html
+ <p><%= linguac(lingua.greeting, person) %></p>
+
+
+
4. **Let the user select a language** - Note that the user's selection is persisted within a cookie. This is an optional step. If you want to let lingua determine the user language from the browser configuration then leave this step out. Anyway, this is a very handy feature for switching the language by a user decision.
```html
@@ -95,4 +126,4 @@ You can find a deployed version of this app [here](http://express-lingua-demo.he
## Author
-Copyright (c) 2011, [André König](http://lochkartenstanzer.de) ([Google+](http://profile.lochkartenstanzer.de)) (andre.koenig -[at]- gmail [*dot*] com)
+Copyright (c) 2012, [André König](http://lochkartenstanzer.de) ([Google+](http://profile.lochkartenstanzer.de)) (andre.koenig -[at]- gmail [*dot*] com)
View
6 example/app.js
@@ -47,10 +47,12 @@ app.configure('development', function () {
// Routes
app.get('/', function(req, res) {
+ var names = ['Valentina', 'Sarah', 'Thomas', 'Claudia'];
+
res.render('index', {
person: {
- name: 'Thomas',
- age: Math.round(Math.random()*100)
+ name: names[Math.floor(Math.random()*names.length)],
+ code: Math.round(Math.random()*100)
}
});
});
View
2 example/i18n/de-de.json
@@ -9,6 +9,6 @@
"headline": "Hallo.",
"subheadline": "Dies ist eine kleine App, welche die Funktionsweise von lingua demonstrieren soll.",
"licence": "MIT-Lizenz",
- "person": "Hallo {name}. Du bist {age} Jahre alt."
+ "person": "Hallo {name}. Dieser Schlüssel: {code} wurde für Dich generiert."
}
}
View
2 example/i18n/en.json
@@ -9,6 +9,6 @@
"headline": "Hello. EN",
"subheadline": "This is a little app which should demonstrate the functionality of lingua.",
"licence": "MIT licence",
- "person": "Hey {name}. You are {age} years old."
+ "person": "Hallo \\{name\\}. This is your generated key: {code}"
}
}

0 comments on commit 7c2117b

Please sign in to comment.