get languages availables through translatewiki API

This commit is contained in:
Iván Eixarch 2012-11-28 23:54:23 +01:00
parent 9ec8a6e3d3
commit 795a20b3f2
2 changed files with 32 additions and 18 deletions

View File

@ -1,4 +1,4 @@
var Globalize = require('globalize') var http = require ('http')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, express = require('express') , express = require('express')
@ -8,24 +8,39 @@ var localesPath = __dirname+"/../../locales";
// Serve English strings directly with /locales.ini // Serve English strings directly with /locales.ini
var localeIndex = fs.readFileSync(localesPath+'/en.ini')+'\r\n'; var localeIndex = fs.readFileSync(localesPath+'/en.ini')+'\r\n';
// add language base 'en' to availableLangs exports.availableLangs = {'en': {'nativeName': 'English'}};
exports.availableLangs = {};
exports.availableLangs['en'] = Globalize.cultures['en'];
fs.readdir(localesPath, function(er, files) { // build availableLangs with translatewiki web API
files.forEach(function(locale) { var request = http.request ('http://translatewiki.net/w/api.php?action=query&meta=siteinfo&siprop=languages&format=json',
locale = locale.split('.')[0] function (res) {
if(locale.toLowerCase() == 'en') return; var twLangs = '';
res.setEncoding ('utf8');
res.on ('data', function (chunk) { twLangs += chunk; });
res.on ('end', function () {
// twLangs = [{code: 'en', '*': 'English'}...]
twLangs = JSON.parse(twLangs)['query']['languages'];
// build locale index fs.readdir(localesPath, function(er, files) {
localeIndex += '['+locale+']\r\n@import url(locales/'+locale+'.ini)\r\n' files.forEach(function(locale) {
locale = locale.split('.')[0];
if(locale.toLowerCase() == 'en') return;
// build locale index
localeIndex += '['+locale+']\r\n@import url(locales/'+locale+'.ini)\r\n';
require('globalize/lib/cultures/globalize.culture.'+locale+'.js') for (var l = 0; l < twLangs.length; l++) {
exports.availableLangs[locale]=Globalize.cultures[locale]; var code = twLangs[l]['code'];
}) if (locale == code) {
}) var nativeName = twLangs[l]['*'];
exports.availableLangs[code] = {'nativeName': nativeName};
console.log(exports.availableLangs); }
}
});
});
});
}).on ('error', function(e) {
console.error('While query translatewiki API: '+e.message);
}).end();
exports.expressCreateServer = function(n, args) { exports.expressCreateServer = function(n, args) {

View File

@ -35,8 +35,7 @@
"security" : "1.0.0", "security" : "1.0.0",
"tinycon" : "0.0.1", "tinycon" : "0.0.1",
"underscore" : "1.3.1", "underscore" : "1.3.1",
"unorm" : "1.0.0", "unorm" : "1.0.0"
"globalize" : "0.1.1"
}, },
"bin": { "etherpad-lite": "./node/server.js" }, "bin": { "etherpad-lite": "./node/server.js" },
"devDependencies": { "devDependencies": {