summaryrefslogtreecommitdiff
path: root/src/node
diff options
context:
space:
mode:
authorMarcel Klehr <mklehr@gmx.net>2012-12-27 07:56:41 -0800
committerMarcel Klehr <mklehr@gmx.net>2012-12-27 07:56:41 -0800
commit369e24682b7958eb30accccf31e971735c3b1076 (patch)
tree4cd66c40ecf3c65a5613f474896cf19c9311aa43 /src/node
parent746396951d66457519f32c71b432c04f2fe003ed (diff)
parente60f8de676a5d0830778bcda61e83650fc5fa7f0 (diff)
downloadetherpad-lite-369e24682b7958eb30accccf31e971735c3b1076.zip
Merge pull request #1297 from marcelklehr/migrate-to-html10n
Migrate to html10n and allow plugin l10n
Diffstat (limited to 'src/node')
-rw-r--r--src/node/hooks/i18n.js103
1 files changed, 85 insertions, 18 deletions
diff --git a/src/node/hooks/i18n.js b/src/node/hooks/i18n.js
index a45d46fe..2d10fd5b 100644
--- a/src/node/hooks/i18n.js
+++ b/src/node/hooks/i18n.js
@@ -2,35 +2,102 @@ var languages = require('languages4translatewiki')
, fs = require('fs')
, path = require('path')
, express = require('express')
+ , _ = require('underscore')
+ , npm = require('npm')
+ , plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins.js').plugins
+;
-var localesPath = __dirname+"/../../locales";
-// Serve English strings directly with /locales.ini
-var localeIndex = fs.readFileSync(localesPath+'/en.ini')+'\r\n';
+// returns all existing messages merged together and grouped by langcode
+// {es: {"foo": "string"}, en:...}
+function getAllLocales() {
+ var locales2paths = {};
-exports.availableLangs = {'en': {'nativeName': 'English', 'direction': 'ltr'}};
+ // Puts the paths of all locale files contained in a given directory
+ // into `locales2paths` (files from various dirs are grouped by lang code)
+ // (only json files with valid language code as name)
+ function extractLangs(dir) {
+ if(!fs.existsSync(dir)) return;
+ var stat = fs.lstatSync(dir);
+ if (!stat.isDirectory() || stat.isSymbolicLink()) return;
-fs.readdir(localesPath, function(er, files) {
- files.forEach(function(locale) {
- var ext = path.extname(locale);
- locale = path.basename(locale, ext).toLowerCase();
- if(locale == 'en' || ext != '.ini') return;
+ fs.readdirSync(dir).forEach(function(file) {
+ file = path.resolve(dir, file);
+ stat = fs.lstatSync(file);
+ if (stat.isDirectory() || stat.isSymbolicLink()) return;
- // build locale index
- localeIndex += '['+locale+']\r\n@import url(locales/'+locale+'.ini)\r\n'
-
- // add info language {nativeName, direction} to availableLangs
- exports.availableLangs[locale]=languages.getLanguageInfo(locale);
- })
-})
+ var ext = path.extname(file)
+ , locale = path.basename(file, ext).toLowerCase();
+
+ if ((ext == '.json') && languages.isValid(locale)) {
+ if(!locales2paths[locale]) locales2paths[locale] = [];
+ locales2paths[locale].push(file);
+ }
+ });
+ }
+
+ //add core supported languages first
+ extractLangs(npm.root+"/ep_etherpad-lite/locales");
+
+ //add plugins languages (if any)
+ for(var pluginName in plugins) extractLangs(path.join(npm.root, pluginName, 'locales'));
+
+ // Build a locale index (merge all locale data)
+ var locales = {}
+ _.each (locales2paths, function(files, langcode) {
+ locales[langcode]={};
+
+ files.forEach(function(file) {
+ var fileContents = JSON.parse(fs.readFileSync(file,'utf8'));
+ _.extend(locales[langcode], fileContents);
+ });
+ });
+
+ return locales;
+}
+
+// returns a hash of all available languages availables with nativeName and direction
+// e.g. { es: {nativeName: "espaƱol", direction: "ltr"}, ... }
+function getAvailableLangs(locales) {
+ var result = {};
+ _.each(_.keys(locales), function(langcode) {
+ result[langcode] = languages.getLanguageInfo(langcode);
+ });
+ return result;
+}
+
+// returns locale index that will be served in /locales.json
+var generateLocaleIndex = function (locales) {
+ var result = _.clone(locales) // keep English strings
+ _.each(_.keys(locales), function(langcode) {
+ if (langcode != 'en') result[langcode]='locales/'+langcode+'.json';
+ });
+ return JSON.stringify(result);
+}
exports.expressCreateServer = function(n, args) {
- args.app.use('/locales', express.static(localesPath));
+ //regenerate locales on server restart
+ var locales = getAllLocales();
+ var localeIndex = generateLocaleIndex(locales);
+ exports.availableLangs = getAvailableLangs(locales);
+
+ 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)) {
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
+ res.send('{"'+locale+'":'+JSON.stringify(locales[locale])+'}');
+ } else {
+ res.send(404, 'Language not available');
+ }
+ })
- args.app.get('/locales.ini', function(req, res) {
+ args.app.get('/locales.json', function(req, res) {
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.send(localeIndex);
})
}
+