import dictionaries in build_app
authorSteve Hackbarth <stephenhackbarth@gmail.com>
Mon, 25 Aug 2014 03:32:28 +0000 (23:32 -0400)
committerSteve Hackbarth <stephenhackbarth@gmail.com>
Mon, 25 Aug 2014 03:32:28 +0000 (23:32 -0400)
scripts/lib/build_all.js
scripts/lib/build_dictionary.js

index 3a1e08b..469fbd6 100644 (file)
@@ -5,6 +5,7 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
 var _ = require('underscore'),
   async = require('async'),
   buildDatabase = require("./build_database"),
+  buildDictionary = require("./build_dictionary"),
   buildClient = require("./build_client").buildClient,
   defaultExtensions = require("./util/default_extensions").extensions,
   dataSource = require('../../node-datasource/lib/ext/datasource').dataSource,
@@ -103,9 +104,21 @@ var _ = require('underscore'),
           });
           done(null, "Build succeeded." + returnMessage);
         });
+      },
+      function (done) {
+        // step 4: import all dictionary files
+        if (specs[0].clientOnly || specs[0].databaseOnly) {
+          // don't build dictionaries if the user doesn't want us to
+          console.log("Not importing the dictionaries");
+          return done();
+        }
+        var databases = _.map(specs, function (spec) {
+          return spec.database;
+        });
+        async.map(databases, buildDictionary.importAllDictionaries, done);
       }
     ], function (err, results) {
-      buildAllCallback(err, results && results[results.length - 1]);
+      buildAllCallback(err, results && results[results.length - 2]);
     });
   };
 
index 7a6ec62..77ffc44 100644 (file)
@@ -201,14 +201,14 @@ if (typeof XT === 'undefined') {
               source: stringObj.value,
               target: preExistingTranslation
             });
-         } else if ( destinationLang.indexOf('en') === 0 ) {
-            // if locale is en_AU en_GB copy the en_US source: strings to target:
-            stringCallback(null, {
-                key: stringObj.key,
-                source: stringObj.value,
-                target: stringObj.value
-              });
-         } else {
+          } else if ( destinationLang.indexOf('en') === 0 ) {
+             // if locale is en_AU en_GB copy the en_US source: strings to target:
+             stringCallback(null, {
+               key: stringObj.key,
+               source: stringObj.value,
+               target: stringObj.value
+             });
+          } else {
             // ask google (or not)
             autoTranslate(stringObj.value, apiKey, destinationLang, function (err, target) {
               stringCallback(null, {
@@ -217,7 +217,7 @@ if (typeof XT === 'undefined') {
                 target: target
               });
             });
-         };
+         }
         };
         async.map(stringsArray, processString, function (err, strings) {
           extensionCallback(null, {
@@ -262,14 +262,11 @@ if (typeof XT === 'undefined') {
   /**
     Takes a dictionary definition file and inserts the data into the database
    */
-  exports.importDictionary = function (database, filename, masterCallback) {
+  var importDictionary = exports.importDictionary = function (database, filename, masterCallback) {
     var creds = require("../../node-datasource/config").databaseServer;
     creds.database = database;
 
-    // the filename relative unless it starts with a slash
-    if (filename.substring(0, 1) !== '/') {
-      filename = path.join(process.cwd(), filename);
-    }
+    filename = path.resolve(process.cwd(), filename);
     if (path.extname(filename) !== '.js') {
       console.log("Skipping non-dictionary file", filename);
       masterCallback();
@@ -299,4 +296,20 @@ if (typeof XT === 'undefined') {
     });
   };
 
+  exports.importAllDictionaries = function (database, callback) {
+    var translationsDir = path.join(__dirname, "../../node_modules/xtuple-linguist/translations");
+    if (!fs.existsSync(translationsDir)) {
+      console.log("No translations directory found. Ignoring linguist.");
+      return callback();
+    }
+    var importOne = function (dictionary, next) {
+      importDictionary(database, dictionary, next);
+    };
+    var allDictionaries = _.map(fs.readdirSync(translationsDir), function (filename) {
+      return path.join(translationsDir, filename);
+    });
+    async.map(allDictionaries, importOne, callback);
+  };
+
+
 }());