Skip to content

Commit

Permalink
Initialize list of supported languages from directories in the locale…
Browse files Browse the repository at this point in the history
… folder.
  • Loading branch information
kallenboone authored and knolleary committed Jul 2, 2015
1 parent 965c093 commit 284d7e2
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions red/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var path = require("path");
var fs = require("fs");

var defaultLang = "en-US";
var supportedLangs = null;

var resourceMap = {
"runtime": {
Expand All @@ -42,6 +43,19 @@ function registerMessageCatalog(namespace,dir,file) {
});
}

var initSupportedLangs = function() {
return when.promise(function(resolve,reject) {
fs.readdir(resourceMap.editor.basedir, function(err,files) {
if(err) {
reject(err);
} else {
supportedLangs = files;
resolve();
}
});
});
}

var MessageFileLoader = {
fetchOne: function(lng, ns, callback) {
if (resourceMap[ns]) {
Expand Down Expand Up @@ -79,7 +93,9 @@ function init() {
},
fallbackLng: ['en-US']
},function() {
resolve();
initSupportedLangs().then(function() {
resolve();
});
});
});
}
Expand All @@ -104,13 +120,12 @@ function getCatalog(namespace,lang) {
function determineLangFromHeaders(acceptedLanguages){
var lang = "en-US";

var supportedLanguages = ['en-US', 'es', 'fr', 'it', 'de', 'pt-BR', 'zh', 'zh-TW', 'ko', 'ja', 'zz-ZZ']; // TODO: pull this value from settings
for (var i=0;i<acceptedLanguages.length;i++){
if (supportedLanguages.indexOf(acceptedLanguages[i]) !== -1){
if (supportedLangs.indexOf(acceptedLanguages[i]) !== -1){
lang = acceptedLanguages[i];
break;
// check the language without the country code
} else if (supportedLanguages.indexOf(acceptedLanguages[i].split("-")[0]) !== -1) {
} else if (supportedLangs.indexOf(acceptedLangs[i].split("-")[0]) !== -1) {
lang = acceptedLanguages[i].split("-")[0];
break;
}
Expand Down

0 comments on commit 284d7e2

Please sign in to comment.