UNPKG

6.87 kBTypeScriptView Raw
1import { AnyJson } from '@salesforce/ts-types';
2export declare type Tokens = Array<string | boolean | number | null | undefined>;
3/**
4 * A loader function to return messages.
5 *
6 * @param locale The local set by the framework.
7 */
8export declare type LoaderFunction = (locale: string) => Messages;
9/**
10 * The core message framework manages messages and allows them to be accessible by
11 * all plugins and consumers of sfdx-core. It is set up to handle localization down
12 * the road at no additional effort to the consumer. Messages can be used for
13 * anything from user output (like the console), to error messages, to returned
14 * data from a method.
15 *
16 * Messages are loaded from loader functions. The loader functions will only run
17 * when a message is required. This prevents all messages from being loaded into memory at
18 * application startup. The functions can load from memory, a file, or a server.
19 *
20 * In the beginning of your app or file, add the loader functions to be used later. If using
21 * json or js files in a root messages directory (`<moduleRoot>/messages`), load the entire directory
22 * automatically with {@link Messages.importMessagesDirectory}. Message files must be in `.json` or `.js`
23 * that exports a json object with **only** top level key-value pairs. The values support
24 * [util.format](https://nodejs.org/api/util.html#util_util_format_format_args) style strings
25 * that apply the tokens passed into {@link Message.getMessage}
26 *
27 * A sample message file.
28 * ```
29 * {
30 * 'msgKey': 'A message displayed in the terminal'
31 * }
32 * ```
33 *
34 * **Note:** When running unit tests individually, you may see errors that the messages aren't found.
35 * This is because `index.js` isn't loaded when tests run like they are when the package is required.
36 * To allow tests to run, import the message directory in each test (it will only
37 * do it once) or load the message file the test depends on individually.
38 *
39 * ```
40 * // Create loader functions for all files in the messages directory
41 * Messages.importMessagesDirectory(__dirname);
42 *
43 * // Now you can use the messages from anywhere in your code or file.
44 * // If using importMessageDirectory, the bundle name is the file name.
45 * const messages : Messages = Messages.loadMessages(packageName, bundleName);
46 *
47 * // Messages now contains all the message in the bundleName file.
48 * messages.getMessage('JsonParseError');
49 * ```
50 */
51export declare class Messages {
52 private messages;
53 private static loaders;
54 private static bundles;
55 /**
56 * The locale of the messages in this bundle.
57 */
58 readonly locale: string;
59 /**
60 * The bundle name.
61 */
62 readonly bundleName: string;
63 /**
64 * Create a new messages bundle.
65 *
66 * **Note:** Use {Messages.loadMessages} unless you are writing your own loader function.
67 *
68 * @param bundleName The bundle name.
69 * @param locale The locale.
70 * @param messages The messages. Can not be modified once created.
71 */
72 constructor(bundleName: string, locale: string, messages: Map<string, AnyJson>);
73 /**
74 * Internal readFile. Exposed for unit testing. Do not use util/fs.readFile as messages.js
75 * should have no internal dependencies.
76 *
77 * @param filePath read file target.
78 * @ignore
79 */
80 static readFile: (filePath: string) => AnyJson;
81 /**
82 * Get the locale. This will always return 'en_US' but will return the
83 * machine's locale in the future.
84 */
85 static getLocale(): string;
86 /**
87 * Set a custom loader function for a package and bundle that will be called on {@link Messages.loadMessages}.
88 *
89 * @param packageName The npm package name.
90 * @param bundle The name of the bundle.
91 * @param loader The loader function.
92 */
93 static setLoaderFunction(packageName: string, bundle: string, loader: LoaderFunction): void;
94 /**
95 * Generate a file loading function. Use {@link Messages.importMessageFile} unless
96 * overriding the bundleName is required, then manually pass the loader
97 * function to {@link Messages.setLoaderFunction}.
98 *
99 * @param bundleName The name of the bundle.
100 * @param filePath The messages file path.
101 */
102 static generateFileLoaderFunction(bundleName: string, filePath: string): LoaderFunction;
103 /**
104 * Add a single message file to the list of loading functions using the file name as the bundle name.
105 * The loader will only be added if the bundle name is not already taken.
106 *
107 * @param packageName The npm package name.
108 * @param filePath The path of the file.
109 */
110 static importMessageFile(packageName: string, filePath: string): void;
111 /**
112 * Import all json and js files in a messages directory. Use the file name as the bundle key when
113 * {@link Messages.loadMessages} is called. By default, we're assuming the moduleDirectoryPart is a
114 * typescript project and will truncate to root path (where the package.json file is). If your messages
115 * directory is in another spot or you are not using typescript, pass in false for truncateToProjectPath.
116 *
117 * ```
118 * // e.g. If your message directory is in the project root, you would do:
119 * Messages.importMessagesDirectory(__dirname);
120 * ```
121 *
122 * @param moduleDirectoryPath The path to load the messages folder.
123 * @param truncateToProjectPath Will look for the messages directory in the project root (where the package.json file is located).
124 * i.e., the module is typescript and the messages folder is in the top level of the module directory.
125 * @param packageName The npm package name. Figured out from the root directory's package.json.
126 */
127 static importMessagesDirectory(moduleDirectoryPath: string, truncateToProjectPath?: boolean, packageName?: string): void;
128 /**
129 * Load messages for a given package and bundle. If the bundle is not already cached, use the loader function
130 * created from {@link Messages.setLoaderFunction} or {@link Messages.importMessagesDirectory}.
131 *
132 * @param packageName The name of the npm package.
133 * @param bundleName The name of the bundle to load.
134 */
135 static loadMessages(packageName: string, bundleName: string): Messages;
136 /**
137 * Check if a bundle already been loaded.
138 *
139 * @param packageName The npm package name.
140 * @param bundleName The bundle name.
141 */
142 static isCached(packageName: string, bundleName: string): boolean;
143 /**
144 * Get a message using a message key and use the tokens as values for tokenization.
145 *
146 * @param key The key of the message.
147 * @param tokens The values to substitute in the message.
148 *
149 * **See** https://nodejs.org/api/util.html#util_util_format_format_args
150 */
151 getMessage(key: string, tokens?: Tokens): string;
152 private getMessageWithMap;
153}