Last active
August 29, 2015 14:01
-
-
Save benhowes/ac346e968e36cadd68cd to your computer and use it in GitHub Desktop.
Files for Zoetrope i18n blog post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | |
... | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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