mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 15:50:32 +00:00
fix: middleware i18n localization.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Response, Request, NextFunction } from 'express';
|
import { Response, Request, NextFunction } from 'express';
|
||||||
import { matchedData, validationResult } from 'express-validator';
|
import { matchedData, validationResult } from 'express-validator';
|
||||||
import accepts from 'accepts';
|
import accepts from 'accepts';
|
||||||
import { camelCase, snakeCase, omit, set, get } from 'lodash';
|
import { isArray, drop, first, camelCase, snakeCase, omit, set, get } from 'lodash';
|
||||||
import { mapKeysDeep } from 'utils';
|
import { mapKeysDeep } from 'utils';
|
||||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||||
|
|
||||||
@@ -57,6 +57,37 @@ export default class BaseController {
|
|||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets localization to response object by the given path.
|
||||||
|
* @param {Response} response -
|
||||||
|
* @param {string} path -
|
||||||
|
* @param {Request} req -
|
||||||
|
*/
|
||||||
|
private setLocalizationByPath(
|
||||||
|
response: any,
|
||||||
|
path: string,
|
||||||
|
req: Request,
|
||||||
|
) {
|
||||||
|
const DOT = '.';
|
||||||
|
|
||||||
|
if (isArray(response)) {
|
||||||
|
response.forEach((va) => {
|
||||||
|
const currentPath = first(path.split(DOT));
|
||||||
|
const value = get(va, currentPath);
|
||||||
|
|
||||||
|
if (isArray(value)) {
|
||||||
|
const nextPath = drop(path.split(DOT)).join(DOT);
|
||||||
|
this.setLocalizationByPath(value, nextPath, req);
|
||||||
|
} else {
|
||||||
|
set(va, path, req.__(value));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
const value = get(response, path);
|
||||||
|
set(response, path, req.__(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform the given data to response.
|
* Transform the given data to response.
|
||||||
* @param {any} data
|
* @param {any} data
|
||||||
@@ -74,13 +105,14 @@ export default class BaseController {
|
|||||||
: [translatable];
|
: [translatable];
|
||||||
|
|
||||||
translatables.forEach((path) => {
|
translatables.forEach((path) => {
|
||||||
const value = get(response, path);
|
this.setLocalizationByPath(response, path, req);
|
||||||
set(response, path, req.__(value));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Async middleware.
|
* Async middleware.
|
||||||
* @param {function} callback
|
* @param {function} callback
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ export default class ViewsController extends BaseController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* List all views that associated with the given resource.
|
* List all views that associated with the given resource.
|
||||||
* @param {Request} req -
|
* @param {Request} req - Request object.
|
||||||
* @param {Response} res -
|
* @param {Response} res - Response object.
|
||||||
* @param {NextFunction} next -
|
* @param {NextFunction} next - Next function.
|
||||||
*/
|
*/
|
||||||
async listResourceViews(req: Request, res: Response, next: NextFunction) {
|
async listResourceViews(req: Request, res: Response, next: NextFunction) {
|
||||||
const { tenantId } = req;
|
const { tenantId } = req;
|
||||||
@@ -50,8 +50,9 @@ export default class ViewsController extends BaseController {
|
|||||||
tenantId,
|
tenantId,
|
||||||
resourceModel
|
resourceModel
|
||||||
);
|
);
|
||||||
|
return res.status(200).send({
|
||||||
return res.status(200).send({ views });
|
views: this.transfromToResponse(views, ['name', 'columns.label'], req),
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import TenancyMiddleware from 'api/middleware/TenancyMiddleware';
|
|||||||
import EnsureTenantIsInitialized from 'api/middleware/EnsureTenantIsInitialized';
|
import EnsureTenantIsInitialized from 'api/middleware/EnsureTenantIsInitialized';
|
||||||
import SettingsMiddleware from 'api/middleware/SettingsMiddleware';
|
import SettingsMiddleware from 'api/middleware/SettingsMiddleware';
|
||||||
import I18nMiddleware from 'api/middleware/I18nMiddleware';
|
import I18nMiddleware from 'api/middleware/I18nMiddleware';
|
||||||
|
import I18nAuthenticatedMiddlware from 'api/middleware/I18nAuthenticatedMiddlware';
|
||||||
import EnsureConfiguredMiddleware from 'api/middleware/EnsureConfiguredMiddleware';
|
import EnsureConfiguredMiddleware from 'api/middleware/EnsureConfiguredMiddleware';
|
||||||
import EnsureTenantIsSeeded from 'api/middleware/EnsureTenantIsSeeded';
|
import EnsureTenantIsSeeded from 'api/middleware/EnsureTenantIsSeeded';
|
||||||
|
|
||||||
@@ -65,10 +66,10 @@ export default () => {
|
|||||||
dashboard.use(JWTAuth);
|
dashboard.use(JWTAuth);
|
||||||
dashboard.use(AttachCurrentTenantUser);
|
dashboard.use(AttachCurrentTenantUser);
|
||||||
dashboard.use(TenancyMiddleware);
|
dashboard.use(TenancyMiddleware);
|
||||||
dashboard.use(I18nMiddleware);
|
|
||||||
dashboard.use(SubscriptionMiddleware('main'));
|
dashboard.use(SubscriptionMiddleware('main'));
|
||||||
dashboard.use(EnsureTenantIsInitialized);
|
dashboard.use(EnsureTenantIsInitialized);
|
||||||
dashboard.use(SettingsMiddleware);
|
dashboard.use(SettingsMiddleware);
|
||||||
|
dashboard.use(I18nAuthenticatedMiddlware);
|
||||||
dashboard.use(EnsureConfiguredMiddleware);
|
dashboard.use(EnsureConfiguredMiddleware);
|
||||||
dashboard.use(EnsureTenantIsSeeded);
|
dashboard.use(EnsureTenantIsSeeded);
|
||||||
|
|
||||||
|
|||||||
30
server/src/api/middleware/I18nAuthenticatedMiddlware.ts
Normal file
30
server/src/api/middleware/I18nAuthenticatedMiddlware.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { Container } from 'typedi';
|
||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import i18n from 'i18n';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* I18n from organization settings.
|
||||||
|
*/
|
||||||
|
export default (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const Logger = Container.get('logger');
|
||||||
|
const { settings } = req;
|
||||||
|
|
||||||
|
if (!req.user) {
|
||||||
|
throw new Error('Should load this middleware after `JWTAuth`.');
|
||||||
|
}
|
||||||
|
if (!req.settings) {
|
||||||
|
throw new Error('Should load this middleware after `SettingsMiddleware`.');
|
||||||
|
}
|
||||||
|
// Get the organization language from settings.
|
||||||
|
const language = settings.get({
|
||||||
|
group: 'organization', key: 'language',
|
||||||
|
});
|
||||||
|
if (language) {
|
||||||
|
i18n.setLocale(req, language);
|
||||||
|
}
|
||||||
|
Logger.info('[i18n_authenticated_middleware] set locale language to i18n.', {
|
||||||
|
language,
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
next();
|
||||||
|
};
|
||||||
@@ -1,15 +1,22 @@
|
|||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { Request, Response, NextFunction } from 'express';
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import { lowerCase } from 'lodash';
|
||||||
import i18n from 'i18n';
|
import i18n from 'i18n';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the language from request `accept-language` header
|
||||||
|
* or default application language.
|
||||||
|
*/
|
||||||
export default (req: Request, res: Response, next: NextFunction) => {
|
export default (req: Request, res: Response, next: NextFunction) => {
|
||||||
const Logger = Container.get('logger');
|
const Logger = Container.get('logger');
|
||||||
let language = req.headers['accept-language'] || 'en';
|
|
||||||
|
|
||||||
if (req.user && req.user.language) {
|
// Parses the accepted language from request object.
|
||||||
language = req.user.language;
|
const language = lowerCase(req.headers['accept-language']) || 'en';
|
||||||
}
|
|
||||||
Logger.info('[i18n_middleware] set locale language to i18n.', { language, user: req.user });
|
Logger.info('[i18n_middleware] set locale language to i18n.', {
|
||||||
|
language,
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
i18n.setLocale(req, language);
|
i18n.setLocale(req, language);
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -1,127 +1,127 @@
|
|||||||
export default {
|
export default {
|
||||||
organization: {
|
organization: {
|
||||||
name: {
|
name: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
base_currency: {
|
base_currency: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
industry: {
|
industry: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
location: {
|
location: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
fiscal_year: {
|
fiscal_year: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
financial_date_start: {
|
financial_date_start: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
language: {
|
language: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
time_zone: {
|
time_zone: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
date_format: {
|
date_format: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
accounting_basis: {
|
accounting_basis: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
manual_journals: {
|
manual_journals: {
|
||||||
next_number: {
|
next_number: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
number_prefix: {
|
number_prefix: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
auto_increment: {
|
auto_increment: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
bill_payments: {
|
bill_payments: {
|
||||||
withdrawal_account: {
|
withdrawal_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
sales_estimates: {
|
sales_estimates: {
|
||||||
next_number: {
|
next_number: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
number_prefix: {
|
number_prefix: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
auto_increment: {
|
auto_increment: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
sales_receipts: {
|
sales_receipts: {
|
||||||
next_number: {
|
next_number: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
number_prefix: {
|
number_prefix: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
auto_increment: {
|
auto_increment: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
preferred_deposit_account: {
|
preferred_deposit_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
sales_invoices: {
|
sales_invoices: {
|
||||||
next_number: {
|
next_number: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
number_prefix: {
|
number_prefix: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
auto_increment: {
|
auto_increment: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
payment_receives: {
|
payment_receives: {
|
||||||
next_number: {
|
next_number: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
number_prefix: {
|
number_prefix: {
|
||||||
type: "string",
|
type: 'string',
|
||||||
},
|
},
|
||||||
auto_increment: {
|
auto_increment: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
deposit_account: {
|
deposit_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
advance_deposit: {
|
advance_deposit: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
items: {
|
items: {
|
||||||
sell_account: {
|
sell_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
cost_account: {
|
cost_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
inventory_account: {
|
inventory_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expenses: {
|
expenses: {
|
||||||
preferred_payment_account: {
|
preferred_payment_account: {
|
||||||
type: "number",
|
type: 'number',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
accounts: {
|
accounts: {
|
||||||
account_code_required: {
|
account_code_required: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
account_code_unique: {
|
account_code_unique: {
|
||||||
type: "boolean",
|
type: 'boolean',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
"Bank": "المصرف",
|
"Bank": "المصرف",
|
||||||
"Other Income": "إيرادات اخري",
|
"Other Income": "إيرادات اخري",
|
||||||
"Interest Income": "إيرادات الفوائد",
|
"Interest Income": "إيرادات الفوائد",
|
||||||
"Opening Balance": "رصيد",
|
|
||||||
"Depreciation Expense": "مصاريف الاهلاك",
|
"Depreciation Expense": "مصاريف الاهلاك",
|
||||||
"Interest Expense": "مصروفات الفوائد",
|
"Interest Expense": "مصروفات الفوائد",
|
||||||
"Sales of Product Income": "مبيعات دخل المنتجات",
|
"Sales of Product Income": "مبيعات دخل المنتجات",
|
||||||
@@ -89,20 +88,21 @@
|
|||||||
"Profit Margin": "هامش الربح",
|
"Profit Margin": "هامش الربح",
|
||||||
"Value": "القيمة",
|
"Value": "القيمة",
|
||||||
"Rate": "السعر",
|
"Rate": "السعر",
|
||||||
"OPERATING ACTIVITIES": "أنشطة التشغيل",
|
"OPERATING ACTIVITIES": "الأنشطة التشغيلية",
|
||||||
"FINANCIAL ACTIVITIES": "الأنشطة المالية",
|
"FINANCIAL ACTIVITIES": "الأنشطة التمويلية",
|
||||||
|
"INVESTMENT ACTIVITIES": "الانشطة الاستثمارية",
|
||||||
"Net income": "صافي الدخل",
|
"Net income": "صافي الدخل",
|
||||||
"Adjustments net income by operating activities.": "تعديلات صافي الدخل حسب الأنشطة التشغيلية.",
|
"Adjustments net income by operating activities.": "تسويات صافي الدخل من الأنشطة التشغيلية.",
|
||||||
"Net cash provided by operating activities": "صافي النقد الناتج من أنشطة التشغيل",
|
"Net cash provided by operating activities": "صافي التدفقات النقدية من أنشطة التشغيل",
|
||||||
"Net cash provided by investing activities": "صافي النقد المقدم من أنشطة الاستثمار",
|
"Net cash provided by investing activities": "صافي التدفقات النقدية من أنشطة الاستثمار",
|
||||||
"Net cash provided by financing activities": "صافي النقد الناتج عن أنشطة التمويل",
|
"Net cash provided by financing activities": "صافي التدفقات النقدية من أنشطة التمويلية",
|
||||||
"Cash at beginning of period": "النقدية في بداية الفترة",
|
"Cash at beginning of period": "التدفقات النقدية في بداية الفترة",
|
||||||
"NET CASH INCREASE FOR PERIOD": "زيادة صافي النقد للفترة",
|
"NET CASH INCREASE FOR PERIOD": "زيادة التدفقات النقدية للفترة",
|
||||||
"CASH AT END OF PERIOD": "النقد في نهاية الفترة",
|
"CASH AT END OF PERIOD": "صافي التدفقات النقدية في نهاية الفترة",
|
||||||
"Expenses": "مصاريف",
|
"Expenses": "مصاريف",
|
||||||
"Services": "خدمات",
|
"Services": "خدمات",
|
||||||
"Inventory": "المخزون",
|
"Inventory": "المخزون",
|
||||||
"Non-Inventory": "غير متعلق بالمخزون",
|
"Non Inventory": "غير المخزون",
|
||||||
"Draft": "مسودة",
|
"Draft": "مسودة",
|
||||||
"Delivered": "تم التوصيل",
|
"Delivered": "تم التوصيل",
|
||||||
"Overdue": "متأخر",
|
"Overdue": "متأخر",
|
||||||
@@ -157,5 +157,8 @@
|
|||||||
"Current Liabilties": "التزامات متداولة",
|
"Current Liabilties": "التزامات متداولة",
|
||||||
"Long-Term Liabilities": "التزامات طويلة الاجل",
|
"Long-Term Liabilities": "التزامات طويلة الاجل",
|
||||||
"Non-Current Liabilities": "التزامات غير متداولة",
|
"Non-Current Liabilities": "التزامات غير متداولة",
|
||||||
"Liabilities and Equity": "التزامات وحقوق الملكية"
|
"Liabilities and Equity": "التزامات وحقوق الملكية",
|
||||||
|
"Closing balance": "الرصيد الختامي",
|
||||||
|
"Opening balance": "الرصيد الفتاحي",
|
||||||
|
"Total {{accountName}}": "إجمالي {{accountName}}"
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,6 @@
|
|||||||
"Bank": "Bank",
|
"Bank": "Bank",
|
||||||
"Other Income": "Other Income",
|
"Other Income": "Other Income",
|
||||||
"Interest Income": "Interest Income",
|
"Interest Income": "Interest Income",
|
||||||
"Opening Balance": "Opening Balance",
|
|
||||||
"Depreciation Expense": "Depreciation Expense",
|
"Depreciation Expense": "Depreciation Expense",
|
||||||
"Interest Expense": "Interest Expense",
|
"Interest Expense": "Interest Expense",
|
||||||
"Sales of Product Income": "Sales of Product Income",
|
"Sales of Product Income": "Sales of Product Income",
|
||||||
@@ -102,7 +101,7 @@
|
|||||||
"Expenses": "Expenses",
|
"Expenses": "Expenses",
|
||||||
"Services": "Services",
|
"Services": "Services",
|
||||||
"Inventory": "Inventory",
|
"Inventory": "Inventory",
|
||||||
"Non-Inventory": "Non-Inventory",
|
"Non Inventory": "Non Inventory",
|
||||||
"Draft": "Draft",
|
"Draft": "Draft",
|
||||||
"Published": "Published",
|
"Published": "Published",
|
||||||
"Delivered": "Delivered",
|
"Delivered": "Delivered",
|
||||||
@@ -157,5 +156,8 @@
|
|||||||
"Current Liabilties": "Current Liabilties",
|
"Current Liabilties": "Current Liabilties",
|
||||||
"Long-Term Liabilities": "Long-Term Liabilities",
|
"Long-Term Liabilities": "Long-Term Liabilities",
|
||||||
"Non-Current Liabilities": "Non-Current Liabilities",
|
"Non-Current Liabilities": "Non-Current Liabilities",
|
||||||
"Liabilities and Equity": "Liabilities and Equity"
|
"Liabilities and Equity": "Liabilities and Equity",
|
||||||
|
"Closing balance": "Closing balance",
|
||||||
|
"Opening Balance": "Opening balance",
|
||||||
|
"Total {{accountName}}": "Total {{accountName}}"
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ export const DEFAULT_VIEWS = [
|
|||||||
columns: DEFAULT_VIEW_COLUMNS,
|
columns: DEFAULT_VIEW_COLUMNS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Opended',
|
name: 'Opened',
|
||||||
slug: 'opened',
|
slug: 'opened',
|
||||||
rolesLogicExpression: '1',
|
rolesLogicExpression: '1',
|
||||||
roles: [
|
roles: [
|
||||||
@@ -59,7 +59,7 @@ export const DEFAULT_VIEWS = [
|
|||||||
columns: DEFAULT_VIEW_COLUMNS,
|
columns: DEFAULT_VIEW_COLUMNS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Partially Paid',
|
name: 'Partially paid',
|
||||||
slug: 'partially-paid',
|
slug: 'partially-paid',
|
||||||
rolesLogicExpression: '1',
|
rolesLogicExpression: '1',
|
||||||
roles: [
|
roles: [
|
||||||
|
|||||||
Reference in New Issue
Block a user