feat: localization invoice, estimate and receipt pdf templates.

This commit is contained in:
a.bouhuolia
2021-08-17 18:10:35 +02:00
parent 5c47d1549b
commit f7c3244145
23 changed files with 1938 additions and 82 deletions

View File

@@ -1,13 +1,15 @@
import { Container } from 'typedi';
import { Request, Response, NextFunction } from 'express';
import i18n from 'i18n';
import HasTenancyService from 'services/Tenancy/TenancyService';
import { injectI18nUtils } from './TenantDependencyInjection';
/**
* I18n from organization settings.
*/
export default (req: Request, res: Response, next: NextFunction) => {
const Logger = Container.get('logger');
const { settings } = req;
const { settings, tenantId } = req;
if (!req.user) {
throw new Error('Should load this middleware after `JWTAuth`.');
@@ -16,9 +18,8 @@ export default (req: Request, res: Response, next: NextFunction) => {
throw new Error('Should load this middleware after `SettingsMiddleware`.');
}
// Get the organization language from settings.
const language = settings.get({
group: 'organization', key: 'language',
});
const language = settings.get({ group: 'organization', key: 'language' });
if (language) {
i18n.setLocale(req, language);
}
@@ -26,5 +27,10 @@ export default (req: Request, res: Response, next: NextFunction) => {
language,
user: req.user,
});
const tenantServices = Container.get(HasTenancyService);
const tenantContainer = tenantServices.tenantContainer(tenantId);
tenantContainer.set('i18n', injectI18nUtils(req));
next();
};

View File

@@ -11,15 +11,11 @@ export default async (req: Request, res: Response, next: NextFunction) => {
if (tenantContainer && !tenantContainer.has('settings')) {
const { settingRepository } = tenantContainer.get('repositories');
Logger.info('[settings_middleware] initialize settings store.');
const settings = new SettingsStore(settingRepository);
tenantContainer.set('settings', settings);
}
Logger.info('[settings_middleware] get settings instance from container.');
const settings = tenantContainer.get('settings');
Logger.info('[settings_middleware] load settings from storage or cache.');
await settings.load();
req.settings = settings;

View File

@@ -3,6 +3,7 @@ import { ITenant } from 'interfaces';
import { Request } from 'express';
import TenancyService from 'services/Tenancy/TenancyService';
import TenantsManagerService from 'services/Tenancy/TenantsManager';
import rtlDetect from 'rtl-detect';
export default (req: Request, tenant: ITenant) => {
const { id: tenantId, organizationId } = tenant;
@@ -20,7 +21,7 @@ export default (req: Request, tenant: ITenant) => {
const tenantContainer = tenantServices.tenantContainer(tenantId);
tenantContainer.set('i18n', { __: req.__ });
tenantContainer.set('i18n', injectI18nUtils(req));
req.knex = knexInstance;
req.organizationId = organizationId;
@@ -29,4 +30,18 @@ export default (req: Request, tenant: ITenant) => {
req.models = models;
req.repositories = repositories;
req.cache = cacheInstance;
}
export const injectI18nUtils =(req) => {
const locale = req.getLocale();
const direction = rtlDetect.getLangDir(locale);
return {
locale,
__: req.__,
direction,
isRtl: direction === 'rtl',
isLtr: direction === 'ltr',
}
}