Feature: finish i18n system

This commit is contained in:
PiEgg
2022-08-20 16:44:55 +08:00
parent 86012c0c7b
commit 428ffc7ef1
35 changed files with 1035 additions and 538 deletions

View File

@@ -2,6 +2,9 @@ import fs from 'fs-extra'
import path from 'path'
import os from 'os'
import { dbPathChecker } from 'apis/core/datastore/dbChecker'
import yaml from 'js-yaml'
import { i18nManager } from '~/main/i18n'
// import { ILocales } from '~/universal/types/i18n'
const configPath = dbPathChecker()
const CONFIG_DIR = path.dirname(configPath)
@@ -11,13 +14,14 @@ function beforeOpen () {
resolveMacWorkFlow()
}
resolveClipboardImageGenerator()
resolveOtherI18nFiles()
}
/**
* macOS 右键菜单
*/
function resolveMacWorkFlow () {
const dest = `${os.homedir}/Library/Services/Upload pictures with PicGo.workflow`
const dest = `${os.homedir()}/Library/Services/Upload pictures with PicGo.workflow`
if (fs.existsSync(dest)) {
return true
} else {
@@ -29,6 +33,20 @@ function resolveMacWorkFlow () {
}
}
function diffFilesAndUpdate (filePath1: string, filePath2: string) {
try {
const file1 = fs.existsSync(filePath1) && fs.readFileSync(filePath1)
const file2 = fs.existsSync(filePath1) && fs.readFileSync(filePath2)
if (!file1 || !file2 || !file1.equals(file2)) {
fs.copyFileSync(filePath1, filePath2)
}
} catch (e) {
console.error(e)
fs.copyFileSync(filePath1, filePath2)
}
}
/**
* 初始化剪贴板生成图片的脚本
*/
@@ -44,20 +62,6 @@ function resolveClipboardImageGenerator () {
})
}
function diffFilesAndUpdate (filePath1: string, filePath2: string) {
try {
const file1 = fs.existsSync(filePath1) && fs.readFileSync(filePath1)
const file2 = fs.existsSync(filePath1) && fs.readFileSync(filePath2)
if (!file1 || !file2 || !file1.equals(file2)) {
fs.copyFileSync(filePath1, filePath2)
}
} catch (e) {
console.error(e)
fs.copyFileSync(filePath1, filePath2)
}
}
function getClipboardFiles () {
const files = [
'/linux.sh',
@@ -76,4 +80,34 @@ function resolveClipboardImageGenerator () {
}
}
/**
* 初始化其他语言文件
*/
function resolveOtherI18nFiles () {
const i18nFolder = path.join(CONFIG_DIR, 'i18n')
if (!fs.pathExistsSync(i18nFolder)) {
fs.mkdirSync(i18nFolder)
}
i18nManager.setOutterI18nFolder(i18nFolder)
const i18nFiles = fs.readdirSync(path.join(CONFIG_DIR, 'i18n'), {
withFileTypes: true
})
i18nFiles.forEach(item => {
if (item.isFile()) {
if (item.name.endsWith('.yml')) {
const i18nFilePath = path.join(i18nFolder, item.name)
const i18nFile = fs.readFileSync(i18nFilePath, 'utf8')
try {
const i18nFileObj = yaml.load(i18nFile) as unknown as ILocales
if (i18nFileObj?.LANG_DISPLAY_LABEL) {
i18nManager.addI18nFile(item.name.replace('.yml', ''), i18nFileObj.LANG_DISPLAY_LABEL)
}
} catch (e) {
console.error(e)
}
}
}
})
}
export default beforeOpen