2017-07-24 19:58:03 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* i18n hook
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
module.exports = strapi => {
|
|
|
|
|
return {
|
|
|
|
|
/**
|
|
|
|
|
* Default options
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
defaults: {
|
|
|
|
|
i18n: {
|
|
|
|
|
defaultLocale: 'en_US',
|
|
|
|
|
modes: ['query', 'subdomain', 'cookie', 'header', 'url', 'tld'],
|
|
|
|
|
cookieName: 'locale'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the hook
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
initialize: function(cb) {
|
|
|
|
|
if (
|
2017-07-25 17:12:18 +02:00
|
|
|
_.isPlainObject(strapi.config.middlewares.settings.i18n) &&
|
|
|
|
|
!_.isEmpty(strapi.config.middlewares.settings.i18n) &&
|
2017-07-24 19:58:03 +02:00
|
|
|
_.get(strapi.config, 'i18n.enabled') !== false
|
|
|
|
|
) {
|
|
|
|
|
strapi.koaMiddlewares.locale(strapi.app);
|
|
|
|
|
strapi.app.use(
|
|
|
|
|
strapi.koaMiddlewares.convert(
|
|
|
|
|
strapi.koaMiddlewares.i18n(strapi.app, {
|
|
|
|
|
directory: path.resolve(
|
|
|
|
|
strapi.config.appPath,
|
|
|
|
|
strapi.config.paths.config,
|
|
|
|
|
'locales'
|
|
|
|
|
),
|
2017-07-25 17:12:18 +02:00
|
|
|
locales: strapi.config.middlewares.settings.i18n.locales,
|
|
|
|
|
defaultLocale: strapi.config.middlewares.settings.i18n.defaultLocale,
|
|
|
|
|
modes: strapi.config.middlewares.settings.i18n.modes,
|
|
|
|
|
cookieName: strapi.config.middlewares.settings.i18n.cookieName,
|
2017-07-24 19:58:03 +02:00
|
|
|
extension: '.json'
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|