@gunshi/plugin-i18n 0.35.1 → 0.35.3
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/lib/index.d.ts +36 -6
- package/lib/index.js +34 -5
- package/package.json +9 -9
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Args, Awaitable, Command, CommandContext, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, PluginWithExtension, Prettify } from "@gunshi/plugin";
|
|
2
2
|
|
|
3
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
3
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/resolver.d.ts
|
|
4
4
|
|
|
5
5
|
//#region src/resolver.d.ts
|
|
6
6
|
/**
|
|
@@ -422,7 +422,7 @@ interface ArgSchema {
|
|
|
422
422
|
*
|
|
423
423
|
* @param value - Raw string value from command line
|
|
424
424
|
* @returns Parsed value of any type
|
|
425
|
-
* @throws Error or subclass when value is invalid
|
|
425
|
+
* @throws {Error} Error or subclass when value is invalid
|
|
426
426
|
*
|
|
427
427
|
* @example
|
|
428
428
|
* Custom parsing functions:
|
|
@@ -454,6 +454,13 @@ interface ArgSchema {
|
|
|
454
454
|
*/
|
|
455
455
|
parse?: (value: string) => any;
|
|
456
456
|
}
|
|
457
|
+
/**
|
|
458
|
+
* Machine-readable error codes for {@link ArgsValidationError}.
|
|
459
|
+
*
|
|
460
|
+
* Each code identifies a validation failure category and is also suitable as an
|
|
461
|
+
* i18n resource key for localized rendering.
|
|
462
|
+
*/
|
|
463
|
+
|
|
457
464
|
/**
|
|
458
465
|
* An object that contains {@link ArgSchema | argument schema}.
|
|
459
466
|
*
|
|
@@ -535,6 +542,9 @@ type CommonArgType = {
|
|
|
535
542
|
};
|
|
536
543
|
declare const COMMON_ARGS: CommonArgType;
|
|
537
544
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
545
|
+
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"];
|
|
546
|
+
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found"];
|
|
547
|
+
declare const SUGGESTION_ERROR_RESOURCE_KEYS: readonly ["err:suggestion:did-you-mean"];
|
|
538
548
|
//#endregion
|
|
539
549
|
//#region ../gunshi/src/index.d.ts
|
|
540
550
|
|
|
@@ -565,14 +575,34 @@ type CommandBuiltinArgsKeys = keyof typeof COMMON_ARGS;
|
|
|
565
575
|
* Command i18n built-in resource keys.
|
|
566
576
|
*/
|
|
567
577
|
type CommandBuiltinResourceKeys = (typeof COMMAND_BUILTIN_RESOURCE_KEYS)[number];
|
|
578
|
+
/**
|
|
579
|
+
* Args validation error resource keys.
|
|
580
|
+
*/
|
|
581
|
+
type ArgErrorResourceKeys = (typeof ARG_ERROR_RESOURCE_KEYS)[number];
|
|
582
|
+
/**
|
|
583
|
+
* Command validation error resource keys.
|
|
584
|
+
*/
|
|
585
|
+
type CommandErrorResourceKeys = (typeof COMMAND_ERROR_RESOURCE_KEYS)[number];
|
|
586
|
+
/**
|
|
587
|
+
* Suggestion error resource keys.
|
|
588
|
+
*/
|
|
589
|
+
type SuggestionErrorResourceKeys = (typeof SUGGESTION_ERROR_RESOURCE_KEYS)[number];
|
|
590
|
+
/**
|
|
591
|
+
* Error resource keys.
|
|
592
|
+
*/
|
|
593
|
+
type ErrorResourceKeys = ArgErrorResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys;
|
|
568
594
|
/**
|
|
569
595
|
* Built-in resource keys.
|
|
570
596
|
*/
|
|
571
|
-
type BuiltinResourceKeys = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
597
|
+
type BuiltinResourceKeys = ErrorResourceKeys | CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
598
|
+
/**
|
|
599
|
+
* Built-in resource.
|
|
600
|
+
*/
|
|
601
|
+
type BuiltinResource = Partial<Record<BuiltinResourceKeys, string>>;
|
|
572
602
|
/**
|
|
573
603
|
* Command built-in keys.
|
|
574
604
|
*/
|
|
575
|
-
type CommandBuiltinKeys = GenerateNamespacedKey<
|
|
605
|
+
type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinArgsKeys | CommandBuiltinResourceKeys>;
|
|
576
606
|
/**
|
|
577
607
|
* Command i18n option keys.
|
|
578
608
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
@@ -589,7 +619,7 @@ E extends Record<string, string> = {},
|
|
|
589
619
|
// for extended resources
|
|
590
620
|
R extends string = keyof RemovedIndex<E>, T extends string = (C extends {
|
|
591
621
|
name: infer N;
|
|
592
|
-
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = CommandBuiltinKeys | O | T;
|
|
622
|
+
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = ErrorResourceKeys | CommandBuiltinKeys | O | T;
|
|
593
623
|
//#endregion
|
|
594
624
|
//#region ../shared/src/localization.d.ts
|
|
595
625
|
|
|
@@ -705,7 +735,7 @@ interface I18nPluginOptions {
|
|
|
705
735
|
/**
|
|
706
736
|
* Built-in localizable resources
|
|
707
737
|
*/
|
|
708
|
-
builtinResources?: Record<string,
|
|
738
|
+
builtinResources?: Record<string, BuiltinResource>;
|
|
709
739
|
}
|
|
710
740
|
/**
|
|
711
741
|
* Translation adapter factory.
|
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { plugin } from "@gunshi/plugin";
|
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
const ARG_PREFIX_AND_KEY_SEPARATOR = `arg:`;
|
|
7
|
+
const ERROR_PREFIX_AND_KEY_SEPARATOR = `err:`;
|
|
7
8
|
const COMMON_ARGS = {
|
|
8
9
|
help: {
|
|
9
10
|
type: "boolean",
|
|
@@ -31,7 +32,15 @@ var en_US_default = {
|
|
|
31
32
|
DEFAULT: "default",
|
|
32
33
|
CHOICES: "choices",
|
|
33
34
|
help: "Display this help message",
|
|
34
|
-
version: "Display this version"
|
|
35
|
+
version: "Display this version",
|
|
36
|
+
"err:arg:required-option": "Optional argument {$displayName} is required",
|
|
37
|
+
"err:arg:required-positional": "Positional argument {$name} is required",
|
|
38
|
+
"err:arg:invalid-type": "Invalid value for {$displayName}: expected {$expected}",
|
|
39
|
+
"err:arg:invalid-choice": "Invalid value for {$displayName}: expected one of {$choices}",
|
|
40
|
+
"err:arg:custom-parse": "Invalid value for {$displayName}: {$reason}",
|
|
41
|
+
"err:arg:unknown-option": "Unknown option: {$rawName}",
|
|
42
|
+
"err:cmd:not-found": "Command not found: {$commandName}",
|
|
43
|
+
"err:suggestion:did-you-mean": "Did you mean {$name}?"
|
|
35
44
|
};
|
|
36
45
|
//#endregion
|
|
37
46
|
//#region ../shared/src/utils.ts
|
|
@@ -110,6 +119,18 @@ function namespacedId(id) {
|
|
|
110
119
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
111
120
|
* @license MIT
|
|
112
121
|
*/
|
|
122
|
+
/**
|
|
123
|
+
* Format a resource message by replacing placeholder values.
|
|
124
|
+
*
|
|
125
|
+
* @param resource - Resource message template
|
|
126
|
+
* @param values - Placeholder values
|
|
127
|
+
* @returns Formatted resource message
|
|
128
|
+
*/
|
|
129
|
+
function formatResource(resource, values = Object.create(null)) {
|
|
130
|
+
return resource?.replaceAll(/\{\$(\w+)\}/g, (_, name) => {
|
|
131
|
+
return values[name] == null ? "" : String(values[name]);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
113
134
|
//#endregion
|
|
114
135
|
//#region ../shared/src/index.ts
|
|
115
136
|
/**
|
|
@@ -388,8 +409,11 @@ function i18n(options = {}) {
|
|
|
388
409
|
}
|
|
389
410
|
function translate(key, values = Object.create(null)) {
|
|
390
411
|
const strKey = key;
|
|
391
|
-
if (strKey
|
|
392
|
-
|
|
412
|
+
if (isBuiltinResourceKey(strKey)) {
|
|
413
|
+
const resource = localeBuiltinResources.get(localeStr);
|
|
414
|
+
const defaultResource = localeBuiltinResources.get(DEFAULT_LOCALE);
|
|
415
|
+
return formatResource(resource?.[strKey] ?? defaultResource[strKey], values) || strKey;
|
|
416
|
+
} else return adapter.translate(localeStr, strKey, values) || "";
|
|
393
417
|
}
|
|
394
418
|
function setResource(locale, resource) {
|
|
395
419
|
const targetLocaleStr = toLocaleString(toLocale(locale));
|
|
@@ -404,7 +428,8 @@ function i18n(options = {}) {
|
|
|
404
428
|
}
|
|
405
429
|
for (const [locale, resource] of Object.entries(builtinResources)) for (const globalOption of builtinGlobalOptions) {
|
|
406
430
|
const globalOptionResource = builtinGlobalOptionResources[globalOption];
|
|
407
|
-
|
|
431
|
+
const message = resource[globalOption];
|
|
432
|
+
if (message) globalOptionResource[locale] = message;
|
|
408
433
|
}
|
|
409
434
|
for (const globalOption of builtinGlobalOptions) registerGlobalOptionResources(globalOption, builtinGlobalOptionResources[globalOption]);
|
|
410
435
|
setResource(DEFAULT_LOCALE, en_US_default);
|
|
@@ -461,10 +486,14 @@ async function loadCommandResource(locale, command) {
|
|
|
461
486
|
}
|
|
462
487
|
function mapResourceWithBuiltinKey(resource, ctx, globalOptions) {
|
|
463
488
|
return Object.entries(resource).reduce((acc, [key, value]) => {
|
|
464
|
-
|
|
489
|
+
if (value == null) return acc;
|
|
490
|
+
acc[key.startsWith(ERROR_PREFIX_AND_KEY_SEPARATOR) ? key : globalOptions.includes(key) ? resolveArgKey(key, ctx.name) : resolveBuiltInKey(key)] = value;
|
|
465
491
|
return acc;
|
|
466
492
|
}, Object.create(null));
|
|
467
493
|
}
|
|
494
|
+
function isBuiltinResourceKey(key) {
|
|
495
|
+
return key.codePointAt(0) === BUILT_IN_PREFIX_CODE || key.startsWith(ERROR_PREFIX_AND_KEY_SEPARATOR);
|
|
496
|
+
}
|
|
468
497
|
function hasI18nResource(command) {
|
|
469
498
|
return "resource" in command && typeof command.resource === "function";
|
|
470
499
|
}
|
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": "0.35.
|
|
4
|
+
"version": "0.35.3",
|
|
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": "0.35.
|
|
56
|
+
"@gunshi/plugin": "0.35.3"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@gunshi/plugin-global": "0.35.
|
|
59
|
+
"@gunshi/plugin-global": "0.35.3"
|
|
60
60
|
},
|
|
61
61
|
"peerDependenciesMeta": {
|
|
62
62
|
"@gunshi/plugin-global": {
|
|
@@ -71,12 +71,12 @@
|
|
|
71
71
|
"messageformat": "4.0.0",
|
|
72
72
|
"publint": "^0.3.20",
|
|
73
73
|
"tsdown": "0.21.0",
|
|
74
|
-
"@gunshi/bone": "0.35.
|
|
75
|
-
"@gunshi/plugin-global": "0.35.
|
|
76
|
-
"@gunshi/plugin-renderer": "0.35.
|
|
77
|
-
"@gunshi/resources": "0.35.
|
|
78
|
-
"@gunshi/shared": "0.35.
|
|
79
|
-
"gunshi": "0.35.
|
|
74
|
+
"@gunshi/bone": "0.35.3",
|
|
75
|
+
"@gunshi/plugin-global": "0.35.3",
|
|
76
|
+
"@gunshi/plugin-renderer": "0.35.3",
|
|
77
|
+
"@gunshi/resources": "0.35.3",
|
|
78
|
+
"@gunshi/shared": "0.35.3",
|
|
79
|
+
"gunshi": "0.35.3"
|
|
80
80
|
},
|
|
81
81
|
"scripts": {
|
|
82
82
|
"build": "tsdown",
|