Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use i18n params detection plugin #239

Merged
merged 2 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ilc/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ const UrlProcessor = require('../common/UrlProcessor');
/**
* @param {Registry} registryService
*/
module.exports = (registryService) => {
module.exports = (registryService, pluginManager) => {
const app = fastify(Object.assign({
trustProxy: false, //TODO: should be configurable via Registry
}, require('./logger/fastify')));

const i18nParamsDetectionPlugin = pluginManager.getI18nParamsDetectionPlugin();

app.addHook('onRequest', async (req, reply) => {
req.raw.ilcState = {};
const registryConfig = await registryService.getConfig();
const i18nOnRequest = i18n.onRequestFactory(registryConfig.data.settings.i18n);
const i18nOnRequest = i18n.onRequestFactory(registryConfig.data.settings.i18n, i18nParamsDetectionPlugin);

await i18nOnRequest(req, reply);
});
Expand Down
32 changes: 21 additions & 11 deletions ilc/server/i18n.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const Cookie = require('cookie');
const Intl = require('ilc-sdk/app').Intl;
const {intlSchema} = require('ilc-sdk/dist/server/IlcProtocol'); //"Private" import
const {intlSchema} = require('ilc-sdk/dist/server/IlcProtocol'); // "Private" import

const cookieEncoder = require('../common/i18nCookie');

const onRequestFactory = (i18nConfig) => async (req, reply) => {
const onRequestFactory = (i18nConfig, i18nParamsDetectionPlugin) => async (req, reply) => {
if (!i18nConfig.enabled || req.raw.url === '/ping' || req.raw.url.startsWith('/_ilc/')) {
return; // Excluding system routes
}

let currI18nConf = { ...i18nConfig.default };
let currI18nConf = {...i18nConfig.default};

const i18nCookie = Cookie.parse(req.headers.cookie || '')[cookieEncoder.name];
if (i18nCookie) {
Expand All @@ -17,15 +18,24 @@ const onRequestFactory = (i18nConfig) => async (req, reply) => {
currI18nConf.currency = i18nConfig.supported.currency.includes(decodedCookie.currency) ? decodedCookie.currency : currI18nConf.currency;
}

//TODO: add ability to "init" i18n configuration not from default values, but rather by use of the IP, "accept-language", etc..
// Interface: async (req: http.IncomingMessage, currI18nConf): {locale?: string, currency?: string}

const routeLocale = Intl.parseUrl(i18nConfig, req.raw.url);
if (routeLocale.locale !== i18nConfig.default.locale) { // URL can override locale only if it's not-default one
currI18nConf.locale = routeLocale.locale;
if (i18nParamsDetectionPlugin) {
currI18nConf = await i18nParamsDetectionPlugin.detectI18nConfig(
req.raw,
{
parseUrl: (url) => Intl.parseUrl(i18nConfig, url),
localizeUrl: (url, {locale}) => Intl.localizeUrl(i18nConfig, url, {locale}),
getCanonicalLocale: (locale) => Intl.getCanonicalLocale(locale, i18nConfig.supported.locale),
},
currI18nConf
);
} else if (!i18nCookie) {
const routeLocale = Intl.parseUrl(i18nConfig, req.raw.url);
if (routeLocale.locale !== i18nConfig.default.locale) { // URL can override locale only if it's not-default one
currI18nConf.locale = routeLocale.locale;
}
}

const fixedUrl = Intl.localizeUrl(i18nConfig, req.raw.url, { locale: currI18nConf.locale });
const fixedUrl = Intl.localizeUrl(i18nConfig, req.raw.url, {locale: currI18nConf.locale});
if (fixedUrl !== req.raw.url) {
reply.redirect(fixedUrl);
return;
Expand Down Expand Up @@ -66,4 +76,4 @@ function unlocalizeUrl(i18nConfig, url) {
module.exports = {
onRequestFactory,
unlocalizeUrl,
}
};
Loading