Skip to content

Instantly share code, notes, and snippets.

@benhowes
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhowes/ac346e968e36cadd68cd to your computer and use it in GitHub Desktop.
Save benhowes/ac346e968e36cadd68cd to your computer and use it in GitHub Desktop.
Files for Zoetrope i18n blog post
var langStrings={
"en": {
"callToAction": {
"desktop": "click and drag image to rotate in 3D",
"mobile": "swipe image to rotate in 3D"
},
"inlineCallToAction": {
"desktop": "click to rotate",
"mobile": "tap to rotate"
},
"rotate": "rotate",
"elevate": "elevate",
"zoom": "zoom",
"changed": "31/03/2014 13:57:45"
},
"hi": {
...
"rotate": "घुमाएँ",
"elevate": "ऊँचा करें",
"zoom": "ज़ूम",
"changed": "31/03/2014 14:19:24"
},
"de": {
...
}
};
{
"callToAction": {
"desktop": "click and drag image to rotate in 3D",
"mobile": "swipe image to rotate in 3D"
},
"inlineCallToAction": {
"desktop": "click to rotate",
"mobile": "tap to rotate"
},
"rotate": "rotate",
"elevate": "elevate",
"zoom": "zoom",
"changed": "31/03/2014 13:57:45"
},
var fs = require('fs');
var Q = require('q');
var download = require('gulp-download');
var convert = require('gulp-convert');
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
/* formats the json into our dictionary format. */
function makeLangugeObject(lang, defaultLang){
var output ={
callToAction : {
desktop : lang['click and drag image to rotate in 3D'] || defaultLang['click and drag image to rotate in 3D'],
mobile : lang['swipe image to rotate in 3D' ] || defaultLang['swipe image to rotate in 3D' ],
},
inlineCallToAction : {
desktop : lang['Click to rotate'] || defaultLang['Click to rotate'],
mobile : lang['Tap to rotate'] || defaultLang['Tap to rotate'],
},
rotate : lang['Rotate'] || defaultLang['Rotate'],
elevate : lang['Elevate'] || defaultLang['Elevate'],
zoom : lang['Zoom'] || defaultLang['Zoom'],
changed : lang.Timestamp,
};
return output;
}
gulp.task('lang-strings',function(){
var prom = Q.defer(),
outputName = './src/languages.js',
langColumn = '2 letter language Code', //language name column from form
outputString = '';
download(paths.languageStringsCSV)
.pipe(convert({from: 'csv', to: 'json'}))
.on('data',function(jsonString){
var languages = JSON.parse(decoder.write(jsonString.contents));
languagesObj = {};
for(index in languages){
var lang = languages[index],
defaultLang = languages[0];
languageName = lang[langColumn].toLowerCase();
languagesObj[languageName] = makeLangugeObject(lang,defaultLang);
}
//wrap JSON up as JS
outputString = 'var langStrings=' + JSON.stringify(languagesObj, null, 4) + ';';
fs.writeFile(outputName, outputString, function(err) {
if(err) {
gutil.error(err);
} else {
gutil.log("JSON saved to " + outputName);
}
prom.resolve(); // We're done when the file's written out
});
});
return prom.promise;
});
var language = (window.navigator.userLanguage || window.navigator.language || 'en');
var dictionary = langStrings[langage];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment