Adding type defs for y18n (#35050)

This commit is contained in:
Adam Zerella 2019-04-29 16:58:52 +10:00 committed by Wesley Wigham
parent 5de1470697
commit a9cb5fa8b8
4 changed files with 109 additions and 0 deletions

60
types/y18n/index.d.ts vendored Normal file
View File

@ -0,0 +1,60 @@
// Type definitions for y18n 4.0
// Project: https://github.com/yargs/y18n
// Definitions by: Adam Zerella <https://github.com/adamzerella>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
interface Config {
/**
* The locale directory, default ./locales.
*/
directory: string;
/**
* Should newly observed strings be updated in file, default true.
*/
updateFiles: boolean;
/**
* What locale should be used.
*/
locale: string;
/**
* Should fallback to a language-only file (e.g. en.json) be allowed
* if a file matching the locale does not exist (e.g. en_US.json), default true.
*/
fallbackToLanguage: boolean;
}
declare class Y18N {
/**
* Create an instance of y18n with the config provided
*/
constructor(config?: Config)
/**
* Print a localized string, %s will be replaced with args.
*/
__(str: string, arg1?: string, arg2?: string, arg3?: string): string;
/**
* Print a localized string with appropriate pluralization.
* If %d is provided in the string, the count will replace this placeholder.
*/
__n(singularString: string, pluralString: string, count: number, arg1?: string, arg2?: string, arg3?: string): string;
/**
* Set the current locale being used.
*/
setLocale(str: string): void;
/**
* What locale is currently being used?
*/
getLocale(): string;
/**
* Update the current locale with the key value pairs in obj.
*/
updateLocale(obj: object): void;
}
export = Y18N;

25
types/y18n/tsconfig.json Normal file
View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"y18n-tests.ts"
]
}

3
types/y18n/tslint.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}

21
types/y18n/y18n-tests.ts Normal file
View File

@ -0,0 +1,21 @@
import y18n = require("y18n");
// Testing custom config
new y18n({
directory: '../locales',
updateFiles: false,
locale: 'en-AU',
fallbackToLanguage: false
});
const Y18N = new y18n();
Y18N.__(`my awesome string: foo`);
Y18N.__n('one fish %s', '%d fishes %s', 2, 'foo');
Y18N.setLocale('en-US');
Y18N.getLocale();
Y18N.updateLocale({});