docusaurus/lib/server/translation.js

45 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-08-15 19:55:38 -04:00
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2017-08-15 19:55:38 -04:00
*/
// translation object contains all translations for each string in i18n/en.json
2017-08-15 19:55:38 -04:00
2017-07-07 13:28:29 -04:00
const CWD = process.cwd();
const fs = require('fs');
const glob = require('glob');
const path = require('path');
2017-07-07 13:28:29 -04:00
let languages;
if (fs.existsSync(`${CWD}/languages.js`)) {
languages = require(`${CWD}/languages.js`);
} else {
languages = [
{
enabled: true,
name: 'English',
tag: 'en',
},
];
2017-07-07 13:28:29 -04:00
}
const enabledLanguages = languages.filter(lang => lang.enabled);
2017-07-07 13:28:29 -04:00
const translation = {languages: enabledLanguages};
2017-07-07 13:28:29 -04:00
const files = glob.sync(`${CWD}/i18n/**`);
const langRegex = /\/i18n\/(.*)\.json$/;
2017-07-07 13:28:29 -04:00
files.forEach(file => {
const extension = path.extname(file);
if (extension === '.json') {
const match = langRegex.exec(file);
const language = match[1];
translation[language] = require(file);
}
});
2017-07-07 13:28:29 -04:00
module.exports = translation;