@ -13,7 +13,7 @@ var Plugins = require('../plugins');
var buildLanguagesPath = path . join ( _ _dirname , '../../build/public/language' ) ;
var coreLanguagesPath = path . join ( _ _dirname , '../../public/language' ) ;
function getTranslation Tree ( callback ) {
function getTranslation Metadata ( callback ) {
async . waterfall ( [
// generate list of languages and namespaces
function ( next ) {
@ -49,42 +49,51 @@ function getTranslationTree(callback) {
// save a list of languages to `${buildLanguagesPath}/metadata.json`
// avoids readdirs later on
function ( ref , next ) {
async . waterfall ( [
async . series ( [
function ( next ) {
mkdirp ( buildLanguagesPath , next ) ;
} ,
function ( x, next) {
function ( next) {
fs . writeFile ( path . join ( buildLanguagesPath , 'metadata.json' ) , JSON . stringify ( {
languages : ref . languages ,
namespaces : ref . namespaces ,
} ) , next ) ;
} ,
function ( next ) {
next ( null , ref ) ;
} ,
] , next ) ;
] , function ( err ) {
next ( err , ref ) ;
} ) ;
} ,
] , callback ) ;
}
// for each language and namespace combination,
// run through core and all plugins to generate
// a full translation hash
function ( ref , next ) {
var languages = ref . languages ;
function writeLanguageFile ( language , namespace , translations , callback ) {
var dev = global . env === 'development' ;
var filePath = path . join ( buildLanguagesPath , language , namespace + '.json' ) ;
async . series ( [
async . apply ( mkdirp , path . dirname ( filePath ) ) ,
async . apply ( fs . writeFile , filePath , JSON . stringify ( translations , null , dev ? 2 : 0 ) ) ,
] , callback ) ;
}
// for each language and namespace combination,
// run through core and all plugins to generate
// a full translation hash
function buildTranslations ( ref , next ) {
var namespaces = ref . namespaces ;
var languages = ref . languages ;
var plugins = _ . values ( Plugins . pluginsData ) . filter ( function ( plugin ) {
return typeof plugin . languages === 'string' ;
} ) ;
var tree = { } ;
async . eachLimit ( languages , 10 , function ( lang , next ) {
async . eachLimit ( namespaces , 10 , function ( namespace , next ) {
async . each ( namespaces , function ( namespace , next ) {
async . each ( languages , function ( lang , next ) {
var translations = { } ;
async . series ( [
// core first
function ( cb ) {
fs . readFile ( path . join ( coreLanguagesPath , lang , namespace + '.json' ) , function ( err , buf fer ) {
fs . readFile ( path . join ( coreLanguagesPath , lang , namespace + '.json' ) , 'utf8' , function ( err , fil e) {
if ( err ) {
if ( err . code === 'ENOENT' ) {
return cb ( ) ;
@ -93,7 +102,7 @@ function getTranslationTree(callback) {
}
try {
Object . assign ( translations , JSON . parse ( buffer. toString ( ) ) ) ;
Object . assign ( translations , JSON . parse ( file ) ) ;
cb ( ) ;
} catch ( err ) {
cb ( err ) ;
@ -106,7 +115,7 @@ function getTranslationTree(callback) {
// 2. old language string (en_GB)
// 3. corrected plugin defaultLang (en-US)
// 4. old plugin defaultLang (en_US)
async . each Limit ( plugins , 20 , function ( pluginData , done ) {
async . each ( plugins , function ( pluginData , done ) {
var pluginLanguages = path . join ( _ _dirname , '../../node_modules/' , pluginData . id , pluginData . languages ) ;
var defaultLang = pluginData . defaultLang || 'en-GB' ;
@ -116,7 +125,7 @@ function getTranslationTree(callback) {
lang . replace ( '-' , '_' ) . replace ( '-x-' , '@' ) ,
lang ,
] , function ( language , next ) {
fs . readFile ( path . join ( pluginLanguages , language , namespace + '.json' ) , function ( err , buf fer ) {
fs . readFile ( path . join ( pluginLanguages , language , namespace + '.json' ) , 'utf8' , function ( err , fil e) {
if ( err ) {
if ( err . code === 'ENOENT' ) {
return next ( null , false ) ;
@ -125,7 +134,7 @@ function getTranslationTree(callback) {
}
try {
Object . assign ( translations , JSON . parse ( buffer. toString ( ) ) ) ;
Object . assign ( translations , JSON . parse ( file ) ) ;
next ( null , true ) ;
} catch ( err ) {
next ( err ) ;
@ -138,40 +147,15 @@ function getTranslationTree(callback) {
}
if ( Object . keys ( translations ) . length ) {
tree [ lang ] = tree [ lang ] || { } ;
tree [ lang ] [ namespace ] = translations ;
writeLanguageFile ( lang , namespace , translations , cb ) ;
return ;
}
cb ( ) ;
} ) ;
} ,
] , next ) ;
} , next ) ;
} , function ( err ) {
next ( err , tree ) ;
} ) ;
} ,
] , callback ) ;
}
// write translation hashes from the generated tree to language files
function writeLanguageFiles ( tree , callback ) {
// iterate over languages and namespaces
async . eachLimit ( Object . keys ( tree ) , 100 , function ( language , cb ) {
var namespaces = tree [ language ] ;
async . eachLimit ( Object . keys ( namespaces ) , 10 , function ( namespace , next ) {
var translations = namespaces [ namespace ] ;
var filePath = path . join ( buildLanguagesPath , language , namespace + '.json' ) ;
mkdirp ( path . dirname ( filePath ) , function ( err ) {
if ( err ) {
return next ( err ) ;
}
fs . writeFile ( filePath , JSON . stringify ( translations ) , next ) ;
} ) ;
} , cb ) ;
} , callback ) ;
} , next ) ;
}
exports . build = function buildLanguages ( callback ) {
@ -179,7 +163,7 @@ exports.build = function buildLanguages(callback) {
function ( next ) {
rimraf ( buildLanguagesPath , next ) ;
} ,
getTranslation Tree ,
writeLanguageFile s,
getTranslation Metadata ,
buildTranslation s,
] , callback ) ;
} ;