🤖 Merge PR #47811 [i18n] Add class for I18n (support i18n version 0.12.0) by @woodcockjosh

* 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 <ltdwoodcock@gmail.com>
This commit is contained in:
Josh Woodcock
2020-12-03 19:25:13 -06:00
committed by GitHub
parent 560c921e8b
commit 89bcbaf252
2 changed files with 113 additions and 11 deletions

View File

@@ -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'
});

83
types/i18n/index.d.ts vendored
View File

@@ -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 <https://github.com/SomaticIT>
// FindQ <https://github.com/FindQ>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 4.1
declare namespace i18n {
interface Response extends i18nAPI {
locals: Partial<i18nAPI>;
}
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<i18nAPI>;
}
}