diff --git a/.env b/.env index 2154df18a..9521446bb 100644 --- a/.env +++ b/.env @@ -13,3 +13,4 @@ APP_IMAGE_CDN= APP_DIANWU_URL= APP_MERCHANT_URL= APP_ADAPAY= +APP_I18N_ORIGIN_LANG= diff --git a/config/dev.js b/config/dev.js index 73155f779..703c6d37c 100644 --- a/config/dev.js +++ b/config/dev.js @@ -1,130 +1,8 @@ -// 处理 ECONNRESET 错误,避免进程崩溃 -// 这个错误通常发生在 webpack watch 模式下连接被重置时 -if (!process.listenerCount('uncaughtException')) { - process.on('uncaughtException', (err) => { - if (err.code === 'ECONNRESET' || err.message?.includes('ECONNRESET')) { - console.warn('⚠️ 连接重置错误已忽略:', err.message) - return - } - throw err - }) -} - -if (!process.listenerCount('unhandledRejection')) { - process.on('unhandledRejection', (reason, promise) => { - if (reason && typeof reason === 'object' && 'code' in reason && reason.code === 'ECONNRESET') { - console.warn('⚠️ 连接重置错误已忽略:', reason.message || reason) - return - } - console.error('未处理的 Promise 拒绝:', reason) - }) -} - module.exports = { env: { NODE_ENV: '"development"' }, defineConstants: {}, weapp: {}, - h5: { - devServer: { - // 处理连接重置错误,避免进程崩溃 - client: { - overlay: { - errors: true, - warnings: false - } - } - }, - webpackChain: (chain, webpack) => { - // 添加错误处理插件 - chain.plugin('handle-errors').use( - class { - apply(compiler) { - compiler.hooks.afterPlugins.tap('HandleErrors', () => { - // 在插件初始化后设置错误处理 - if (!process.listenerCount('uncaughtException')) { - process.on('uncaughtException', (err) => { - if (err.code === 'ECONNRESET' || err.message?.includes('ECONNRESET')) { - console.warn('⚠️ 连接重置错误已忽略:', err.message) - return - } - throw err - }) - } - if (!process.listenerCount('unhandledRejection')) { - process.on('unhandledRejection', (reason, promise) => { - if ( - reason && - typeof reason === 'object' && - 'code' in reason && - reason.code === 'ECONNRESET' - ) { - console.warn('⚠️ 连接重置错误已忽略:', reason.message || reason) - return - } - console.error('未处理的 Promise 拒绝:', reason) - }) - } - }) - } - } - ) - } - }, - mini: { - webpackChain: (chain, webpack) => { - chain.merge({ - plugin: { - install: { - plugin: require('terser-webpack-plugin'), - args: [ - { - terserOptions: { - compress: true, // 默认使用terser压缩 - // mangle: false, - keep_classnames: true, // 不改变class名称 - keep_fnames: true // 不改变函数名称 - } - } - ] - } - } - }) - - // 添加错误处理插件 - chain.plugin('handle-errors').use( - class { - apply(compiler) { - compiler.hooks.done.tap('HandleErrors', () => { - // 在编译完成后设置错误处理 - if (!process.listenerCount('uncaughtException')) { - process.on('uncaughtException', (err) => { - if (err.code === 'ECONNRESET' || err.message?.includes('ECONNRESET')) { - console.warn('⚠️ 连接重置错误已忽略:', err.message) - return - } - throw err - }) - } - if (!process.listenerCount('unhandledRejection')) { - process.on('unhandledRejection', (reason, promise) => { - if ( - reason && - typeof reason === 'object' && - 'code' in reason && - reason.code === 'ECONNRESET' - ) { - console.warn('⚠️ 连接重置错误已忽略:', reason.message || reason) - return - } - console.error('未处理的 Promise 拒绝:', reason) - }) - } - }) - } - } - ) - } - } + h5: {} } diff --git a/src/app.js b/src/app.js index 22ba5e5e1..1886d5deb 100644 --- a/src/app.js +++ b/src/app.js @@ -127,6 +127,7 @@ function App({ children }) { } getSystemConfig() }) + //分包异步加载语言包 if (process.env.TARO_ENV === 'weapp') { __non_webpack_require__ && diff --git a/src/lang/index.js b/src/lang/index.js index 98917c43a..3aa96ec0b 100644 --- a/src/lang/index.js +++ b/src/lang/index.js @@ -122,7 +122,9 @@ class Lang { // 从本地存储中获取通用语言,如果不存在则使用空字符串 const commonLang = withStorageCommonLang ? globalThis._localStorage.getItem('') : '' // 从本地存储中获取当前语言,如果不存在则使用源语言 - const baseLang = withStorageLang ? globalThis._localStorage.getItem('lang') : 'en' + const baseLang = withStorageLang + ? globalThis._localStorage.getItem('lang') + : process.env.APP_I18N_ORIGIN_LANG const lang = commonLang ? commonLang : baseLang // 如果本地存储中没有语言设置,则使用环境变量中的默认语言 if (!globalThis._localStorage.getItem('lang')) { diff --git a/src/subpages/i18n/lang/index.js b/src/subpages/i18n/lang/index.js index bd7926c67..bb151e619 100644 --- a/src/subpages/i18n/lang/index.js +++ b/src/subpages/i18n/lang/index.js @@ -103,7 +103,11 @@ const withStorageCommonLang = // 从本地存储中获取通用语言,如果不存在则使用空字符串 const commonLang = withStorageCommonLang ? globalThis.localStorage.getItem('') : '' // 从本地存储中获取当前语言,如果不存在则使用源语言 -const baseLang = withStorageLang ? globalThis.localStorage.getItem('lang') : 'en' +const baseLang = withStorageLang + ? globalThis.localStorage.getItem('lang') + : process.env.APP_I18N_ORIGIN_LANG + +debugger const lang = commonLang ? commonLang : baseLang // 根据当前语言设置翻译函数的语言包 globalThis.$t.locale(globalThis.langMap[lang], 'lang')