Skip to content

Instantly share code, notes, and snippets.

@JedWatson
Created February 24, 2014 16:00
Show Gist options
  • Save JedWatson/9191081 to your computer and use it in GitHub Desktop.
Save JedWatson/9191081 to your computer and use it in GitHub Desktop.
Example of how to integrate node-i18n with a KeystoneJS app (using yo keystone generated site as a basis) - see https://github.com/mashpie/i18n-node for more docs.
// Simulate config options from your production environment by
// customising the .env file in your project's root folder.
require('dotenv')().load();
// Require keystone and i18n
var keystone = require('keystone'),
i18n= require('i18n');
// Initialise Keystone with your project's configuration.
// See http://keystonejs.com/guide/config for available options
// and documentation.
keystone.init({
'name': 'Your Site',
'brand': 'Your Site',
'less': 'public',
'static': 'public',
'favicon': 'public/favicon.ico',
'views': 'templates/views',
'view engine': 'jade',
'auto update': true,
'session': true,
'auth': true,
'user model': 'User',
'cookie secret': 'your secret'
});
// Load your project's Models
keystone.import('models');
// Setup common locals for your templates. The following are required for the
// bundled templates and layouts. Any runtime locals (that should be set uniquely
// for each request) should be added to ./routes/middleware.js
keystone.set('locals', {
_: require('underscore'),
env: keystone.get('env'),
utils: keystone.utils,
editable: keystone.content.editable
});
// Configure i18n
i18n.configure({
locales:['en', 'de'],
directory: __dirname + '/locales'
});
// Load your project's Routes
keystone.set('routes', require('./routes'));
// Configure the navigation bar in Keystone's Admin UI
keystone.set('nav', {
'posts': ['posts', 'post-categories'],
'galleries': 'galleries',
'enquiries': 'enquiries',
'users': 'users'
});
// Start Keystone to connect to your database and initialise the web server
keystone.start();
var _ = require('underscore'),
keystone = require('keystone'),
i18n = require("i18n"),
middleware = require('./middleware'),
importRoutes = keystone.importer(__dirname);
// Add-in i18n support
keystone.pre('routes', i18n.init);
// Common Middleware
keystone.pre('routes', middleware.initLocals);
keystone.pre('render', middleware.flashMessages);
// Import Route Controllers
var routes = {
views: importRoutes('./views')
};
// Setup Route Bindings
exports = module.exports = function(app) {
// Views
app.get('/', routes.views.index);
app.get('/blog/:category?', routes.views.blog);
app.get('/blog/post/:post', routes.views.post);
app.get('/gallery', routes.views.gallery);
app.all('/contact', routes.views.contact);
}
@talvasconcelos
Copy link

Hi guys, i have this working. I now need a way to change the locale from the DOM. i want the user to have the ability to change to language he seems better.

Also, if i enable cookie, how do i save the user's language choice to the cookie, so that he doesn't need to change it every time?
thanks,
Tiago

Copy link

ghost commented Jun 13, 2017

Dear all
I almost port i18n successfully. However, i am having final problem that how i can use i18n for text in flash-messages (mixins folder).
Hope you can help me

Thank you so much for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment