Skip to content

Instantly share code, notes, and snippets.

@sharpred
Created February 18, 2014 11:26
Show Gist options
  • Save sharpred/9069190 to your computer and use it in GitHub Desktop.
Save sharpred/9069190 to your computer and use it in GitHub Desktop.
main.js change
(function() {
var Tabletop = require('tabletop');
var wrench = require('wrench');
var public_url = "https://docs.google.com/spreadsheet/pub?key=0ArTqrFjFwISadGI4anRRZlM4UGZrcjNTMTJ0OWsydkE&output";
var builder = require('xmlbuilder');
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var cwd;
var configPath;
console.log('configPath ' + configPath);
var xml = builder.create('resources', {
'version' : '1.0',
'encoding' : 'UTF-8'
});
var config, brand, brandMap = {}, languageMap = {}, brandLanguage, brandVal, newItems = [], xmlString = '', i18nDir, saveDir, saveDirEN, savePath, savePathEN;
var showInfo = function(data) {
var sitemapref = '';
//console.log('showInfo' +JSON.stringify(data));
//build an array of language objects
data.forEach(function(key, val) {
var obj = {}, text;
//don't push empty fields into the markup
text = key[brandVal];
if (text !== '') {
if (key['sitemapref'] !== sitemapref) {
sitemapref = key['sitemapref'];
obj = {
'com' : key['sitemapref']
};
}
obj.ele = {
'name' : key['resourceid'],
'value' : key[brandVal]
};
newItems.push(obj);
}
});
//convert language objects into an XML document
_.each(newItems, function(item) {
if (item.com) {
var com = xml.com(item.com);
}
if (item.ele) {
var ele = xml.ele('string', {
name : item.ele['name']
}, item.ele['value']);
}
});
//end the XML document
xmlString = xml.end({
pretty : true
});
//replace apos with single quote
xmlString = xmlString.replace(/'/g, "'");
//nuke existing files/folders from i18n
//write the file
fs.writeFile(savePath, xmlString, function(err) {
if (err) {
console.log('unable to save to ' + savePath);
} else {
console.log('saved language strings');
}
});
if (saveDir !== saveDirEN) {
//write the file
fs.writeFile(savePathEN, xmlString, function(err) {
if (err) {
console.log('unable to save to ' + savePath);
} else {
console.log('saved language strings');
}
});
}
};
var init = function(path) {
cwd = path;
configPath = cwd + '/app/config.json';
console.log('configPath ' + configPath);
xml = builder.create('resources', {
'version' : '1.0',
'encoding' : 'UTF-8'
});
config = fs.readFileSync(configPath, 'utf8');
config = JSON.parse(config);
brand = config.global.theme;
brandMap = {
'firstchoice' : 'firstchoice',
'thomson' : 'thomson',
'meinetui' : 'meinetui',
'nordics' : 'se'
};
languageMap = {
'firstchoice' : 'en',
'thomson' : 'en',
'meinetui' : 'de',
'se' : 'se'
};
brandLanguage = languageMap[brand];
brandVal = brandMap[brand];
console.log('lang: ' + brandLanguage + ' brand: ' + brandVal);
i18nDir = cwd + '/i18n';
saveDir = cwd + '/i18n/' + brandLanguage;
saveDirEN = cwd + '/i18n/en';
wrench.rmdirSyncRecursive(i18nDir, true);
wrench.mkdirSyncRecursive(i18nDir, 0777);
wrench.mkdirSyncRecursive(saveDir, 0777);
savePath = saveDir + '/strings.xml';
savePathEN = saveDirEN + '/strings.xml';
//you must have an EN folder or you get blank strings
if (saveDir !== saveDirEN) {
wrench.mkdirSyncRecursive(saveDirEN, 0777);
}
saveRawPath = cwd + '/google.txt';
console.log('save path: ' + savePath);
configPath = Tabletop.init({
key : public_url,
callback : showInfo,
simpleSheet : true
});
};
//init();
//public API
exports.init = init;
exports.showInfo = showInfo;
exports.brandLanguage = brandLanguage;
exports.brandVal = brandVal;
exports.savePath = savePath;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment