Skip to content

Commit e5f1f3b

Browse files
authored
Improve i18n caching (HabitRPG#12030)
* fix indentation * wip: cache i18n responses * cache i18n browser script to disk * typos * misc fixes
1 parent 446122d commit e5f1f3b

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ website/transpiled-babel/
44
website/common/transpiled-babel/
55
node_modules
66
content_cache
7+
i18n_cache
78
apidoc_build
89
*.swp
910
.idea*

gulp/gulp-build.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ gulp.task('build:babel:common', () => gulp.src('website/common/script/**/*.js')
1111

1212
gulp.task('build:babel', gulp.parallel('build:babel:server', 'build:babel:common', done => done()));
1313

14+
gulp.task('build:cache', gulp.parallel(
15+
'cache:content',
16+
'cache:i18n',
17+
done => done(),
18+
));
19+
1420
gulp.task('build:prod', gulp.series(
1521
'build:babel',
1622
'apidoc',
17-
'content:cache',
23+
'build:cache',
1824
done => done(),
1925
));
2026

gulp/gulp-content.js renamed to gulp/gulp-cache.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import gulp from 'gulp';
22
import fs from 'fs';
33

44
// TODO parallelize, use gulp file helpers
5-
gulp.task('content:cache', done => {
5+
gulp.task('cache:content', done => {
66
// Requiring at runtime because these files access `common`
77
// code which in production works only if transpiled so after
88
// gulp build:babel:common has run
@@ -32,3 +32,32 @@ gulp.task('content:cache', done => {
3232
done(err);
3333
}
3434
});
35+
36+
gulp.task('cache:i18n', done => {
37+
// Requiring at runtime because these files access `common`
38+
// code which in production works only if transpiled so after
39+
// gulp build:babel:common has run
40+
const { BROWSER_SCRIPT_CACHE_PATH, geti18nBrowserScript } = require('../website/server/libs/i18n'); // eslint-disable-line global-require
41+
const { langCodes } = require('../website/server/libs/i18n'); // eslint-disable-line global-require
42+
43+
try {
44+
// create the cache folder (if it doesn't exist)
45+
try {
46+
fs.mkdirSync(BROWSER_SCRIPT_CACHE_PATH);
47+
} catch (err) {
48+
if (err.code !== 'EEXIST') throw err;
49+
}
50+
51+
// create and save the i18n browser script for each language
52+
langCodes.forEach(languageCode => {
53+
fs.writeFileSync(
54+
`${BROWSER_SCRIPT_CACHE_PATH}${languageCode}.js`,
55+
geti18nBrowserScript(languageCode),
56+
'utf8',
57+
);
58+
});
59+
done();
60+
} catch (err) {
61+
done(err);
62+
}
63+
});

gulpfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const gulp = require('gulp');
1313

1414
if (process.env.NODE_ENV === 'production') { // eslint-disable-line no-process-env
1515
require('./gulp/gulp-apidoc'); // eslint-disable-line global-require
16-
require('./gulp/gulp-content'); // eslint-disable-line global-require
16+
require('./gulp/gulp-cache'); // eslint-disable-line global-require
1717
require('./gulp/gulp-build'); // eslint-disable-line global-require
1818
} else {
1919
require('./gulp/gulp-apidoc'); // eslint-disable-line global-require
20-
require('./gulp/gulp-content'); // eslint-disable-line global-require
20+
require('./gulp/gulp-cache'); // eslint-disable-line global-require
2121
require('./gulp/gulp-build'); // eslint-disable-line global-require
2222
require('./gulp/gulp-console'); // eslint-disable-line global-require
2323
require('./gulp/gulp-sprites'); // eslint-disable-line global-require

website/server/controllers/api-v3/i18n.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
import _ from 'lodash';
1+
import nconf from 'nconf';
22
import {
3-
translations,
4-
momentLangs,
5-
availableLanguages,
3+
BROWSER_SCRIPT_CACHE_PATH,
4+
geti18nBrowserScript,
65
} from '../../libs/i18n';
76

8-
const api = {};
9-
10-
function geti18nBrowserScript (language) {
11-
const langCode = language.code;
7+
const IS_PROD = nconf.get('IS_PROD');
128

13-
return `(function () {
14-
if (!window) return;
15-
window['habitica-i18n'] = ${JSON.stringify({
16-
availableLanguages,
17-
language,
18-
strings: translations[langCode],
19-
momentLang: momentLangs[langCode],
20-
})};
21-
})()`;
22-
}
9+
const api = {};
2310

2411
/**
2512
* @api {get} /api/v3/i18n/browser-script Returns the i18n JS script.
@@ -33,14 +20,16 @@ api.geti18nBrowserScript = {
3320
method: 'GET',
3421
url: '/i18n/browser-script',
3522
async handler (req, res) {
36-
const language = _.find(availableLanguages, { code: req.language });
37-
38-
res.set({
39-
'Content-Type': 'application/javascript',
40-
});
23+
if (IS_PROD) {
24+
res.sendFile(`${BROWSER_SCRIPT_CACHE_PATH}${req.language}.js`);
25+
} else {
26+
res.set({
27+
'Content-Type': 'application/javascript',
28+
});
4129

42-
const jsonResString = geti18nBrowserScript(language);
43-
res.status(200).send(jsonResString);
30+
const jsonResString = geti18nBrowserScript(req.language);
31+
res.status(200).send(jsonResString);
32+
}
4433
},
4534
};
4635

website/server/libs/i18n.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import _ from 'lodash';
44
import shared from '../../common';
55

66
export const localePath = path.join(__dirname, '../../common/locales/');
7+
export const BROWSER_SCRIPT_CACHE_PATH = path.join(__dirname, '/../../../i18n_cache/');
78

89
// Store translations
910
export const translations = {};
@@ -115,3 +116,17 @@ export const multipleVersionsLanguages = {
115116
'pt-br': 'pt_BR',
116117
},
117118
};
119+
120+
export function geti18nBrowserScript (languageCode) {
121+
const language = _.find(availableLanguages, { code: languageCode });
122+
123+
return `(function () {
124+
if (!window) return;
125+
window['habitica-i18n'] = ${JSON.stringify({
126+
availableLanguages,
127+
language,
128+
strings: translations[languageCode],
129+
momentLang: momentLangs[languageCode],
130+
})};
131+
})()`;
132+
}

0 commit comments

Comments
 (0)