2012-12-04 12:12:58 +01:00
|
|
|
var languages = require('languages4translatewiki')
|
2012-11-10 14:12:17 +01:00
|
|
|
, fs = require('fs')
|
|
|
|
, path = require('path')
|
2012-11-14 17:01:59 +01:00
|
|
|
, express = require('express')
|
2012-12-19 16:31:17 +01:00
|
|
|
, _ = require('underscore')
|
|
|
|
, npm = require('npm')
|
|
|
|
;
|
2012-11-10 14:12:17 +01:00
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
/*
|
|
|
|
* PRIVATE
|
|
|
|
*/
|
2012-11-14 17:01:59 +01:00
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
// locales will store all locales ini files merged (core+plugins) in RAM
|
|
|
|
var locales = {};
|
2012-11-17 14:33:01 +01:00
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
//explore recursive subdirectories from root and execute callback
|
|
|
|
//don't explore symbolic links
|
|
|
|
var exploreDir = function (root, callback) {
|
|
|
|
var stat = fs.lstatSync(root);
|
|
|
|
if (stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
|
|
var names = fs.readdirSync(root),
|
|
|
|
subdirs = [],
|
|
|
|
files = [];
|
|
|
|
names.forEach (function(file) {
|
|
|
|
file = path.resolve(root,file);
|
|
|
|
stat = fs.lstatSync(file);
|
|
|
|
if (stat.isDirectory() && !stat.isSymbolicLink()) {
|
|
|
|
subdirs.push(file);
|
|
|
|
} else {
|
|
|
|
files.push(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
callback(root, subdirs, files);
|
|
|
|
subdirs.forEach(function (d) {
|
|
|
|
exploreDir(d, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2012-11-28 23:54:23 +01:00
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
// return all files languages absolute path group by langcode
|
|
|
|
// {es: [pathcore, pathplugin1...], en:...}
|
|
|
|
var getAllLocalesPaths = function () {
|
|
|
|
var result = {};
|
|
|
|
|
|
|
|
//extract only files paths with name is a supported language code and have json extension
|
|
|
|
var extractLangs = function (root, subdirs, files) {
|
|
|
|
_.each(files, function(file) {
|
|
|
|
var ext = path.extname(file),
|
|
|
|
locale = path.basename(file, ext).toLowerCase();
|
|
|
|
if ((ext == '.json') && languages.isValid(locale)) {
|
|
|
|
if (!(_.has(result, locale))) result[locale] = [];
|
|
|
|
result[locale].push(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//add core supported languages first
|
2012-12-19 16:31:17 +01:00
|
|
|
exploreDir (npm.root+"/ep_etherpad-lite/locales", extractLangs);
|
|
|
|
|
|
|
|
//add plugins languages (if any) -- bad practice
|
|
|
|
exploreDir (npm.root, extractLangs);
|
2012-12-17 18:18:07 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//save in locales all json files merged by language code
|
|
|
|
var getAllLocales = function () {
|
|
|
|
_.each (getAllLocalesPaths(), function(files, langcode) {
|
|
|
|
locales[langcode]={}
|
|
|
|
_.each (files, function(file) {
|
|
|
|
_.extend(locales[langcode], JSON.parse(fs.readFileSync(file,'utf8'))[langcode]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//return all languages availables with nativeName and direction
|
|
|
|
//{es: {nativeName: "español", direction: "ltr"},...}
|
|
|
|
var getAvailableLangs = function () {
|
|
|
|
var result = {};
|
|
|
|
if (_.isEmpty(locales)) getAllLocales();
|
|
|
|
_.each(_.keys(locales), function(langcode) {
|
|
|
|
result[langcode]=languages.getLanguageInfo(langcode);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//return locale index that will be served in /locales.json
|
|
|
|
var generateLocaleIndex = function () {
|
|
|
|
if (_.isEmpty(locales)) getAllLocales();
|
|
|
|
var result = _.clone(locales);
|
|
|
|
_.each(_.keys(locales), function(langcode) {
|
2012-12-19 18:22:12 +01:00
|
|
|
if (langcode != 'en') result[langcode]='locales/'+langcode+'.json';
|
2012-12-17 18:18:07 +01:00
|
|
|
});
|
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
2012-12-04 12:12:58 +01:00
|
|
|
|
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
/*
|
|
|
|
* PUBLIC
|
|
|
|
*/
|
2012-11-28 04:02:55 +01:00
|
|
|
|
2012-11-10 14:12:17 +01:00
|
|
|
exports.expressCreateServer = function(n, args) {
|
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
//regenerate locales when server restart
|
|
|
|
locales = {};
|
|
|
|
var localeIndex = generateLocaleIndex();
|
|
|
|
exports.availableLangs = getAvailableLangs();
|
|
|
|
|
|
|
|
args.app.get ('/locales/:locale', function(req, res) {
|
|
|
|
//works with /locale/en and /locale/en.json requests
|
|
|
|
var locale = req.params.locale.split('.')[0];
|
|
|
|
if (exports.availableLangs.hasOwnProperty(locale)) {
|
2012-12-19 19:48:08 +01:00
|
|
|
res.setHeader('Content-Type', 'application/json; charset=utf8');
|
|
|
|
res.send('{"'+locale+'":'+JSON.stringify(locales[locale])+'}');
|
2012-12-17 18:18:07 +01:00
|
|
|
} else {
|
|
|
|
res.send(404, 'Language not available');
|
|
|
|
}
|
|
|
|
})
|
2012-11-14 17:01:59 +01:00
|
|
|
|
2012-12-17 18:18:07 +01:00
|
|
|
args.app.get('/locales.json', function(req, res) {
|
2012-12-19 19:48:08 +01:00
|
|
|
res.setHeader('Content-Type', 'application/json; charset=utf8');
|
2012-11-14 17:01:59 +01:00
|
|
|
res.send(localeIndex);
|
2012-11-10 14:12:17 +01:00
|
|
|
})
|
|
|
|
|
2012-11-20 19:46:17 +01:00
|
|
|
}
|
2012-12-17 18:18:07 +01:00
|
|
|
|