Skip to content

Commit

Permalink
✨ Feature: finish i18n text
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Aug 20, 2022
1 parent a649fcc commit 11b3197
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
"upyun",
"weibo"
],
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@types/fs-extra": "^5.0.4",
"@types/image-size": "^0.0.29",
"@types/inquirer": "^0.0.42",
"@types/js-yaml": "^4.0.5",
"@types/lodash": "^4.14.175",
"@types/md5": "^2.1.32",
"@types/mime-types": "^2.1.0",
Expand Down Expand Up @@ -106,6 +107,7 @@
"image-size": "^0.8.3",
"inquirer": "^6.0.0",
"is-wsl": "^2.2.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"md5": "^2.2.1",
"mime-types": "2.1.33",
Expand Down
1 change: 0 additions & 1 deletion src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export class PicGo extends EventEmitter implements IPicGo {

getConfig<T> (name?: string): T {
if (!name) {
console.log(this._config)
return this._config as unknown as T
} else {
return get(this._config, name)
Expand Down
24 changes: 24 additions & 0 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const EN: ILocales = {
PICBED_ALICLOUD_PATH: 'Set Path',
PICBED_ALICLOUD_CUSTOMURL: 'Set Custom URL',
PICBED_ALICLOUD_OPTIONS: 'Set URL Suffix',
PICBED_ALICLOUD_MESSAGE_AREA: 'Ex. oss-cn-beijing',
PICBED_ALICLOUD_MESSAGE_PATH: 'Ex. test/',
PICBED_ALICLOUD_MESSAGE_OPTIONS: 'Ex. ?x-oss-process=xxx',
PICBED_ALICLOUD_MESSAGE_CUSTOMURL: 'Ex. https://test.com',

// Tencent-cloud
PICBED_TENCENTCLOUD: 'Tencent Cloud',
Expand All @@ -32,7 +36,13 @@ export const EN: ILocales = {
PICBED_TENCENTCLOUD_BUCKET: 'Set Bucket',
PICBED_TENCENTCLOUD_AREA: 'Set Area',
PICBED_TENCENTCLOUD_PATH: 'Set Path',
PICBED_TENCENTCLOUD_OPTIONS: 'Set URL Suffix',
PICBED_TENCENTCLOUD_CUSTOMURL: 'Set Custom URL',
PICBED_TENCENTCLOUD_MESSAGE_APPID: 'Ex. 1234567890',
PICBED_TENCENTCLOUD_MESSAGE_AREA: 'Ex. ap-beijing',
PICBED_TENCENTCLOUD_MESSAGE_PATH: 'Ex. test/',
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: 'Ex. http://test.com',
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: 'Ex. ?imageMogr2',

// GitHub
PICBED_GITHUB: 'GitHub',
Expand All @@ -41,6 +51,10 @@ export const EN: ILocales = {
PICBED_GITHUB_PATH: 'Set Path',
PICBED_GITHUB_BRANCH: 'Set Branch',
PICBED_GITHUB_CUSTOMURL: 'Set Custom URL',
PICBED_GITHUB_MESSAGE_REPO: 'Ex. username/repo',
PICBED_GITHUB_MESSAGE_BRANCH: 'Ex. main',
PICBED_GITHUB_MESSAGE_PATH: 'Ex. test/',
PICBED_GITHUB_MESSAGE_CUSTOMURL: 'Ex. https://test.com',

// qiniu
PICBED_QINIU: 'Qiniu',
Expand All @@ -51,11 +65,16 @@ export const EN: ILocales = {
PICBED_QINIU_URL: 'Set URL',
PICBED_QINIU_OPTIONS: 'Set URL Suffix',
PICBED_QINIU_AREA: 'Set Area',
PICBED_QINIU_MESSAGE_PATH: 'Ex. test/',
PICBED_QINIU_MESSAGE_AREA: 'Ex. z0',
PICBED_QINIU_MESSAGE_OPTIONS: 'Ex. ?imageslim',
PICBED_QINIU_MESSAGE_URL: 'Ex. https://xxx.yyy.glb.clouddn.com',

// imgur
PICBED_IMGUR: 'Imgur',
PICBED_IMGUR_CLIENTID: 'Set ClientId',
PICBED_IMGUR_PROXY: 'Set Proxy',
PICBED_IMGUR_MESSAGE_PROXY: 'Ex. http://127.0.0.1:1080',

// upyun
PICBED_UPYUN: 'Upyun',
Expand All @@ -65,6 +84,11 @@ export const EN: ILocales = {
PICBED_UPYUN_PATH: 'Set Path',
PICBED_UPYUN_URL: 'Set URL',
PICBED_UPYUN_OPTIONS: 'Set URL Suffix',
PICBED_UPYUN_MESSAGE_OPERATOR: 'Ex. me',
PICBED_UPYUN_MESSAGE_PASSWORD: 'Please type the operator password',
PICBED_UPYUN_MESSAGE_URL: 'Ex. http://xxx.test.upcdn.net',
PICBED_UPYUN_MESSAGE_OPTIONS: 'Ex. !imgslim',
PICBED_UPYUN_MESSAGE_PATH: 'Ex. test/',

// Plugin Handler
PLUGIN_HANDLER_PLUGIN_INSTALL_SUCCESS: 'Plugin installed successfully',
Expand Down
40 changes: 37 additions & 3 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ZH_CN, ILocalesKey } from './zh-CN'
import { ZH_CN, ILocalesKey, ILocales } from './zh-CN'
import { merge } from 'lodash'
import { IPicGo } from '../types'
import path from 'path'
import fs from 'fs-extra'
import yaml from 'js-yaml'

import { ObjectAdapter, I18n } from '@picgo/i18n'
import { IStringKeyMap, II18nManager } from '../types/index'
Expand All @@ -19,15 +22,46 @@ class I18nManager implements II18nManager {
constructor (ctx: IPicGo) {
this.ctx = ctx
this.objectAdapter = new ObjectAdapter(languageList)
const language = this.ctx.getConfig<string>('settings.language') || 'zh-CN'
let language = this.ctx.getConfig<string>('settings.language') || 'zh-CN'
if (!languageList[language]) {
language = 'zh-CN' // use default
}
this.i18n = new I18n({
adapter: this.objectAdapter,
defaultLanguage: language
})
this.loadOutterI18n()
}

private loadOutterI18n (): void {
const i18nFolder = this.getOutterI18nFolder()
const files = fs.readdirSync(i18nFolder, {
withFileTypes: true
})
files.forEach(file => {
if (file.isFile() && file.name.endsWith('.yml')) {
const i18nFilePath = path.join(i18nFolder, file.name)
const i18nFile = fs.readFileSync(i18nFilePath, 'utf8')
try {
const i18nFileObj = yaml.load(i18nFile) as ILocales
languageList[file.name.replace(/\.yml$/, '')] = i18nFileObj
} catch (e) {
console.error(e)
}
}
})
}

private getOutterI18nFolder (): string {
const i18nFolder = path.join(this.ctx.baseDir, 'i18n-cli')
if (!fs.pathExistsSync(i18nFolder)) {
fs.ensureDirSync(i18nFolder)
}
return i18nFolder
}

translate<T extends string>(key: ILocalesKey | T, args?: IStringKeyMap<string>): string {
return this.i18n.translate(key, args) || ''
return this.i18n.translate(key, args) || key
}

setLanguage (language: string): void {
Expand Down
24 changes: 24 additions & 0 deletions src/i18n/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const ZH_CN = {
PICBED_ALICLOUD_PATH: '设定存储路径',
PICBED_ALICLOUD_CUSTOMURL: '设定自定义域名',
PICBED_ALICLOUD_OPTIONS: '设定网址后缀',
PICBED_ALICLOUD_MESSAGE_AREA: '例如:oss-cn-beijing',
PICBED_ALICLOUD_MESSAGE_PATH: '例如:test/',
PICBED_ALICLOUD_MESSAGE_OPTIONS: '例如:?x-oss-process=xxx',
PICBED_ALICLOUD_MESSAGE_CUSTOMURL: '例如:https://test.com',

// Tencent-cloud
PICBED_TENCENTCLOUD: '腾讯云COS',
Expand All @@ -30,7 +34,13 @@ export const ZH_CN = {
PICBED_TENCENTCLOUD_BUCKET: '设定Bucket',
PICBED_TENCENTCLOUD_AREA: '设定存储区域',
PICBED_TENCENTCLOUD_PATH: '设定存储路径',
PICBED_TENCENTCLOUD_OPTIONS: '设定网址后缀',
PICBED_TENCENTCLOUD_CUSTOMURL: '设定自定义域名',
PICBED_TENCENTCLOUD_MESSAGE_APPID: '例如:1234567890',
PICBED_TENCENTCLOUD_MESSAGE_AREA: '例如:ap-beijing',
PICBED_TENCENTCLOUD_MESSAGE_PATH: '例如:test/',
PICBED_TENCENTCLOUD_MESSAGE_CUSTOMURL: '例如:https://test.com',
PICBED_TENCENTCLOUD_MESSAGE_OPTIONS: '例如:?imageMogr2',

// GitHub
PICBED_GITHUB: 'GitHub',
Expand All @@ -39,6 +49,10 @@ export const ZH_CN = {
PICBED_GITHUB_PATH: '设定存储路径',
PICBED_GITHUB_BRANCH: '设定分支名',
PICBED_GITHUB_CUSTOMURL: '设定自定义域名',
PICBED_GITHUB_MESSAGE_REPO: '格式:username/repo',
PICBED_GITHUB_MESSAGE_BRANCH: '例如:main',
PICBED_GITHUB_MESSAGE_PATH: '例如:test/',
PICBED_GITHUB_MESSAGE_CUSTOMURL: '例如:https://test.com',

// qiniu
PICBED_QINIU: '七牛云',
Expand All @@ -49,11 +63,16 @@ export const ZH_CN = {
PICBED_QINIU_URL: '设定访问网址',
PICBED_QINIU_OPTIONS: '设定网址后缀',
PICBED_QINIU_AREA: '设定存储区域',
PICBED_QINIU_MESSAGE_PATH: '例如:test/',
PICBED_QINIU_MESSAGE_AREA: '例如:z0',
PICBED_QINIU_MESSAGE_OPTIONS: '例如:?imageslim',
PICBED_QINIU_MESSAGE_URL: '例如:https://xxx.yyy.glb.clouddn.com',

// imgur
PICBED_IMGUR: 'Imgur',
PICBED_IMGUR_CLIENTID: '设定ClientId',
PICBED_IMGUR_PROXY: '设定代理',
PICBED_IMGUR_MESSAGE_PROXY: '例如:http://127.0.0.1:1080',

// upyun
PICBED_UPYUN: '又拍云',
Expand All @@ -63,6 +82,11 @@ export const ZH_CN = {
PICBED_UPYUN_PATH: '设定存储路径',
PICBED_UPYUN_URL: '设定加速域名',
PICBED_UPYUN_OPTIONS: '设定网址后缀',
PICBED_UPYUN_MESSAGE_OPERATOR: '例如:me',
PICBED_UPYUN_MESSAGE_PASSWORD: '输入操作员密码',
PICBED_UPYUN_MESSAGE_URL: '例如:http://xxx.test.upcdn.net',
PICBED_UPYUN_MESSAGE_OPTIONS: '例如:!imgslim',
PICBED_UPYUN_MESSAGE_PATH: '例如:test/',

// Plugin Handler
PLUGIN_HANDLER_PLUGIN_INSTALL_SUCCESS: '插件安装成功',
Expand Down
24 changes: 15 additions & 9 deletions src/plugins/uploader/aliyun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,49 +80,53 @@ const config = (ctx: IPicGo): IPluginConfig[] => {
{
name: 'accessKeyId',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_ACCESSKEYID'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_ACCESSKEYID') },
default: userConfig.accessKeyId || '',
required: true
},
{
name: 'accessKeySecret',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_ACCESSKEYSECRET'),
type: 'password',
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_ACCESSKEYSECRET') },
default: userConfig.accessKeySecret || '',
required: true
},
{
name: 'bucket',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_BUCKET'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_BUCKET') },
default: userConfig.bucket || '',
required: true
},
{
name: 'area',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_AREA'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_AREA') },
default: userConfig.area || '',
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_MESSAGE_AREA') },
required: true
},
{
name: 'path',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_PATH'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_PATH') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_MESSAGE_PATH') },
default: userConfig.path || '',
required: false
},
{
name: 'customUrl',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_CUSTOMURL'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_CUSTOMURL') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_MESSAGE_CUSTOMURL') },
default: userConfig.customUrl || '',
required: false
},
{
name: 'options',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_OPTIONS'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_OPTIONS') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD_MESSAGE_OPTIONS') },
default: userConfig.options || '',
required: false
}
Expand All @@ -132,7 +136,9 @@ const config = (ctx: IPicGo): IPluginConfig[] => {

export default function register (ctx: IPicGo): void {
ctx.helper.uploader.register('aliyun', {
name: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD'),
get name () {
return ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD')
},
handle,
config
})
Expand Down
18 changes: 11 additions & 7 deletions src/plugins/uploader/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,39 @@ const config = (ctx: IPicGo): IPluginConfig[] => {
{
name: 'repo',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_REPO'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_REPO') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_MESSAGE_REPO') },
default: userConfig.repo || '',
required: true
},
{
name: 'branch',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_BRANCH'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_BRANCH') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_MESSAGE_BRANCH') },
default: userConfig.branch || 'master',
required: true
},
{
name: 'token',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_TOKEN'),
type: 'password',
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_TOKEN') },
default: userConfig.token || '',
required: true
},
{
name: 'path',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_PATH'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_PATH') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_MESSAGE_PATH') },
default: userConfig.path || '',
required: false
},
{
name: 'customUrl',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_CUSTOMURL'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_CUSTOMURL') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB_MESSAGE_CUSTOMURL') },
default: userConfig.customUrl || '',
required: false
}
Expand All @@ -103,7 +107,7 @@ const config = (ctx: IPicGo): IPluginConfig[] => {

export default function register (ctx: IPicGo): void {
ctx.helper.uploader.register('github', {
name: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB'),
get name () { return ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB') },
handle,
config
})
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/uploader/imgur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ const config = (ctx: IPicGo): IPluginConfig[] => {
{
name: 'clientId',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR_CLIENTID'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR_CLIENTID') },
default: userConfig.clientId || '',
required: true
},
{
name: 'proxy',
type: 'input',
alias: ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR_PROXY'),
get alias () { return ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR_PROXY') },
get message () { return ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR_MESSAGE_PROXY') },
default: userConfig.proxy || '',
required: false
}
Expand All @@ -81,7 +82,7 @@ const config = (ctx: IPicGo): IPluginConfig[] => {

export default function register (ctx: IPicGo): void {
ctx.helper.uploader.register('imgur', {
name: ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR'),
get name () { return ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR') },
handle,
config
})
Expand Down
Loading

0 comments on commit 11b3197

Please sign in to comment.