Skip to content
This repository was archived by the owner on May 6, 2019. It is now read-only.

Commit acce257

Browse files
author
Brian R. Bondy
committed
Issue #246 - Get l10n started/working
1 parent 6bf794b commit acce257

File tree

6 files changed

+310
-30
lines changed

6 files changed

+310
-30
lines changed

app.js

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const HOUR = 3600000,
44
DAY = 24 * HOUR;
55

66
let express = require('express'),
7+
I18n = require('i18n-2'),
78
async = require('async'),
89
routes = require('./routes'),
910
stylus = require('stylus'),
@@ -20,6 +21,22 @@ appController.initPromise().done(function() {
2021
let config = configController.config;
2122
configController.print();
2223

24+
app.configure(function() {
25+
// Attach the i18n property to the express request object
26+
// And attach helper methods for use in templates
27+
I18n.expressBind(app, {
28+
// setup some locales - other locales default to en silently
29+
locales: ['en', 'fr'],
30+
// change the cookie name from 'lang' to 'locale'
31+
cookieName: 'locale'
32+
});
33+
34+
app.use(function(req, res, next) {
35+
req.i18n.setLocaleFromCookie();
36+
next();
37+
});
38+
});
39+
2340
app.set('views', __dirname + '/views');
2441
app.set('view engine', 'jade');
2542
app.set('view options', { layout: false, pretty: true });

controllers/lessonController.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

33
let redisController = require('../controllers/redisController.js'),
4+
_ = require('underscore'),
5+
I18n = require('i18n-2'),
46
Promise = require('promise');
57

68
/**
@@ -15,6 +17,7 @@ function loadIntoDB(callback) {
1517
*/
1618
function loadFromDB(callback) {
1719
redisController.getAllPromise("category").done(function onSuccess(cat) {
20+
l18n(cat);
1821
exports.categories = cat;
1922
exports.categories.sort(redisController.sortByPriority);
2023
exports._categoriesByTag = [];
@@ -24,6 +27,30 @@ function loadFromDB(callback) {
2427
});
2528
};
2629

30+
function l18n(categories) {
31+
let i18n = new I18n( {
32+
// setup some locales - other locales default to en silently
33+
locales: ['en', 'fr'],
34+
// change the cookie name from 'lang' to 'locale'
35+
cookieName: 'locale'
36+
});//TODO find a better place for this
37+
38+
_.each(categories, function(c) {
39+
i18n.__(c.title);
40+
i18n.__(c.description);
41+
_.each(c.videos, function(v) {
42+
i18n.__(v.title);
43+
i18n.__(v.description);
44+
_.each(v.assertions, function(a) {
45+
i18n.__(a.title);
46+
_.each(a.hints, function(h) {
47+
i18n.__(h);
48+
});
49+
});
50+
});
51+
});
52+
}
53+
2754

2855
/**
2956
* Loads in overall lesson stats into exports.stats

0 commit comments

Comments
 (0)