update
This commit is contained in:
@ -2,48 +2,114 @@ import path from 'path';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { NextFunction, Request, Response } from 'express';
|
import { NextFunction, Request, Response } from 'express';
|
||||||
|
|
||||||
|
// interface LanguageValue {
|
||||||
|
// success_insert?: string;
|
||||||
|
// success_update?: string;
|
||||||
|
// success_delete?: string;
|
||||||
|
// success_restore?: string;
|
||||||
|
// success_view?: string;
|
||||||
|
// success?: string;
|
||||||
|
// success_valid_otp?: string;
|
||||||
|
|
||||||
|
// failed?: string;
|
||||||
|
// failed_access?: string;
|
||||||
|
// failed_access_otp?: string;
|
||||||
|
// failed_expired_otp?: string;
|
||||||
|
// failed_access_pin?: string;
|
||||||
|
// failed_try_pin?: string;
|
||||||
|
// failed_insert?: string;
|
||||||
|
// failed_update?: string;
|
||||||
|
// failed_delete?: string;
|
||||||
|
// failed_restore?: string;
|
||||||
|
// failed_save?: string;
|
||||||
|
// failed_view?: string;
|
||||||
|
// failed_empty?: string;
|
||||||
|
// failed_data?: string;
|
||||||
|
// failed_duplicate?: string;
|
||||||
|
// failed_not_found?: string;
|
||||||
|
// failed_limit_otp?: string;
|
||||||
|
// failed_limit_link_code?: string;
|
||||||
|
// failed_expired_token?: string;
|
||||||
|
// failed_token?: string;
|
||||||
|
// failed_add_point?: string;
|
||||||
|
|
||||||
|
// //login
|
||||||
|
// success_login?: string;
|
||||||
|
// failed_password?: string;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// export class Language {
|
||||||
|
// static lang_init: LanguageValue = {};
|
||||||
|
// static lang: LanguageValue = {};
|
||||||
|
|
||||||
|
// static setup() {
|
||||||
|
// const fileLang: any = fs.readFileSync(
|
||||||
|
// path.join(__dirname, '../langs/json/en.json')
|
||||||
|
// );
|
||||||
|
// Language.lang_init['en'] = JSON.parse(fileLang.toString('utf8'));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// static apply = (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
// const acceptLanguageHeader = req.get('Accept-Language') as string | null;
|
||||||
|
// if (!acceptLanguageHeader) {
|
||||||
|
// //default
|
||||||
|
// req.params.language = 'en';
|
||||||
|
// } else {
|
||||||
|
// req.params.language = acceptLanguageHeader.substring(0, 2).toLowerCase();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Language.lang = Language.lang_init[req.params.language] ? Language.lang_init[req.params.language] : Language.lang_init['en'];
|
||||||
|
|
||||||
|
// return next();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
interface LanguageValue {
|
interface LanguageValue {
|
||||||
success_insert?: string;
|
success_insert: string;
|
||||||
success_update?: string;
|
success_update: string;
|
||||||
success_delete?: string;
|
success_delete: string;
|
||||||
success_restore?: string;
|
success_restore: string;
|
||||||
success_view?: string;
|
success_view: string;
|
||||||
success?: string;
|
success: string;
|
||||||
success_valid_otp?: string;
|
success_valid_otp: string;
|
||||||
|
|
||||||
failed?: string;
|
failed: string;
|
||||||
failed_access?: string;
|
failed_access: string;
|
||||||
failed_access_otp?: string;
|
failed_access_otp: string;
|
||||||
failed_expired_otp?: string;
|
failed_expired_otp: string;
|
||||||
failed_access_pin?: string;
|
failed_access_pin: string;
|
||||||
failed_try_pin?: string;
|
failed_try_pin: string;
|
||||||
failed_insert?: string;
|
failed_insert: string;
|
||||||
failed_update?: string;
|
failed_update: string;
|
||||||
failed_delete?: string;
|
failed_delete: string;
|
||||||
failed_restore?: string;
|
failed_restore: string;
|
||||||
failed_save?: string;
|
failed_save: string;
|
||||||
failed_view?: string;
|
failed_view: string;
|
||||||
failed_empty?: string;
|
failed_empty: string;
|
||||||
failed_data?: string;
|
failed_data: string;
|
||||||
failed_duplicate?: string;
|
failed_duplicate: string;
|
||||||
failed_not_found?: string;
|
failed_not_found: string;
|
||||||
failed_limit_otp?: string;
|
failed_limit_otp: string;
|
||||||
failed_limit_link_code?: string;
|
failed_limit_link_code: string;
|
||||||
failed_expired_token?: string;
|
failed_expired_token: string;
|
||||||
failed_token?: string;
|
failed_token: string;
|
||||||
failed_add_point?: string;
|
failed_add_point: string;
|
||||||
|
|
||||||
//login
|
success_login: string;
|
||||||
success_login?: string;
|
failed_password: string;
|
||||||
failed_password?: string;
|
}
|
||||||
|
|
||||||
|
// Tambah index signature agar bisa akses dengan ['en']
|
||||||
|
interface LanguageStore {
|
||||||
|
[key: string]: LanguageValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Language {
|
export class Language {
|
||||||
static lang_init: LanguageValue = {};
|
static lang_init: LanguageStore = {};
|
||||||
static lang: LanguageValue = {};
|
static lang: LanguageValue = {} as LanguageValue;
|
||||||
|
|
||||||
static setup() {
|
static setup() {
|
||||||
const fileLang: any = fs.readFileSync(
|
const fileLang = fs.readFileSync(
|
||||||
path.join(__dirname, '../langs/json/en.json')
|
path.join(__dirname, '../langs/json/en.json')
|
||||||
);
|
);
|
||||||
Language.lang_init['en'] = JSON.parse(fileLang.toString('utf8'));
|
Language.lang_init['en'] = JSON.parse(fileLang.toString('utf8'));
|
||||||
@ -52,13 +118,13 @@ export class Language {
|
|||||||
static apply = (req: Request, res: Response, next: NextFunction) => {
|
static apply = (req: Request, res: Response, next: NextFunction) => {
|
||||||
const acceptLanguageHeader = req.get('Accept-Language') as string | null;
|
const acceptLanguageHeader = req.get('Accept-Language') as string | null;
|
||||||
if (!acceptLanguageHeader) {
|
if (!acceptLanguageHeader) {
|
||||||
//default
|
|
||||||
req.params.language = 'en';
|
req.params.language = 'en';
|
||||||
} else {
|
} else {
|
||||||
req.params.language = acceptLanguageHeader.substring(0, 2).toLowerCase();
|
req.params.language = acceptLanguageHeader.substring(0, 2).toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
Language.lang = Language.lang_init[req.params.language] ? Language.lang_init[req.params.language] : Language.lang_init['en'];
|
Language.lang = Language.lang_init[req.params.language]
|
||||||
|
?? Language.lang_init['en'];
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user