Skip to content

Commit 3e50e9d

Browse files
committed
feat: Add the feature of i18n fields.
1 parent 6839c2d commit 3e50e9d

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

plugins/userscript.plugin.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { readFileSync, writeFileSync } from 'fs';
22
import { join } from 'path';
33

4+
/**
5+
* I18n field of the userscript.
6+
*/
7+
type I18nField = { [locale: string]: string };
8+
49
/**
510
* Userscript's all headers.
611
*/
712
interface UserScriptOptions {
813
'require-template': string;
914
name: string;
15+
'i18n-names': I18nField;
1016
namespace: string;
1117
description: string;
18+
'i18n-descriptions': I18nField;
1219
version: string;
1320
author: string;
1421
homepage: string;
@@ -75,6 +82,14 @@ export function generateHeader() {
7582
} else {
7683
throw new Error('No name specified in package.json');
7784
}
85+
/**
86+
* Add userscript header's i18n-names.
87+
*/
88+
if (userscript['i18n-names'] && typeof userscript['i18n-names'] === 'object') {
89+
for (const [locale, name] of Object.entries(userscript['i18n-names'])) {
90+
headers.push(`// @name:${locale} ${name}`);
91+
}
92+
}
7893
/**
7994
* Add userscript header's version.
8095
* If the version is not set, the package version is used. If neither is set, an error is thrown.
@@ -92,6 +107,12 @@ export function generateHeader() {
92107
if (packageJson.description || userscript.description) {
93108
headers.push(`// @description ${userscript.description ?? packageJson.description}`);
94109
}
110+
// Add userscript header's i18n-descriptions.
111+
if (userscript['i18n-descriptions'] && typeof userscript['i18n-descriptions'] === 'object') {
112+
for (const [locale, description] of Object.entries(userscript['i18n-descriptions'])) {
113+
headers.push(`// @description:${locale} ${description}`);
114+
}
115+
}
95116
// Add userscript header's author.
96117
if (packageJson.author || userscript.author) {
97118
headers.push(`// @author ${userscript.author ?? packageJson.author}`);

0 commit comments

Comments
 (0)