Skip to content

Commit

Permalink
refactor: remove aliases and update i18n for schema
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan committed Apr 1, 2024
1 parent cea034f commit 7330669
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import { Schema } from 'koishi'
import { Locale, locales } from './utils'

export interface Config {
aliases: string[]
defaultLanguage: Locale
defaultMode: 'auto' | 'text'
threshold: number
}

export const Config: Schema<Config> = Schema.object({
aliases: Schema.array(Schema.string()).default([]),
defaultLanguage: Schema.union(locales).default('en'),
defaultMode: Schema.union(['auto', 'text'] as const).default('auto'),
defaultMode: Schema.union([Schema.const('auto'), Schema.const('text')])
.role('radio')
.default('auto'),
threshold: Schema.number().default(3),
}).i18n({
'zh': require('./locales/zh-CN/macrodict.schema.yml'),
'zh-CN': require('./locales/zh-CN/macrodict.schema.yml'),
'en': require('./locales/en-US/macrodict.schema.yml'),
})
5 changes: 0 additions & 5 deletions src/i18n/index.ts

This file was deleted.

14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, h } from 'koishi'
import { Context, h, omit } from 'koishi'

import { Config } from './config'
import i18n from './i18n'
import * as i18n from './locales'
import { parseMacroDescription } from './parser'
import { Search } from './search'
import { Locale, locales } from './utils'
Expand All @@ -19,13 +19,19 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
})

// register i18n resources
Object.entries(i18n).forEach(([key, value]) => ctx.i18n.define(key, value))
ctx.plugin(i18n)

ctx.plugin(Search)

// remove obsolete `aliases` configuration
if (typeof (ctx.config as any).aliases !== 'undefined') {
ctx.setTimeout(() => {
ctx.scope.update(omit(ctx.config, 'aliases'))
}, 0)
}

ctx
.command('macrodict <macro>')
.alias(...config.aliases)
.channelFields(['macrodict'])
.option('lang', '-l <language:string>')
.option('imageMode', '-i')
Expand Down
15 changes: 15 additions & 0 deletions src/locales/en-US/macrodict.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defaultLanguage:
$description: Default output language, can be overridden by the -l option
$value:
- English
- German
- French
- Japanese
- Korean
- Simplified Chinese
defaultMode:
$description: Default output mode
$value:
- Prefer to output image
- Always output text
threshold: Distance threshold for fuzzy search text commands
File renamed without changes.
7 changes: 7 additions & 0 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Context } from 'koishi'

export function apply(ctx: Context) {
ctx.i18n.define('en', require('./en-US/macrodict.yml'))
ctx.i18n.define('zh', require('./zh-CN/macrodict.yml'))
ctx.i18n.define('zh-CN', require('./zh-CN/macrodict.yml'))
}
15 changes: 15 additions & 0 deletions src/locales/zh-CN/macrodict.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defaultLanguage:
$description: 默认输出语言,可被 -l 选项覆盖
$value:
- 英文
- 德文
- 法文
- 日文
- 韩文
- 简体中文
defaultMode:
$description: 默认输出模式
$value:
- 优先输出图片
- 总是输出文本
threshold: 模糊搜索文本指令的距离阈值
File renamed without changes.

0 comments on commit 7330669

Please sign in to comment.