Files
habitica/website/server/controllers/api-v3/i18n.js
Matteo Pagliazzi bd46e3e195 Client: i18n (#8972)
* wip: client: i18n

* remove maxAge from cookies to get same expiration ad localStorage

* set cookies expiration to 10 years

* moment: load translations in browser, moment: only load necessary data, remove jquery, remove bluebird

* ability to change language

* fix logout

* add some requiresLogin: false to static pages

* fix tests
2017-08-22 18:26:53 +02:00

47 lines
1.1 KiB
JavaScript

import {
translations,
momentLangs,
availableLanguages,
} from '../../libs/i18n';
import _ from 'lodash';
const api = {};
function geti18nBrowserScript (language) {
const langCode = language.code;
return `(function () {
if (!window) return;
window['habitica-i18n'] = ${JSON.stringify({
availableLanguages,
language,
strings: translations[langCode],
momentLang: momentLangs[language.momentLangCode],
})};
})()`;
}
/**
* @api {get} /api/v3/i18n/browser-script Returns a JS script to make all the i18n strings available in the browser
* under window.i18n.strings
* @apiDescription Does not require authentication.
* @apiName i18nBrowserScriptGet
* @apiGroup i18n
*/
api.geti18nBrowserScript = {
method: 'GET',
url: '/i18n/browser-script',
async handler (req, res) {
const language = _.find(availableLanguages, {code: req.language});
res.set({
'Content-Type': 'application/javascript',
});
const jsonResString = geti18nBrowserScript(language);
res.status(200).send(jsonResString);
},
};
module.exports = api;