🚧 WIP: add i18n ing

This commit is contained in:
PiEgg
2022-01-10 10:20:56 +08:00
parent 04701d4202
commit c2991dde2f
8 changed files with 120 additions and 51 deletions

View File

@@ -17,6 +17,7 @@ import pasteTemplate from '~/main/utils/pasteTemplate'
import pkg from 'root/package.json'
import { handleCopyUrl } from '~/main/utils/common'
import { privacyManager } from '~/main/utils/privacyManager'
import { T } from '#/i18n'
let contextMenu: Menu | null
let menu: Menu | null
let tray: Tray | null
@@ -41,7 +42,7 @@ export function createContextMenu () {
})
contextMenu = Menu.buildFromTemplate([
{
label: '关于',
label: T('ABOUT'),
click () {
dialog.showMessageBox({
title: 'PicGo',
@@ -51,7 +52,7 @@ export function createContextMenu () {
}
},
{
label: '打开详细窗口',
label: T('OPEN_MAIN_WINDOW'),
click () {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
settingWindow!.show()
@@ -62,14 +63,14 @@ export function createContextMenu () {
}
},
{
label: '选择默认图床',
label: T('CHOOSE_DEFAULT_PICBED'),
type: 'submenu',
// @ts-ignore
submenu
},
// @ts-ignore
{
label: '打开更新助手',
label: T('OPEN_UPDATE_HELPER'),
type: 'checkbox',
checked: db.get('settings.showUpdateTip'),
click () {
@@ -78,13 +79,13 @@ export function createContextMenu () {
}
},
{
label: '隐私协议',
label: T('PRIVACY_AGREEMENT'),
click () {
privacyManager.show(false)
}
},
{
label: '重启应用',
label: T('RELOAD_APP'),
click () {
app.relaunch()
app.exit(0)
@@ -93,7 +94,7 @@ export function createContextMenu () {
// @ts-ignore
{
role: 'quit',
label: '退出'
label: T('QUIT')
}
])
} else if (process.platform === 'linux') {
@@ -106,7 +107,7 @@ export function createContextMenu () {
contextMenu = Menu.buildFromTemplate([
{
label: '打开详细窗口',
label: T('OPEN_MAIN_WINDOW'),
click () {
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
settingWindow!.show()
@@ -118,7 +119,7 @@ export function createContextMenu () {
},
// @ts-ignore
{
label: '打开更新助手',
label: T('OPEN_UPDATE_HELPER'),
type: 'checkbox',
checked: db.get('settings.showUpdateTip'),
click () {
@@ -127,7 +128,7 @@ export function createContextMenu () {
}
},
{
label: '关于应用',
label: T('ABOUT'),
click () {
dialog.showMessageBox({
title: 'PicGo',
@@ -140,7 +141,7 @@ export function createContextMenu () {
// @ts-ignore
{
role: 'quit',
label: '退出'
label: T('QUIT')
}
])
}
@@ -214,7 +215,7 @@ export function createTray () {
for (let i = 0; i < imgs.length; i++) {
pasteText.push(pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink')))
const notification = new Notification({
title: '上传成功',
title: T('UPLOAD_SUCCESSFULLY'),
body: imgs[i].imgUrl!,
icon: files[i]
})

View File

@@ -0,0 +1,18 @@
// TODO: so how to import pure esm module in electron main process????? help wanted
// just copy the fix-path because I can't import pure ESM module in electron main process
const shellPath = require('shell-path')
export default function fixPath () {
if (process.platform === 'win32') {
return
}
process.env.PATH = shellPath.sync() || [
'./node_modules/.bin',
'/.nodebrew/current/bin',
'/usr/local/bin',
process.env.PATH
].join(':')
}

View File

@@ -34,6 +34,7 @@ import bus from '@core/bus'
import { privacyManager } from '~/main/utils/privacyManager'
import logger from 'apis/core/picgo/logger'
import picgo from 'apis/core/picgo'
import fixPath from './fixPath'
const isDevelopment = process.env.NODE_ENV !== 'production'
@@ -57,8 +58,6 @@ const handleStartUpFiles = (argv: string[], cwd: string) => {
class LifeCycle {
private async beforeReady () {
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
// https://stackoverflow.com/questions/56691391/dynamic-loading-of-external-modules-in-webpack-fails
const fixPath = (await import(/* webpackIgnore: true */ 'fix-path')).default
// fix the $PATH in macOS & linux
fixPath()
beforeOpen()

View File

@@ -0,0 +1,27 @@
import { ZH_CN } from './zh-CN'
import { ObjectAdapter, I18n } from '@picgo/i18n'
const languageList = {
'zh-CN': ZH_CN
}
const lowercaseKeys = (obj: any) =>
Object.keys(obj).reduce((acc: any, key: string) => {
acc[key.toLowerCase()] = obj[key]
return acc
}, {})
// FIXME: @picgo/i18n no lowecase
const objectAdapter = new ObjectAdapter(lowercaseKeys(languageList))
const i18n = new I18n({
adapter: objectAdapter,
defaultLanguage: 'zh-cn'
})
// FIXME: @picgo/i18n args should be optional
const T = (key: ILocalesKey, args: IStringKeyMap = {}): string => {
return i18n.translate(key, args)!
}
export { i18n, T }

View File

@@ -0,0 +1,12 @@
export const ZH_CN = {
ABOUT: '关于',
OPEN_MAIN_WINDOW: '打开主窗口',
CHOOSE_DEFAULT_PICBED: '选择默认图床',
OPEN_UPDATE_HELPER: '打开更新助手',
PRIVACY_AGREEMENT: '隐私协议',
RELOAD_APP: '重启应用',
UPLOAD_SUCCESSFULLY: '上传成功',
QUIT: '退出'
}
export type ILocalesKey = keyof typeof ZH_CN

View File

@@ -335,3 +335,5 @@ interface IMiniWindowPos {
}
type PromiseResType<T> = T extends Promise<infer R> ? R : T
type ILocalesKey = import('#/i18n/zh-CN').ILocalesKey