@discord/intl-loader-core 0.8.0 → 0.9.0-canary.66e0616
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +16 -7
- package/src/database.js +2 -2
- package/src/processing.js +18 -10
- package/src/transformer.js +1 -1
- package/src/watcher.js +15 -13
- package/types/index.d.ts +14 -0
- package/types/src/database.d.ts +6 -0
- package/types/src/processing.d.ts +74 -0
- package/types/src/transformer.d.ts +106 -0
- package/types/src/util.d.ts +18 -0
- package/types/src/watcher.d.ts +16 -0
- package/types/types.d.ts +111 -0
- package/index.d.ts +0 -17
- /package/{src/types.d.ts → types.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/intl-loader-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0-canary.66e0616",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Core utilities for writing loaders and transformers using @discord/intl",
|
|
6
6
|
"author": "Jon Egeland",
|
|
7
7
|
"main": "index.js",
|
|
8
|
-
"types": "index.d.ts",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"index.js",
|
|
11
|
-
"
|
|
12
|
-
"src"
|
|
11
|
+
"types.d.ts",
|
|
12
|
+
"src",
|
|
13
|
+
"types"
|
|
13
14
|
],
|
|
14
15
|
"exports": {
|
|
15
|
-
".":
|
|
16
|
+
".": {
|
|
17
|
+
"default": "./index.js",
|
|
18
|
+
"types": "./types/index.d.ts"
|
|
19
|
+
}
|
|
16
20
|
},
|
|
17
21
|
"repository": {
|
|
18
22
|
"type": "git",
|
|
@@ -22,9 +26,14 @@
|
|
|
22
26
|
"chokidar": "^3.6.0",
|
|
23
27
|
"debug": "^4.3.6",
|
|
24
28
|
"fast-glob": "^3.3.2",
|
|
25
|
-
"@discord/intl-message-database": "0.
|
|
29
|
+
"@discord/intl-message-database": "0.9.0-rc.2"
|
|
26
30
|
},
|
|
27
31
|
"devDependencies": {
|
|
28
|
-
"@types/debug": "^4.1.12"
|
|
32
|
+
"@types/debug": "^4.1.12",
|
|
33
|
+
"typescript": "*"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc && cp types.d.ts types/",
|
|
37
|
+
"build:release": "tsc && cp types.d.ts types/"
|
|
29
38
|
}
|
|
30
39
|
}
|
package/src/database.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const {IntlMessagesDatabase} = require('@discord/intl-message-database');
|
|
1
|
+
const { IntlMessagesDatabase } = require('@discord/intl-message-database');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A shared message database instance that's used and shared across all parts
|
|
@@ -6,4 +6,4 @@ const {IntlMessagesDatabase} = require('@discord/intl-message-database');
|
|
|
6
6
|
*/
|
|
7
7
|
const database = new IntlMessagesDatabase();
|
|
8
8
|
|
|
9
|
-
module.exports = {database};
|
|
9
|
+
module.exports = { database };
|
package/src/processing.js
CHANGED
|
@@ -6,6 +6,13 @@ const { IntlCompiledMessageFormat } = require('@discord/intl-message-database');
|
|
|
6
6
|
const { database } = require('./database');
|
|
7
7
|
const { findAllTranslationFiles, getLocaleFromTranslationsFileName } = require('./util');
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* format?: IntlCompiledMessageFormat,
|
|
12
|
+
* bundleSecrets?: boolean,
|
|
13
|
+
* }} IntlPrecompileOptions
|
|
14
|
+
*/
|
|
15
|
+
|
|
9
16
|
/**
|
|
10
17
|
* @param {string} sourcePath
|
|
11
18
|
* @param {import('@discord/intl-message-database').IntlSourceFile} sourceFile
|
|
@@ -43,7 +50,7 @@ function buildTranslationsLocaleMap(sourcePath, sourceFile, translationsPath) {
|
|
|
43
50
|
* processTranslations?: boolean,
|
|
44
51
|
* locale?: string
|
|
45
52
|
* }=} options
|
|
46
|
-
* @returns {import('
|
|
53
|
+
* @returns {import('../types').ProcessDefinitionsResult}
|
|
47
54
|
*/
|
|
48
55
|
function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
|
|
49
56
|
const {
|
|
@@ -95,7 +102,7 @@ function processDefinitionsFile(sourcePath, sourceContent, options = {}) {
|
|
|
95
102
|
* @param {{
|
|
96
103
|
* locale?: string,
|
|
97
104
|
* }=} options
|
|
98
|
-
* @returns {import('
|
|
105
|
+
* @returns {import('../types').ProcessTranslationsResult}
|
|
99
106
|
*/
|
|
100
107
|
function processTranslationsFile(sourcePath, sourceContent, options = {}) {
|
|
101
108
|
const { locale = getLocaleFromTranslationsFileName(sourcePath) } = options;
|
|
@@ -129,20 +136,21 @@ function processTranslationsFile(sourcePath, sourceContent, options = {}) {
|
|
|
129
136
|
* but if `outputFile` is given then the content will be written directly to the file and the
|
|
130
137
|
* function becomes `void`.
|
|
131
138
|
*
|
|
139
|
+
* Compiling automatically handles filtering out messages based on the meta information like
|
|
140
|
+
* `translate`, `secret`, and `bundleSecrets`, to ensure that all consumers apply these values
|
|
141
|
+
* accurately and consistently.
|
|
142
|
+
*
|
|
132
143
|
* @param {string} sourcePath
|
|
133
144
|
* @param {string} locale
|
|
134
|
-
* @param {
|
|
135
|
-
*
|
|
136
|
-
* outputFile?: string
|
|
137
|
-
* }=} options
|
|
145
|
+
* @param {string=} outputFile
|
|
146
|
+
* @param {IntlPrecompileOptions} [options]
|
|
138
147
|
*
|
|
139
148
|
* @returns {Buffer | void}
|
|
140
149
|
*/
|
|
141
|
-
function precompileFileForLocale(sourcePath, locale, options = {}) {
|
|
142
|
-
const { format = IntlCompiledMessageFormat.KeylessJson, outputFile } = options;
|
|
150
|
+
function precompileFileForLocale(sourcePath, locale, outputFile, options = {}) {
|
|
143
151
|
return outputFile != null
|
|
144
|
-
? database.precompile(sourcePath, locale, outputFile,
|
|
145
|
-
: database.precompileToBuffer(sourcePath, locale,
|
|
152
|
+
? database.precompile(sourcePath, locale, outputFile, options)
|
|
153
|
+
: database.precompileToBuffer(sourcePath, locale, options);
|
|
146
154
|
}
|
|
147
155
|
|
|
148
156
|
/**
|
package/src/transformer.js
CHANGED
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
*/
|
|
65
65
|
class MessageDefinitionsTransformer {
|
|
66
66
|
/**
|
|
67
|
-
* @param {import('
|
|
67
|
+
* @param {import('../types.d.ts').MessageDefinitionsTransformerOptions} options
|
|
68
68
|
*/
|
|
69
69
|
constructor(options) {
|
|
70
70
|
this.options = options;
|
package/src/watcher.js
CHANGED
|
@@ -2,7 +2,7 @@ const path = require('node:path');
|
|
|
2
2
|
|
|
3
3
|
const chokidar = require('chokidar');
|
|
4
4
|
const fg = require('fast-glob');
|
|
5
|
-
const debug = require('debug')('intl:
|
|
5
|
+
const debug = require('debug')('intl:loader-core:watcher');
|
|
6
6
|
const {
|
|
7
7
|
isMessageDefinitionsFile,
|
|
8
8
|
IntlCompiledMessageFormat,
|
|
@@ -23,8 +23,10 @@ const DEFAULT_LOCALE = 'en-US';
|
|
|
23
23
|
/**
|
|
24
24
|
* @param {string} filePath
|
|
25
25
|
* @param {string} assetExtension
|
|
26
|
+
* @param {import('./processing').IntlPrecompileOptions} [options]
|
|
26
27
|
*/
|
|
27
|
-
function processFile(filePath, assetExtension) {
|
|
28
|
+
function processFile(filePath, assetExtension, options = {}) {
|
|
29
|
+
const { format = IntlCompiledMessageFormat.KeylessJson, bundleSecrets = false } = options;
|
|
28
30
|
debug(`Processing file: ${filePath}`);
|
|
29
31
|
if (!isMessageDefinitionsFile(filePath)) {
|
|
30
32
|
debug(`${filePath} is not a definitions file. Skipping processing`);
|
|
@@ -35,17 +37,16 @@ function processFile(filePath, assetExtension) {
|
|
|
35
37
|
// Convert the file name from `.messages.js` to `.compiled.messages.jsona` for output.
|
|
36
38
|
const outputPath = filePath.replace(/\.messages\.js$/, `.compiled.messages.${assetExtension}`);
|
|
37
39
|
const result = processDefinitionsFile(filePath);
|
|
38
|
-
precompileFileForLocale(filePath, result.locale, {
|
|
39
|
-
format
|
|
40
|
+
precompileFileForLocale(filePath, result.locale, undefined, {
|
|
41
|
+
format,
|
|
42
|
+
bundleSecrets,
|
|
40
43
|
});
|
|
41
44
|
|
|
42
45
|
database.processDefinitionsFile(filePath);
|
|
43
|
-
database.precompile(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
IntlCompiledMessageFormat.KeylessJson,
|
|
48
|
-
);
|
|
46
|
+
database.precompile(filePath, DEFAULT_LOCALE, outputPath, {
|
|
47
|
+
format: IntlCompiledMessageFormat.KeylessJson,
|
|
48
|
+
bundleSecrets,
|
|
49
|
+
});
|
|
49
50
|
debug(`Wrote definitions to: ${outputPath}`);
|
|
50
51
|
} catch (e) {
|
|
51
52
|
debug('[INTL Error] Failed to compile messages');
|
|
@@ -59,11 +60,12 @@ function processFile(filePath, assetExtension) {
|
|
|
59
60
|
* watch?: boolean,
|
|
60
61
|
* ignore?: string[],
|
|
61
62
|
* assetExtension?: string
|
|
63
|
+
* precompileOptions?: import('./processing').IntlPrecompileOptions,
|
|
62
64
|
* }} options
|
|
63
65
|
*/
|
|
64
66
|
async function compileIntlMessageFiles(
|
|
65
67
|
watchedFolders,
|
|
66
|
-
{ watch = true, ignore = [], assetExtension = 'json' } = {},
|
|
68
|
+
{ watch = true, ignore = [], assetExtension = 'json', precompileOptions = {} } = {},
|
|
67
69
|
) {
|
|
68
70
|
const ignoredPatterns = ignore.concat(ALWAYS_IGNORE_PATTERNS);
|
|
69
71
|
const globs = watchedFolders.flatMap((folder) =>
|
|
@@ -79,7 +81,7 @@ async function compileIntlMessageFiles(
|
|
|
79
81
|
absolute: true,
|
|
80
82
|
onlyFiles: true,
|
|
81
83
|
})) {
|
|
82
|
-
processFile(filePath.toString(), assetExtension);
|
|
84
|
+
processFile(filePath.toString(), assetExtension, precompileOptions);
|
|
83
85
|
}
|
|
84
86
|
debug('Initial message scan completed.');
|
|
85
87
|
|
|
@@ -89,7 +91,7 @@ async function compileIntlMessageFiles(
|
|
|
89
91
|
.watch(globs, { ignored: ignoredPatterns, ignoreInitial: true })
|
|
90
92
|
.on('all', (event, filePath) => {
|
|
91
93
|
debug(`Got event ${event} for ${filePath}`);
|
|
92
|
-
processFile(filePath, assetExtension);
|
|
94
|
+
processFile(filePath, assetExtension, precompileOptions);
|
|
93
95
|
});
|
|
94
96
|
} else {
|
|
95
97
|
debug('Not watching files because `watch` option was false');
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IntlCompiledMessageFormat } from "@discord/intl-message-database";
|
|
2
|
+
import { MessageDefinitionsTransformer } from "./src/transformer";
|
|
3
|
+
import { database } from "./src/database";
|
|
4
|
+
import { findAllTranslationFiles } from "./src/util";
|
|
5
|
+
import { getLocaleFromTranslationsFileName } from "./src/util";
|
|
6
|
+
import { generateTypeDefinitions } from "./src/processing";
|
|
7
|
+
import { hashMessageKey } from "@discord/intl-message-database";
|
|
8
|
+
import { isMessageDefinitionsFile } from "@discord/intl-message-database";
|
|
9
|
+
import { isMessageTranslationsFile } from "@discord/intl-message-database";
|
|
10
|
+
import { processDefinitionsFile } from "./src/processing";
|
|
11
|
+
import { processTranslationsFile } from "./src/processing";
|
|
12
|
+
import { precompileFileForLocale } from "./src/processing";
|
|
13
|
+
import watcher = require("./src/watcher");
|
|
14
|
+
export { IntlCompiledMessageFormat, MessageDefinitionsTransformer, database, findAllTranslationFiles, getLocaleFromTranslationsFileName, generateTypeDefinitions, hashMessageKey, isMessageDefinitionsFile, isMessageTranslationsFile, processDefinitionsFile, processTranslationsFile, precompileFileForLocale, watcher };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export type IntlPrecompileOptions = {
|
|
2
|
+
format?: IntlCompiledMessageFormat;
|
|
3
|
+
bundleSecrets?: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Generate a `.d.ts` file containing TypeScript type definitions for all of the messages defined in
|
|
7
|
+
* `sourcePath`. This method does not process `sourcePath` at all, meaning it expects the database
|
|
8
|
+
* to already know about the source, as well as all of the related translations to create an
|
|
9
|
+
* accurate typescript definition for each message.
|
|
10
|
+
*
|
|
11
|
+
* If not given, `outputFile` will default to the same path as `sourcePath`, with the last extension
|
|
12
|
+
* replaced by `.d.ts`. For example, a file like `SomeMessages.Other.messages.js` would become
|
|
13
|
+
* `SomeMessages.Other.messages.d.ts`.
|
|
14
|
+
*
|
|
15
|
+
* If `allowNullability` is set, the generated types for variables within messages will allow
|
|
16
|
+
* `null` and `undefined` for most value types, as well as looser restrictions on typing, such as
|
|
17
|
+
* allowing `string | number` for number variables.
|
|
18
|
+
*
|
|
19
|
+
* Returns `true` if the types were successfully generated, or `false` otherwise, such as if the
|
|
20
|
+
* source file is not already in the database.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} sourcePath
|
|
23
|
+
* @param {string=} outputFile
|
|
24
|
+
* @param {boolean=} allowNullability
|
|
25
|
+
* @returns {boolean}
|
|
26
|
+
*/
|
|
27
|
+
export function generateTypeDefinitions(sourcePath: string, outputFile?: string | undefined, allowNullability?: boolean | undefined): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Precompile the messages defined in the given `sourcePath` using the value of the translation for
|
|
30
|
+
* that message in the given `locale`. `format` specifies which serialization format the result
|
|
31
|
+
* will be written in.
|
|
32
|
+
*
|
|
33
|
+
* By default, the compiled content will be returned as a Buffer containing the serialized string,
|
|
34
|
+
* but if `outputFile` is given then the content will be written directly to the file and the
|
|
35
|
+
* function becomes `void`.
|
|
36
|
+
*
|
|
37
|
+
* Compiling automatically handles filtering out messages based on the meta information like
|
|
38
|
+
* `translate`, `secret`, and `bundleSecrets`, to ensure that all consumers apply these values
|
|
39
|
+
* accurately and consistently.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} sourcePath
|
|
42
|
+
* @param {string} locale
|
|
43
|
+
* @param {string=} outputFile
|
|
44
|
+
* @param {IntlPrecompileOptions} [options]
|
|
45
|
+
*
|
|
46
|
+
* @returns {Buffer | void}
|
|
47
|
+
*/
|
|
48
|
+
export function precompileFileForLocale(sourcePath: string, locale: string, outputFile?: string | undefined, options?: IntlPrecompileOptions | undefined): Buffer | void;
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} sourcePath
|
|
51
|
+
* @param {string=} sourceContent
|
|
52
|
+
* @param {{
|
|
53
|
+
* processTranslations?: boolean,
|
|
54
|
+
* locale?: string
|
|
55
|
+
* }=} options
|
|
56
|
+
* @returns {import('../types').ProcessDefinitionsResult}
|
|
57
|
+
*/
|
|
58
|
+
export function processDefinitionsFile(sourcePath: string, sourceContent?: string | undefined, options?: {
|
|
59
|
+
processTranslations?: boolean;
|
|
60
|
+
locale?: string;
|
|
61
|
+
} | undefined): import("../types").ProcessDefinitionsResult;
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* @param {string} sourcePath
|
|
65
|
+
* @param {string=} sourceContent
|
|
66
|
+
* @param {{
|
|
67
|
+
* locale?: string,
|
|
68
|
+
* }=} options
|
|
69
|
+
* @returns {import('../types').ProcessTranslationsResult}
|
|
70
|
+
*/
|
|
71
|
+
export function processTranslationsFile(sourcePath: string, sourceContent?: string | undefined, options?: {
|
|
72
|
+
locale?: string;
|
|
73
|
+
} | undefined): import("../types").ProcessTranslationsResult;
|
|
74
|
+
import { IntlCompiledMessageFormat } from "@discord/intl-message-database";
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common class for parsing and transforming the content of a messages
|
|
3
|
+
* definition file (e.g., "SomeFeature.messages.js") into a production-ready
|
|
4
|
+
* version, with message keys obfuscated, loading harnesses configured, and
|
|
5
|
+
* more.
|
|
6
|
+
*
|
|
7
|
+
* This transformation is intended to be used alongside the _consumer_
|
|
8
|
+
* transforms implemented as SWC and Babel plugins, which transform the
|
|
9
|
+
* callsites for messages into matching formats. Consider this input example:
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // SomeModule.messages.js
|
|
13
|
+
* import {defineMessages} from '@discord/intl';
|
|
14
|
+
*
|
|
15
|
+
* export default defineMessages({
|
|
16
|
+
* THIS_IS_A_MESSAGE: 'it has some content with {values}',
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // SomeConsumer.tsx
|
|
20
|
+
* import someModuleMessages from 'SomeModule.messages.js';
|
|
21
|
+
* i18n.format(someModuleMessages.THIS_IS_A_MESSAGE, {values: "I'm a value!"});
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* This transformer will only handle `SomeModule.messages.js`, and will output
|
|
25
|
+
* something like:
|
|
26
|
+
*
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const {i18n} = require('@discord/intl');
|
|
29
|
+
* const _keys = ["a9fn23"];
|
|
30
|
+
* const _locales = {"en-US": () => require('./messages/en-US.messages.json')};
|
|
31
|
+
* export const messagesLoader = createLoader(_keys, _locales);
|
|
32
|
+
* export default messagesLoader.getBinds();
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* Notice how the message keys have been hashed into short keys, and the
|
|
36
|
+
* dynamic imports for each locale's data have been inserted automatically. In
|
|
37
|
+
* this example, the only locale is the source locale.
|
|
38
|
+
*
|
|
39
|
+
* The SWC and Babel transformers then take care of the second file,
|
|
40
|
+
* transforming the usage to use the hashed keys:
|
|
41
|
+
*
|
|
42
|
+
* ```typescript
|
|
43
|
+
* import someModuleMessages from 'SomeModule.messages.js';
|
|
44
|
+
* i18n.format(someModuleMessages["a9fn23"], {values: "i'm a value!"});
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* The transformed file also contains a named export for `messagesLoader`,
|
|
48
|
+
* which consumers can use to query and update the loading state for the
|
|
49
|
+
* messages managed by that loader, including waiting for a locale to be
|
|
50
|
+
* loaded, kicking off new loads, and more:
|
|
51
|
+
*
|
|
52
|
+
* ```typescript
|
|
53
|
+
* import {messagesLoader} from 'SomeModule.messages.js';
|
|
54
|
+
* // Wait for the loader to be initialized with default messages
|
|
55
|
+
* await messagesLoader.waitForDefaultLocaleLoaded();
|
|
56
|
+
* // Wait for a specific locale to load, starting the load if it
|
|
57
|
+
* // is not yet in progress.
|
|
58
|
+
* await messagesLoader.waitForLocaleLoaded('fr');
|
|
59
|
+
* // In hot-reloading environments, use the second `requireCurrent`
|
|
60
|
+
* // parameter to wait for the latest data, even if a value already
|
|
61
|
+
* // exists.
|
|
62
|
+
* const loaded = messagesLoader.isLocaleLoaded('fr', true);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export class MessageDefinitionsTransformer {
|
|
66
|
+
/**
|
|
67
|
+
* @param {import('../types.d.ts').MessageDefinitionsTransformerOptions} options
|
|
68
|
+
*/
|
|
69
|
+
constructor(options: import("../types.d.ts").MessageDefinitionsTransformerOptions);
|
|
70
|
+
options: import("../types.d.ts").MessageDefinitionsTransformerOptions;
|
|
71
|
+
loaderName: string;
|
|
72
|
+
/**
|
|
73
|
+
* Returns a compiled string for an object that maps locale names to a
|
|
74
|
+
* dynamic require function for that locale, based on the supported locales
|
|
75
|
+
* that were determined for this file. The shape ends up as:
|
|
76
|
+
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* {
|
|
79
|
+
* "en-US": () => import("path/to/en-US.json"),
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @returns {string}
|
|
84
|
+
*/
|
|
85
|
+
getLocaleRequireMap(): string;
|
|
86
|
+
/**
|
|
87
|
+
* Return a map of key hashes to their original values, as well as a plain-text map of locales
|
|
88
|
+
* to the file names that they import from.
|
|
89
|
+
*
|
|
90
|
+
* @returns {string[]}
|
|
91
|
+
*/
|
|
92
|
+
debugModeSetup(): string[];
|
|
93
|
+
/**
|
|
94
|
+
* Return the lines to export fields from this module, as determined by the `exportMode` on this
|
|
95
|
+
* transformer.
|
|
96
|
+
*/
|
|
97
|
+
exportFields(): string[];
|
|
98
|
+
/**
|
|
99
|
+
* Returns the reduced, transformed output for this file. Currently not
|
|
100
|
+
* configurable, but could be told to include default messages or preserve
|
|
101
|
+
* information as necessary.
|
|
102
|
+
*
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
getOutput(): string;
|
|
106
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const IGNORED_MESSAGE_FILE_PATTERNS: RegExp[];
|
|
2
|
+
/**
|
|
3
|
+
* Scan the given `translationsPath` to discover all translation files that exist, returning them
|
|
4
|
+
* as a map from locale name to the path for importing.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} translationsPath
|
|
7
|
+
* @returns {Record<string, string> | Error}
|
|
8
|
+
*/
|
|
9
|
+
export function findAllTranslationFiles(translationsPath: string): Record<string, string> | Error;
|
|
10
|
+
/**
|
|
11
|
+
* Return the presumed locale for a translations file from it's name. The convention follows the
|
|
12
|
+
* format: `some/path/to/<locale>.messages.jsona`, so the locale is determined by taking the content
|
|
13
|
+
* of the basename up until the first `.`.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} fileName
|
|
16
|
+
* @returns {string}
|
|
17
|
+
*/
|
|
18
|
+
export function getLocaleFromTranslationsFileName(fileName: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string[]} watchedFolders
|
|
3
|
+
* @param {{
|
|
4
|
+
* watch?: boolean,
|
|
5
|
+
* ignore?: string[],
|
|
6
|
+
* assetExtension?: string
|
|
7
|
+
* precompileOptions?: import('./processing').IntlPrecompileOptions,
|
|
8
|
+
* }} options
|
|
9
|
+
*/
|
|
10
|
+
export function compileIntlMessageFiles(watchedFolders: string[], { watch, ignore, assetExtension, precompileOptions }?: {
|
|
11
|
+
watch?: boolean;
|
|
12
|
+
ignore?: string[];
|
|
13
|
+
assetExtension?: string;
|
|
14
|
+
precompileOptions?: import("./processing").IntlPrecompileOptions;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
export const ALWAYS_IGNORE_PATTERNS: string[];
|
package/types/types.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { IntlSourceFile } from '@discord/intl-message-database';
|
|
2
|
+
|
|
3
|
+
export interface MessageDefinitionsTransformerOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The map of message keys that this file manages to their original values. By default, only the
|
|
6
|
+
* keys of this map are used (the hashed names), but in debug mode the values will also be
|
|
7
|
+
* included in the transformed file to provide context in errors and warnings.
|
|
8
|
+
*/
|
|
9
|
+
messageKeys: Record<string, string>;
|
|
10
|
+
/**
|
|
11
|
+
* Map of locale names to import paths used for loading translations.
|
|
12
|
+
*/
|
|
13
|
+
localeMap: Record<string, string>;
|
|
14
|
+
/**
|
|
15
|
+
* Default locale to use for the runtime loader. This is almost always the locale of the source
|
|
16
|
+
* file being transformed, but can be set explicitly to something else for special cases.
|
|
17
|
+
*/
|
|
18
|
+
defaultLocale: string;
|
|
19
|
+
/**
|
|
20
|
+
* Function to create a prelude that gets injected at the start of the transformed file to set up
|
|
21
|
+
* anything needed for other injections later on.
|
|
22
|
+
*/
|
|
23
|
+
getPrelude?: () => string;
|
|
24
|
+
/**
|
|
25
|
+
* Function to generate an import/require statement for the compiled asset file. All imports
|
|
26
|
+
* should be asynchronous (e.g., typically use `import` rather than `require`), but some platforms
|
|
27
|
+
* implement loading differently and may need different syntax. For example, React Native Assets
|
|
28
|
+
* are bundled using `require` statements to return an Asset ID, which can then be loaded
|
|
29
|
+
* asynchronously by some other code to get the actual content of the asset.
|
|
30
|
+
*
|
|
31
|
+
* The code created by this function must create a `Promise<{default: Record<string, any>}>`. In
|
|
32
|
+
* other words, a Promise for an object with a `default` key pointing to an object map of message
|
|
33
|
+
* keys to their values. For `import` statements, this is already the default. For `requires`, you
|
|
34
|
+
* may need to wrap the result with the `default` key, like `.then((data) => ({default: data}))`.
|
|
35
|
+
*/
|
|
36
|
+
getTranslationImport(importPath: string): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether to include additional information about keys and source files in the transformed loader
|
|
40
|
+
* code to provide context for debugging in errors and warning messages.
|
|
41
|
+
*/
|
|
42
|
+
debug?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Control how value exports are written in the transformed file to match what any downstream
|
|
45
|
+
* transformer or interpreter may expect. `esm` will leave exports as `export default` and other
|
|
46
|
+
* module features supported in ES6 and onward. `commonjs` will explicitly use `module.exports`
|
|
47
|
+
* with a single object containing `default` as the key for a default export. `transpiledEsModule`
|
|
48
|
+
* does the same, but includes the compatibility field `__esModule` to indicate that the module
|
|
49
|
+
* "was transpiled" to this syntax.
|
|
50
|
+
*
|
|
51
|
+
* @default esm
|
|
52
|
+
*/
|
|
53
|
+
exportMode?: 'esm' | 'commonjs' | 'transpiledEsModule';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The result of calling `processDefinitionsFile`, including the created source file, locale map,
|
|
58
|
+
* and more.
|
|
59
|
+
*/
|
|
60
|
+
export interface ProcessDefinitionsResult {
|
|
61
|
+
/**
|
|
62
|
+
* Direct source file from the database that was created or updated by this process.
|
|
63
|
+
*/
|
|
64
|
+
sourceFile: IntlSourceFile;
|
|
65
|
+
/**
|
|
66
|
+
* The locale that was either determined from the sourceFile name or overridden by the options
|
|
67
|
+
* provided to this call.
|
|
68
|
+
*/
|
|
69
|
+
locale: string;
|
|
70
|
+
/**
|
|
71
|
+
* The full map of message keys contained by the processed source file to their original values.
|
|
72
|
+
* While `sourceFile` contains a list of key _symbols_, this list contains all of the
|
|
73
|
+
* resolved strings for the hashed message keys.
|
|
74
|
+
*/
|
|
75
|
+
messageKeys: Record<string, string>;
|
|
76
|
+
/**
|
|
77
|
+
* Fully-resolved path to the translations directory that was scanned for entries for the
|
|
78
|
+
* source file.
|
|
79
|
+
*/
|
|
80
|
+
translationsPath: string;
|
|
81
|
+
/**
|
|
82
|
+
* Map of locale names to file paths for all translations files that were discovered when scanning
|
|
83
|
+
* the configured `translationsPath`. Note that this _does not_ include the source locale, since
|
|
84
|
+
* it's target is often different between loaders (e.g., could be a virtual file, an asset that
|
|
85
|
+
* gets compiled separately, or use query parameters to control loader behavior when reusing the
|
|
86
|
+
* same file).
|
|
87
|
+
*/
|
|
88
|
+
translationsLocaleMap: Record<string, string>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The result of calling `processTranslationsFile`, including the created source file, locale map,
|
|
93
|
+
* and more.
|
|
94
|
+
*/
|
|
95
|
+
export interface ProcessTranslationsResult {
|
|
96
|
+
/**
|
|
97
|
+
* Direct source file from the database that was created or updated by this process.
|
|
98
|
+
*/
|
|
99
|
+
sourceFile: IntlSourceFile;
|
|
100
|
+
/**
|
|
101
|
+
* The full map of message keys contained by the processed source file to their original values.
|
|
102
|
+
* While `sourceFile` contains a list of key _symbols_, this list contains all of the
|
|
103
|
+
* resolved strings for the hashed message keys.
|
|
104
|
+
*/
|
|
105
|
+
messageKeys: Record<string, string>;
|
|
106
|
+
/**
|
|
107
|
+
* The locale that was either determined from the sourceFile name or overridden by the options
|
|
108
|
+
* provided to this call.
|
|
109
|
+
*/
|
|
110
|
+
locale: string;
|
|
111
|
+
}
|
package/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
hashMessageKey,
|
|
3
|
-
isMessageDefinitionsFile,
|
|
4
|
-
isMessageTranslationsFile,
|
|
5
|
-
IntlCompiledMessageFormat,
|
|
6
|
-
} from '@discord/intl-message-database';
|
|
7
|
-
|
|
8
|
-
export { database } from './src/database';
|
|
9
|
-
export {
|
|
10
|
-
generateTypeDefinitions,
|
|
11
|
-
processDefinitionsFile,
|
|
12
|
-
processTranslationsFile,
|
|
13
|
-
precompileFileForLocale,
|
|
14
|
-
} from './src/processing';
|
|
15
|
-
export { MessageDefinitionsTransformer } from './src/transformer';
|
|
16
|
-
export { findAllTranslationFiles, getLocaleFromTranslationsFileName } from './src/util';
|
|
17
|
-
export * as watcher from './src/watcher';
|
|
File without changes
|