From 89bcbaf252839fb545a44fed202ceeb07675689d Mon Sep 17 00:00:00 2001 From: Josh Woodcock Date: Thu, 3 Dec 2020 19:25:13 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#47811=20[i18n]=20A?= =?UTF-8?q?dd=20class=20for=20I18n=20(support=20i18n=20version=200.12.0)?= =?UTF-8?q?=20by=20@woodcockjosh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add class for I18n * Add missing addLocale and removeLocale * [i18n]: Add type definitions for 0.10.0. Remove properties from Response and Request interface * [i18n] Update typescript and i18n-node versions in header comment Co-authored-by: joshwoodcock --- types/i18n/i18n-tests.ts | 41 ++++++++++++++++++-- types/i18n/index.d.ts | 83 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 113 insertions(+), 11 deletions(-) diff --git a/types/i18n/i18n-tests.ts b/types/i18n/i18n-tests.ts index e5ba2b4bd9..d967b33766 100644 --- a/types/i18n/i18n-tests.ts +++ b/types/i18n/i18n-tests.ts @@ -7,9 +7,11 @@ import express = require("express"); import i18n = require("i18n"); +import { I18n } from "i18n"; +import { Request } from "express-serve-static-core"; const app = express(); -declare const req: express.Request; +declare const req: Express.Request & Request; /** * Configuration @@ -78,6 +80,11 @@ i18n.configure({ console.log('error', msg); }, + // Function to provide missing translations. + missingKeyFn: (locale, value) => { + return `Translation for "${value}" is missing for locale "${locale}"!`; + }, + // object or [obj1, obj2] to bind the i18n api and current locale to - defaults to null register: global, @@ -91,7 +98,23 @@ i18n.configure({ // Downcase locale when passed on queryParam; e.g. lang=en-US becomes // en-us. When set to false, the queryParam value will be used as passed; // e.g. lang=en-US remains en-US. - preserveLegacyCase: true + preserveLegacyCase: true, + + // Static translation catalog. Setting this option overrides `locales` + // tslint:disable:object-literal-key-quotes + staticCatalog: { + 'en-US': { + 'no': 'No', + 'ok': 'Ok', + 'yes': 'Yes' + }, + 'nl-NL': { + 'no': 'Nee', + 'ok': 'Oké', + 'yes': 'Ja' + } + } + // tslint:enable:object-literal-key-quotes }); /** @@ -163,7 +186,7 @@ i18n.__n({ singular: "%s cat", plural: "%s cats", locale: "fr", count: 3 }); // * __mf() * https://github.com/mashpie/i18n-node#i18n__mf */ -app.get('/de', (_req: Express.Request, res: Express.Response) => { +app.get('/de', (_req: Express.Request, res: i18n.Response) => { // assume res is set to german res.setLocale('de'); @@ -200,7 +223,7 @@ i18n.setLocale('de'); i18n.setLocale(req, 'de'); req.setLocale('de'); -app.get('/ar', (_req: Express.Request, res: Express.Response) => { +app.get('/ar', (_req: Express.Request, res: i18n.Response) => { i18n.setLocale(req, 'ar'); i18n.setLocale(res, 'ar'); i18n.setLocale(res.locals, 'ar'); @@ -223,6 +246,9 @@ req.getLocale(); // --> de */ i18n.getLocales(); // --> ['en', 'de', 'en-GB'] +i18n.addLocale('de'); // adds locale +i18n.removeLocale('de'); // removes locale + /** * getCatalog() * https://github.com/mashpie/i18n-node#getcatalog @@ -235,3 +261,10 @@ i18n.getCatalog(req, 'de'); // returns just 'de' req.getCatalog(); // returns all locales req.getCatalog('de'); // returns just 'de' + +const i18nInstance = new I18n(); // creates new instance of i18n + +i18nInstance.configure({ + locales: ['en', 'de'], + directory: __dirname + '/locales' +}); diff --git a/types/i18n/index.d.ts b/types/i18n/index.d.ts index 37727ee9c3..a21e4fa91d 100644 --- a/types/i18n/index.d.ts +++ b/types/i18n/index.d.ts @@ -1,11 +1,15 @@ -// Type definitions for i18n-node 0.8 +// Type definitions for i18n-node 0.12 // Project: http://github.com/mashpie/i18n-node // Definitions by: Maxime LUCE // FindQ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 4.1 declare namespace i18n { + interface Response extends i18nAPI { + locals: Partial; + } + interface ConfigurationOptions { /** * Setup some locales - other locales default to en silently @@ -111,6 +115,12 @@ declare namespace i18n { */ logErrorFn?: (msg: string) => void; + /** + * Function to provide missing translations. + * @since 0.10.0 + */ + missingKeyFn?: (locale: string, value: string) => string; + /** * object or [obj1, obj2] to bind the i18n api and current locale to * @default null @@ -132,6 +142,14 @@ declare namespace i18n { * @default true */ preserveLegacyCase?: boolean; + + /** + * Static translation catalog. Setting this option overrides `locales`. + * + * **NOTE**: Enabling `staticCatalog` disables all other fs realated options such as `updateFiles`, `autoReload` and `syncFiles`. + * @since 0.10.0 + */ + staticCatalog?: GlobalCatalog; } interface TranslateOptions { phrase: string; @@ -293,6 +311,10 @@ declare namespace i18n { */ function getLocales(): string[]; + function addLocale(locale: string): void; + + function removeLocale(locale: string): void; + //#endregion //#region Catalog @@ -328,6 +350,56 @@ declare namespace i18n { * Get current i18n-node version */ const version: string; + + class I18n { + configure(options: ConfigurationOptions): void; + + init(request: Express.Request, response: Express.Response, next?: () => void): void; + + __(phraseOrOptions: string | TranslateOptions, ...replace: string[]): string; + + __(phraseOrOptions: string | TranslateOptions, replacements: Replacements): string; + + __n(phrase: string, count: number): string; + + __n(options: PluralOptions, count?: number): string; + + __n(singular: string, plural: string, count: number | string): string; + + __mf(phraseOrOptions: string | TranslateOptions, ...replace: any[]): string; + + __mf(phraseOrOptions: string | TranslateOptions, replacements: Replacements): string; + + __l(phrase: string): string[]; + + __h(phrase: string): HashedList[]; + + setLocale(locale: string): void; + + // tslint:disable-next-line:unified-signatures + setLocale(requestOrResponse: Express.Request | Express.Response, locale: string, inheritance?: boolean): void; + + // tslint:disable-next-line:unified-signatures + setLocale(objects: any | any[], locale: string, inheritance?: boolean): void; + + getLocale(request?: Express.Request): string; + + getLocales(): string[]; + + addLocale(locale: string): void; + + removeLocale(locale: string): void; + + getCatalog(): GlobalCatalog; + + getCatalog(locale: string): LocaleCatalog; + + getCatalog(request: Express.Request, locale?: string): LocaleCatalog; + + overrideLocaleFromQuery(request?: Express.Request): void; + + version: string; + } } interface i18nAPI { @@ -449,14 +521,11 @@ declare module "i18n" { } declare namespace Express { + // tslint:disable-next-line:no-empty-interface interface Request extends i18nAPI { - languages: string[]; - regions: string[]; - language: string; - region: string; } + // tslint:disable-next-line:no-empty-interface interface Response extends i18nAPI { - locals: Partial; } }