@gunshi/plugin-i18n 1.0.0-rc.5 → 1.0.0-rc.6
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/README.md +1 -0
- package/lib/index.d.ts +17 -6
- package/lib/index.js +42 -0
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -108,6 +108,7 @@ This means:
|
|
|
108
108
|
- If you're using the globals plugin (which adds `--help` and `--version` options), the i18n plugin will automatically handle their translations
|
|
109
109
|
- The translations for "Display this help message" and "Display this version" will be available in your configured locale
|
|
110
110
|
- You can override these translations by providing your own `help` and `version` keys in your built-in resources
|
|
111
|
+
- The description of `--json` of a command that spreads `jsonArgs` ("Output the result as JSON") is translated by the `json` key of the built-in resources in the same way, unless the resource of the command has its own `arg:json`
|
|
111
112
|
|
|
112
113
|
Example with globals plugin:
|
|
113
114
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Args, Awaitable, Command, CommandContext, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, PluginWithExtension, Prettify } from "@gunshi/plugin";
|
|
1
|
+
import { Args, Awaitable, Command, CommandContext, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, PluginWithExtension, Prettify, SubCommandable } from "@gunshi/plugin";
|
|
2
2
|
//#region ../../node_modules/.pnpm/args-tokens@1.0.2/node_modules/args-tokens/lib/resolver.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -623,11 +623,13 @@ type CommonArgType = {
|
|
|
623
623
|
};
|
|
624
624
|
};
|
|
625
625
|
declare const COMMON_ARGS: CommonArgType;
|
|
626
|
-
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
626
|
+
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES", "CAUSEDBY"];
|
|
627
627
|
declare const ARG_ERROR_RESOURCE_KEYS: readonly ["err:arg:required-option", "err:arg:required-positional", "err:arg:invalid-type", "err:arg:invalid-choice", "err:arg:custom-parse", "err:arg:unknown-option", "err:arg:unexpected-value", "err:arg:missing-value", "err:arg:conflict", "err:arg:invalid-default"];
|
|
628
628
|
declare const ARG_ERROR_HINT_RESOURCE_KEYS: readonly ["err:arg:missing-value:hint"];
|
|
629
629
|
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found", "err:cmd:ambiguous", "err:cmd:inconsistent-options", "err:cmd:lazy-schema-mismatch"];
|
|
630
630
|
declare const SUGGESTION_ERROR_RESOURCE_KEYS: readonly ["err:suggestion:did-you-mean"];
|
|
631
|
+
declare const JSON_RESOURCE_KEYS: readonly ["json", "json:invalid-arguments", "json:next:help", "json:next:help-placeholder"];
|
|
632
|
+
declare const JSON_ERROR_RESOURCE_KEYS: readonly ["err:json:not-supported", "err:json:invalid-result", "err:json:unserializable-result"];
|
|
631
633
|
//#endregion
|
|
632
634
|
//#region ../shared/src/types.d.ts
|
|
633
635
|
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K]; };
|
|
@@ -667,14 +669,23 @@ type CommandErrorResourceKeys = (typeof COMMAND_ERROR_RESOURCE_KEYS)[number];
|
|
|
667
669
|
* Suggestion error resource keys.
|
|
668
670
|
*/
|
|
669
671
|
type SuggestionErrorResourceKeys = (typeof SUGGESTION_ERROR_RESOURCE_KEYS)[number];
|
|
672
|
+
/**
|
|
673
|
+
* Resource keys of the errors of the JSON output.
|
|
674
|
+
*/
|
|
675
|
+
type JsonErrorResourceKeys = (typeof JSON_ERROR_RESOURCE_KEYS)[number];
|
|
676
|
+
/**
|
|
677
|
+
* Resource keys of the description of the `json` argument of `jsonArgs`, and of the texts that only
|
|
678
|
+
* the JSON errors print.
|
|
679
|
+
*/
|
|
680
|
+
type JsonResourceKeys = (typeof JSON_RESOURCE_KEYS)[number];
|
|
670
681
|
/**
|
|
671
682
|
* Error resource keys.
|
|
672
683
|
*/
|
|
673
|
-
type ErrorResourceKeys = ArgErrorResourceKeys | ArgErrorHintResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys;
|
|
684
|
+
type ErrorResourceKeys = ArgErrorResourceKeys | ArgErrorHintResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys | JsonErrorResourceKeys;
|
|
674
685
|
/**
|
|
675
686
|
* Built-in resource keys.
|
|
676
687
|
*/
|
|
677
|
-
type BuiltinResourceKeys = ErrorResourceKeys | CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
688
|
+
type BuiltinResourceKeys = ErrorResourceKeys | CommandBuiltinArgsKeys | CommandBuiltinResourceKeys | JsonResourceKeys;
|
|
678
689
|
/**
|
|
679
690
|
* Built-in resource.
|
|
680
691
|
*/
|
|
@@ -916,7 +927,7 @@ type I18nCommandDefinitionResult<G extends GunshiParamsConstraint = DefaultGunsh
|
|
|
916
927
|
*
|
|
917
928
|
* @internal
|
|
918
929
|
*/
|
|
919
|
-
type WithI18nResourceResult<G extends GunshiParamsConstraint = DefaultGunshiParams, C extends Command<G> = Command<G>> = Prettify<C & {
|
|
930
|
+
type WithI18nResourceResult<G extends GunshiParamsConstraint = DefaultGunshiParams, C extends Command<G> | SubCommandable = Command<G>> = Prettify<C & {
|
|
920
931
|
resource: CommandResourceFetcher<G>;
|
|
921
932
|
} & { [K in Exclude<keyof I18nCommand<G>, keyof C | 'resource'>]?: I18nCommand<G>[K]; }>;
|
|
922
933
|
/**
|
|
@@ -1065,7 +1076,7 @@ declare function defineI18nWithTypes<G extends GunshiParamsConstraint>(): Define
|
|
|
1065
1076
|
* @param resource - A {@link CommandResourceFetcher | resource fetcher} for the command
|
|
1066
1077
|
* @returns A {@link I18nCommand | command} with i18n resource support
|
|
1067
1078
|
*/
|
|
1068
|
-
declare function withI18nResource<G extends GunshiParamsConstraint, C extends Command<G
|
|
1079
|
+
declare function withI18nResource<G extends GunshiParamsConstraint, C extends Command<G> | SubCommandable>(command: C, resource: CommandResourceFetcher<G>): WithI18nResourceResult<G, C>;
|
|
1069
1080
|
//#endregion
|
|
1070
1081
|
//#region src/translation.d.ts
|
|
1071
1082
|
/**
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,25 @@ import { plugin } from "@gunshi/plugin";
|
|
|
3
3
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region ../gunshi/src/utils.ts
|
|
8
|
+
/**
|
|
9
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
10
|
+
* @license MIT
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Check whether an argument schema is the `json` argument of `jsonArgs`, by its mark.
|
|
14
|
+
*
|
|
15
|
+
* @param schema - An argument schema
|
|
16
|
+
* @returns `true` if the schema has the mark of `jsonArgs`
|
|
17
|
+
*/
|
|
18
|
+
function isJsonArg(schema) {
|
|
19
|
+
return typeof schema === "object" && schema !== null && schema["~gunshi.json-arg"] === true;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
23
|
+
* @license MIT
|
|
24
|
+
*/
|
|
6
25
|
const ARG_PREFIX_AND_KEY_SEPARATOR = `arg:`;
|
|
7
26
|
const ERROR_PREFIX_AND_KEY_SEPARATOR = `err:`;
|
|
8
27
|
const COMMON_ARGS = {
|
|
@@ -31,8 +50,13 @@ var en_US_default = {
|
|
|
31
50
|
NEGATABLE: "Negatable of",
|
|
32
51
|
DEFAULT: "default",
|
|
33
52
|
CHOICES: "choices",
|
|
53
|
+
CAUSEDBY: "Caused by",
|
|
34
54
|
help: "Display this help message",
|
|
35
55
|
version: "Display this version",
|
|
56
|
+
json: "Output the result as JSON",
|
|
57
|
+
"json:invalid-arguments": "{$count} arguments are invalid",
|
|
58
|
+
"json:next:help": "See the arguments of the command",
|
|
59
|
+
"json:next:help-placeholder": "Replace <COMMAND> with how you ran this CLI, to see the arguments of the command",
|
|
36
60
|
"err:arg:required-option": "Option {$displayName} is required",
|
|
37
61
|
"err:arg:required-positional": "Positional argument {$displayName} is required",
|
|
38
62
|
"err:arg:invalid-type": "Invalid value for {$displayName}: expected {$expected}",
|
|
@@ -48,6 +72,9 @@ var en_US_default = {
|
|
|
48
72
|
"err:cmd:ambiguous": "The command is ambiguous. Move options after the command name: {$candidatePaths}",
|
|
49
73
|
"err:cmd:inconsistent-options": "The options do not identify a consistent command. Move options after the command name.",
|
|
50
74
|
"err:cmd:lazy-schema-mismatch": "The lazy command definition does not match its loaded options. Declare leading options in the lazy definition or move options after the command name: {$commandPath}",
|
|
75
|
+
"err:json:not-supported": "This command does not support --json",
|
|
76
|
+
"err:json:invalid-result": "The command finished, but did not return an object to output as JSON. A plugin that decorates the command may have dropped its result",
|
|
77
|
+
"err:json:unserializable-result": "The command finished, but its result cannot be converted to JSON: {$message}",
|
|
51
78
|
"err:suggestion:did-you-mean": "Did you mean {$name}?"
|
|
52
79
|
};
|
|
53
80
|
//#endregion
|
|
@@ -139,6 +166,11 @@ function formatResource(resource, values = Object.create(null)) {
|
|
|
139
166
|
return values[name] == null ? "" : String(values[name]);
|
|
140
167
|
});
|
|
141
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
171
|
+
* @license MIT
|
|
172
|
+
*/
|
|
173
|
+
namespacedId("i18n");
|
|
142
174
|
//#endregion
|
|
143
175
|
//#region ../shared/src/index.ts
|
|
144
176
|
/**
|
|
@@ -449,6 +481,16 @@ function i18n(options = {}) {
|
|
|
449
481
|
if (originalResource) loaded = true;
|
|
450
482
|
const localeStr = toLocaleString(locale);
|
|
451
483
|
const resource = originalResource ? await normalizeResource(originalResource, ctx) : Object.create(null);
|
|
484
|
+
/**
|
|
485
|
+
* NOTE(kazupon): the `json` argument of `jsonArgs` is described by the built-in resources, as the
|
|
486
|
+
* global options are, unless the resource of the command describes it. A command without a
|
|
487
|
+
* translation of its own keeps the English description of `jsonArgs`.
|
|
488
|
+
*/
|
|
489
|
+
if (isJsonArg(ctx.args.json)) {
|
|
490
|
+
const jsonKey = resolveArgKey("json", ctx.name);
|
|
491
|
+
const description = builtinResources[localeStr]?.json;
|
|
492
|
+
if (resource[jsonKey] === void 0 && description) resource[jsonKey] = description;
|
|
493
|
+
}
|
|
452
494
|
for (const globalOption of getGlobalOptions()) if (globalOptionResources.has(globalOption)) {
|
|
453
495
|
const optionResource = globalOptionResources.get(globalOption);
|
|
454
496
|
const globalOptionKey = resolveArgKey(globalOption, ctx.name);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/plugin-i18n",
|
|
3
3
|
"description": "internationalization plugin for gunshi",
|
|
4
|
-
"version": "1.0.0-rc.
|
|
4
|
+
"version": "1.0.0-rc.6",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -53,10 +53,10 @@
|
|
|
53
53
|
}
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@gunshi/plugin": "1.0.0-rc.
|
|
56
|
+
"@gunshi/plugin": "1.0.0-rc.6"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@gunshi/plugin-global": "1.0.0-rc.
|
|
59
|
+
"@gunshi/plugin-global": "1.0.0-rc.6"
|
|
60
60
|
},
|
|
61
61
|
"peerDependenciesMeta": {
|
|
62
62
|
"@gunshi/plugin-global": {
|
|
@@ -64,13 +64,13 @@
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@gunshi/bone": "1.0.0-rc.
|
|
68
|
-
"@gunshi/plugin-global": "1.0.0-rc.
|
|
69
|
-
"@gunshi/plugin-renderer": "1.0.0-rc.
|
|
70
|
-
"@gunshi/resources": "1.0.0-rc.
|
|
71
|
-
"@gunshi/shared": "1.0.0-rc.
|
|
67
|
+
"@gunshi/bone": "1.0.0-rc.6",
|
|
68
|
+
"@gunshi/plugin-global": "1.0.0-rc.6",
|
|
69
|
+
"@gunshi/plugin-renderer": "1.0.0-rc.6",
|
|
70
|
+
"@gunshi/resources": "1.0.0-rc.6",
|
|
71
|
+
"@gunshi/shared": "1.0.0-rc.6",
|
|
72
72
|
"@intlify/core-base": "12.0.0-alpha.7",
|
|
73
|
-
"gunshi": "1.0.0-rc.
|
|
73
|
+
"gunshi": "1.0.0-rc.6",
|
|
74
74
|
"messageformat": "4.1.0",
|
|
75
75
|
"publint": "^0.3.20",
|
|
76
76
|
"tsdown": "0.22.14"
|