提交 d5eda2ca 编写于 作者: fxy060608's avatar fxy060608

refactor: make i18n tree-shakeable

上级 e0a9b59d
...@@ -32,6 +32,11 @@ declare var __UNI_FEATURE_NAVIGATIONBAR__: boolean ...@@ -32,6 +32,11 @@ declare var __UNI_FEATURE_NAVIGATIONBAR__: boolean
declare var __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__: boolean declare var __UNI_FEATURE_NAVIGATIONBAR_BUTTONS__: boolean
declare var __UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__: boolean declare var __UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__: boolean
declare var __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__: boolean declare var __UNI_FEATURE_NAVIGATIONBAR_TRANSPARENT__: boolean
declare var __UNI_FEATURE_I18N_EN__: boolean
declare var __UNI_FEATURE_I18N_ES__: boolean
declare var __UNI_FEATURE_I18N_FR__: boolean
declare var __UNI_FEATURE_I18N_ZH_HANS__: boolean
declare var __UNI_FEATURE_I18N_ZH_HANT__: boolean
// TODO // TODO
declare var __uniRoutes: UniApp.UniRoutes declare var __uniRoutes: UniApp.UniRoutes
declare var __uniConfig: UniApp.UniConfig declare var __uniConfig: UniApp.UniConfig
......
import { extend, isString, isPlainObject } from '@vue/shared' import {
extend,
hasOwn,
isString,
isFunction,
isPlainObject,
} from '@vue/shared'
import { API_TYPE_ON_PROTOCOLS, validateProtocols } from '../protocol' import { API_TYPE_ON_PROTOCOLS, validateProtocols } from '../protocol'
import { import {
invokeCallback, invokeCallback,
...@@ -24,7 +30,15 @@ function formatApiArgs<T extends ApiLike>( ...@@ -24,7 +30,15 @@ function formatApiArgs<T extends ApiLike>(
} }
const formatArgs = options.formatArgs! const formatArgs = options.formatArgs!
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name]!(args[0][name], params) const formatterOrDefaultValue = formatArgs[name]!
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params)
} else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue
}
}
}) })
return args return args
} }
......
...@@ -31,6 +31,7 @@ export * from './protocols/network/uploadFile' ...@@ -31,6 +31,7 @@ export * from './protocols/network/uploadFile'
export * from './protocols/route/route' export * from './protocols/route/route'
export * from './protocols/ui/navigationBar' export * from './protocols/ui/navigationBar'
export * from './protocols/ui/popup'
export * from './protocols/ui/tabBar' export * from './protocols/ui/tabBar'
// helpers // helpers
export { export {
......
...@@ -2,7 +2,7 @@ export const API_DOWNLOAD_FILE = 'downloadFile' ...@@ -2,7 +2,7 @@ export const API_DOWNLOAD_FILE = 'downloadFile'
export type API_TYPE_DOWNLOAD_FILE = typeof uni.downloadFile export type API_TYPE_DOWNLOAD_FILE = typeof uni.downloadFile
export const DownloadFileOptions: ApiOptions<API_TYPE_DOWNLOAD_FILE> = { export const DownloadFileOptions: ApiOptions<API_TYPE_DOWNLOAD_FILE> = {
formatArgs: { formatArgs: {
header(value, params) { header(value: Record<string, any>, params: Record<string, any>) {
params.header = value || {} params.header = value || {}
}, },
}, },
......
...@@ -43,28 +43,16 @@ function stringifyQuery(url: string, data: Record<string, any>) { ...@@ -43,28 +43,16 @@ function stringifyQuery(url: string, data: Record<string, any>) {
} }
export const RequestProtocol: ApiProtocol<API_TYPE_REQUEST> = { export const RequestProtocol: ApiProtocol<API_TYPE_REQUEST> = {
method: { method: String as any,
type: String as any, data: [Object, String, Array, ArrayBuffer],
},
data: {
type: [Object, String, Array, ArrayBuffer],
},
url: { url: {
type: String, type: String,
required: true, required: true,
}, },
header: { header: Object,
type: Object, dataType: String,
}, responseType: String,
dataType: { withCredentials: Boolean,
type: String,
},
responseType: {
type: String,
},
withCredentials: {
type: Boolean,
},
} }
export const RequestOptions: ApiOptions<API_TYPE_REQUEST> = { export const RequestOptions: ApiOptions<API_TYPE_REQUEST> = {
...@@ -88,7 +76,7 @@ export const RequestOptions: ApiOptions<API_TYPE_REQUEST> = { ...@@ -88,7 +76,7 @@ export const RequestOptions: ApiOptions<API_TYPE_REQUEST> = {
params.url = stringifyQuery(value, params.data) params.url = stringifyQuery(value, params.data)
} }
}, },
header(value, params) { header(value: Record<string, any>, params: Record<string, any>) {
const header = (params.header = value || {}) const header = (params.header = value || {})
if (params.method !== HTTP_METHODS[0]) { if (params.method !== HTTP_METHODS[0]) {
if ( if (
......
...@@ -2,10 +2,10 @@ export const API_UPLOAD_FILE = 'uploadFile' ...@@ -2,10 +2,10 @@ export const API_UPLOAD_FILE = 'uploadFile'
export type API_TYPE_UPLOAD_FILE = typeof uni.uploadFile export type API_TYPE_UPLOAD_FILE = typeof uni.uploadFile
export const UploadFileOptions: ApiOptions<API_TYPE_UPLOAD_FILE> = { export const UploadFileOptions: ApiOptions<API_TYPE_UPLOAD_FILE> = {
formatArgs: { formatArgs: {
header(value, params) { header(value: Record<string, any>, params: Record<string, any>) {
params.header = value || {} params.header = value || {}
}, },
formData(value, params) { formData(value: Record<string, any>, params: Record<string, any>) {
params.formData = value || {} params.formData = value || {}
}, },
}, },
...@@ -16,9 +16,7 @@ export const UploadFileProtocol: ApiProtocol<API_TYPE_UPLOAD_FILE> = { ...@@ -16,9 +16,7 @@ export const UploadFileProtocol: ApiProtocol<API_TYPE_UPLOAD_FILE> = {
type: String, type: String,
required: true, required: true,
}, },
files: { files: Array,
type: Array,
},
filePath: String, filePath: String,
name: String, name: String,
header: Object, header: Object,
......
import getRealPath from 'uni-platform/helpers/get-real-path'
export const showModal = {
title: {
type: String,
default: ''
},
content: {
type: String,
default: ''
},
showCancel: {
type: Boolean,
default: true
},
cancelText: {
type: String,
default: '取消'
},
cancelColor: {
type: String,
default: '#000000'
},
confirmText: {
type: String,
default: '确定'
},
confirmColor: {
type: String,
default: '#007aff'
},
visible: {
type: Boolean,
default: true
}
}
export const showToast = {
title: {
type: String,
default: ''
},
icon: {
default: 'success',
validator(icon, params) {
if (['success', 'loading', 'none'].indexOf(icon) === -1) {
params.icon = 'success'
}
}
},
image: {
type: String,
default: '',
validator(image, params) {
if (image) {
params.image = getRealPath(image)
}
}
},
duration: {
type: Number,
default: 1500
},
mask: {
type: Boolean,
default: false
},
visible: {
type: Boolean,
default: true
}
}
export const showLoading = {
title: {
type: String,
default: ''
},
icon: {
type: String,
default: 'loading'
},
duration: {
type: Number,
default: 100000000 // 简单处理 showLoading,直接设置个大值
},
mask: {
type: Boolean,
default: false
},
visible: {
type: Boolean,
default: true
}
}
export const showActionSheet = {
itemList: {
type: Array,
required: true,
validator(itemList, params) {
if (!itemList.length) {
return 'parameter.itemList should have at least 1 item'
}
}
},
itemColor: {
type: String,
default: '#000000'
},
visible: {
type: Boolean,
default: true
},
popover: {
type: Object
}
}
import { hasOwn } from '@vue/shared'
import { useI18n, initI18nShowModalMsgs } from '@dcloudio/uni-core'
import { PRIMARY_COLOR } from '@dcloudio//uni-shared'
import { getRealPath } from '@dcloudio/uni-platform'
export const API_SHOW_MODAL = 'showModal'
export type API_TYPE_SHOW_MODAL = typeof uni.showModal
const { t } = useI18n()
export const ShowModalProtocol: ApiProtocol<API_TYPE_SHOW_MODAL> = {
title: String,
content: String,
showCancel: Boolean,
cancelText: String,
cancelColor: String,
confirmText: String,
confirmColor: String,
}
let isInitI18nShowModalMsgs = false
export const ShowModalOptions: ApiOptions<API_TYPE_SHOW_MODAL> = {
beforeInvoke() {
// dynamic init (tree shaking)
if (isInitI18nShowModalMsgs) {
return
}
isInitI18nShowModalMsgs = true
initI18nShowModalMsgs()
},
formatArgs: {
title: '',
content: '',
showCancel: true,
cancelText(_value, params) {
if (!hasOwn(params, 'cancelText')) {
params.cancelText = t('uni.showModal.cancel')
}
},
cancelColor: '#000',
confirmText(_value, params) {
if (!hasOwn(params, 'confirmText')) {
params.confirmText = t('uni.showModal.confirm')
}
},
confirmColor: PRIMARY_COLOR,
},
}
export const API_SHOW_TOAST = 'showToast'
export type API_TYPE_SHOW_TOAST = typeof uni.showToast
export const ShowToastProtocol: ApiProtocol<API_TYPE_SHOW_TOAST> = {
title: String,
icon: String as any,
image: String,
duration: Number,
mask: Boolean,
}
export const ShowToastOptions: ApiOptions<API_TYPE_SHOW_TOAST> = {
formatArgs: {
title: '',
icon(value, params) {
if (['success', 'loading', 'none'].indexOf(value!) === -1) {
params.icon = 'success'
}
},
image(value, params) {
if (value) {
params.image = getRealPath(value)
}
},
duration: 1500,
mask: false,
},
}
export const API_SHOW_LOADING = 'showLoading'
export type API_TYPE_SHOW_LOADING = typeof uni.showLoading
export const ShowLoadingProtocol: ApiProtocol<API_TYPE_SHOW_LOADING> = {
title: String,
mask: Boolean,
}
export const ShowLoadingOptions: ApiOptions<API_TYPE_SHOW_LOADING> = {
formatArgs: {
title: '',
mask: false,
},
}
export const API_SHOW_ACTION_SHEET = 'showActionSheet'
export type API_TYPE_SHOW_ACTION_SHEET = typeof uni.showActionSheet
export const ShowActionSheetProtocol: ApiProtocol<API_TYPE_SHOW_ACTION_SHEET> = {
itemList: {
type: Array,
required: true,
},
itemColor: String,
}
export const ShowActionSheetOptions: ApiOptions<API_TYPE_SHOW_ACTION_SHEET> = {
formatArgs: {
itemColor: '#000',
},
}
export const API_HIDE_TOAST = 'hideToast'
export type API_TYPE_HIDE_TOAST = typeof uni.hideToast
export const API_HIDE_LOADING = 'hideLoading'
export type API_TYPE_HIDE_LOADING = typeof uni.hideLoading
...@@ -22,7 +22,7 @@ interface ApiOptions<T extends ApiLike, P = AsyncApiOptions<T>> { ...@@ -22,7 +22,7 @@ interface ApiOptions<T extends ApiLike, P = AsyncApiOptions<T>> {
beforeAll?: (res: unknown) => void beforeAll?: (res: unknown) => void
beforeSuccess?: (res: unknown) => void beforeSuccess?: (res: unknown) => void
formatArgs?: { formatArgs?: {
[K in keyof P]?: ApiArgsValidator<P[K], P> [K in keyof P]?: ApiArgsValidator<P[K], P> | P[K]
} }
} }
......
import { defineComponent, inject } from 'vue' import { defineComponent, inject } from 'vue'
import { useI18n } from '@dcloudio/uni-core' import { useI18n, initI18nButtonMsgs } from '@dcloudio/uni-core'
import { useHover } from '../../helpers/useHover' import { useHover } from '../../helpers/useHover'
import { useBooleanAttr } from '../../helpers/useBooleanAttr' import { useBooleanAttr } from '../../helpers/useBooleanAttr'
import { UniFormCtx, uniFormKey } from '../form' import { UniFormCtx, uniFormKey } from '../form'
if (__PLATFORM__ === 'app-plus') {
initI18nButtonMsgs()
}
export default defineComponent({ export default defineComponent({
name: 'Button', name: 'Button',
props: { props: {
......
import { initVueI18n } from '@dcloudio/uni-i18n'
import en from './en.json'
import es from './es.json'
import fr from './fr.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'
const messages = {
en,
es,
fr,
'zh-Hans': zhHans,
'zh-Hant': zhHant,
}
const fallbackLocale = 'en'
const i18n = /*#__PURE__*/ initVueI18n(
__PLATFORM__ === 'app-plus' || __PLATFORM__ === 'h5' ? messages : {},
fallbackLocale
)
export function useI18n() {
return i18n
}
export * from './util' export * from './util'
export * from './icon' export * from './icon'
export * from './i18n'
export * from './getRealRoute' export * from './getRealRoute'
export * from './getWindowOffset' export * from './getWindowOffset'
export * from './useI18n'
export * from './messages'
// This file is created by scripts/i18n.js
// Do not modify this file!!!!!!!!!
import {
LOCALE_EN,
LOCALE_ES,
LOCALE_FR,
LOCALE_ZH_HANS,
LOCALE_ZH_HANT,
} from '@dcloudio/uni-i18n'
import { useI18n } from './useI18n'
const i18n = useI18n()
function normalizeMessages(
namespace: string,
messages: Record<string, string>
) {
return Object.keys(messages).reduce<Record<string, string>>((res, name) => {
res[namespace + name] = messages[name]
return res
}, {})
}
export function initI18nAppMsgs() {
const name = 'uni.app.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, { quit: 'Press back button again to exit' })
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, { quit: 'Pulse otra vez para salir' })
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
quit: "Appuyez à nouveau pour quitter l'application",
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, { quit: '再按一次退出应用' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, { quit: '再按一次退出應用' })
)
}
}
export function initI18nAsyncMsgs() {
const name = 'uni.async.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
error: 'The connection timed out, click the screen to try again.',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
error:
'Se agotó el tiempo de conexión, haga clic en la pantalla para volver a intentarlo.',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
error: "La connexion a expiré, cliquez sur l'écran pour réessayer.",
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, { error: '连接服务器超时,点击屏幕重试' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, { error: '連接服務器超時,點擊屏幕重試' })
)
}
}
export function initI18nShowActionSheetMsgs() {
const name = 'uni.showActionSheet.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(LOCALE_EN, normalizeMessages(name, { cancel: 'Cancel' }))
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(LOCALE_ES, normalizeMessages(name, { cancel: 'Cancelar' }))
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(LOCALE_FR, normalizeMessages(name, { cancel: 'Annuler' }))
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(LOCALE_ZH_HANS, normalizeMessages(name, { cancel: '取消' }))
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(LOCALE_ZH_HANT, normalizeMessages(name, { cancel: '取消' }))
}
}
export function initI18nShowToastMsgs() {
const name = 'uni.showToast.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
unpaired: 'Please note showToast must be paired with hideToast',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
unpaired:
'Tenga en cuenta que showToast debe estar emparejado con hideToast',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
unpaired: 'Veuillez noter que showToast doit être associé à hideToast',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
unpaired: '请注意 showToast 与 hideToast 必须配对使用',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
unpaired: '請注意 showToast 與 hideToast 必須配對使用',
})
)
}
}
export function initI18nShowLoadingMsgs() {
const name = 'uni.showLoading.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
unpaired: 'Please note showLoading must be paired with hideLoading',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
unpaired:
'Tenga en cuenta que showLoading debe estar emparejado con hideLoading',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
unpaired:
'Veuillez noter que showLoading doit être associé à hideLoading',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
unpaired: '请注意 showLoading 与 hideLoading 必须配对使用',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
unpaired: '請注意 showLoading 與 hideLoading 必須配對使用',
})
)
}
}
export function initI18nShowModalMsgs() {
const name = 'uni.showModal.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, { cancel: 'Cancel', confirm: 'OK' })
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, { cancel: 'Cancelar', confirm: 'OK' })
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, { cancel: 'Annuler', confirm: 'OK' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, { cancel: '取消', confirm: '确定' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, { cancel: '取消', confirm: '確定' })
)
}
}
export function initI18nChooseImageMsgs() {
const name = 'uni.chooseImage.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
cancel: 'Cancel',
'sourceType.album': 'Album',
'sourceType.camera': 'Camera',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
cancel: 'Cancelar',
'sourceType.album': 'Álbum',
'sourceType.camera': 'Cámara',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
cancel: 'Annuler',
'sourceType.album': 'Album',
'sourceType.camera': 'Caméra',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
cancel: '取消',
'sourceType.album': '从相册选择',
'sourceType.camera': '拍摄',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
cancel: '取消',
'sourceType.album': '從相冊選擇',
'sourceType.camera': '拍攝',
})
)
}
}
export function initI18nChooseVideoMsgs() {
const name = 'uni.chooseVideo.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
cancel: 'Cancel',
'sourceType.album': 'Album',
'sourceType.camera': 'Camera',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
cancel: 'Cancelar',
'sourceType.album': 'Álbum',
'sourceType.camera': 'Cámara',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
cancel: 'Annuler',
'sourceType.album': 'Album',
'sourceType.camera': 'Caméra',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
cancel: '取消',
'sourceType.album': '从相册选择',
'sourceType.camera': '拍摄',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
cancel: '取消',
'sourceType.album': '從相冊選擇',
'sourceType.camera': '拍攝',
})
)
}
}
export function initI18nPreviewImageMsgs() {
const name = 'uni.previewImage.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
cancel: 'Cancel',
'button.save': 'Save Image',
'save.success': 'Saved successfully',
'save.fail': 'Save failed',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
cancel: 'Cancelar',
'button.save': 'Guardar imagen',
'save.success': 'Guardado exitosamente',
'save.fail': 'Error al guardar',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
cancel: 'Annuler',
'button.save': 'Guardar imagen',
'save.success': 'Enregistré avec succès',
'save.fail': 'Échec de la sauvegarde',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
cancel: '取消',
'button.save': '保存图像',
'save.success': '保存图像到相册成功',
'save.fail': '保存图像到相册失败',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
cancel: '取消',
'button.save': '保存圖像',
'save.success': '保存圖像到相冊成功',
'save.fail': '保存圖像到相冊失敗',
})
)
}
}
export function initI18nSetClipboardDataMsgs() {
const name = 'uni.setClipboardData.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(LOCALE_EN, normalizeMessages(name, { success: 'Content copied' }))
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, { success: 'Contenido copiado' })
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(LOCALE_FR, normalizeMessages(name, { success: 'Contenu copié' }))
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(LOCALE_ZH_HANS, normalizeMessages(name, { success: '内容已复制' }))
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(LOCALE_ZH_HANT, normalizeMessages(name, { success: '內容已復制' }))
}
}
export function initI18nScanCodeMsgs() {
const name = 'uni.scanCode.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
title: 'Scan code',
album: 'Album',
fail: 'Recognition failure',
'flash.on': 'Tap to turn light on',
'flash.off': 'Tap to turn light off',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
title: 'Código de escaneo',
album: 'Álbum',
fail: 'Échec de la reconnaissance',
'flash.on': 'Toque para encender la luz',
'flash.off': 'Toque para apagar la luz',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
title: 'Code d’analyse',
album: 'Album',
fail: 'Fallo de reconocimiento',
'flash.on': "Appuyez pour activer l'éclairage",
'flash.off': "Appuyez pour désactiver l'éclairage",
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
title: '扫码',
album: '相册',
fail: '识别失败',
'flash.on': '轻触照亮',
'flash.off': '轻触关闭',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
title: '掃碼',
album: '相冊',
fail: '識別失敗',
'flash.on': '輕觸照亮',
'flash.off': '輕觸關閉',
})
)
}
}
export function initI18nStartSoterAuthenticationMsgs() {
const name = 'uni.startSoterAuthentication.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, { authContent: 'Fingerprint recognition' })
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
authContent: 'Reconocimiento de huellas dactilares',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
authContent: "Reconnaissance de l'empreinte digitale",
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, { authContent: '指纹识别中...' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, { authContent: '指紋識別中...' })
)
}
}
export function initI18nPickerMsgs() {
const name = 'uni.picker.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, { done: 'Done', cancel: 'Cancel' })
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, { done: 'OK', cancel: 'Cancelar' })
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, { done: 'OK', cancel: 'Annuler' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, { done: '完成', cancel: '取消' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, { done: '完成', cancel: '取消' })
)
}
}
export function initI18nVideoMsgs() {
const name = 'uni.video.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, { danmu: 'Danmu', volume: 'Volume' })
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, { danmu: 'Danmu', volume: 'Volumen' })
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, { danmu: 'Danmu', volume: 'Le Volume' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, { danmu: '弹幕', volume: '音量' })
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, { danmu: '彈幕', volume: '音量' })
)
}
}
export function initI18nButtonMsgs() {
const name = 'uni.button.'
if (__UNI_FEATURE_I18N_EN__) {
i18n.add(
LOCALE_EN,
normalizeMessages(name, {
'feedback.title': 'feedback',
'feedback.send': 'send',
})
)
}
if (__UNI_FEATURE_I18N_ES__) {
i18n.add(
LOCALE_ES,
normalizeMessages(name, {
'feedback.title': 'realimentación',
'feedback.send': 'enviar',
})
)
}
if (__UNI_FEATURE_I18N_FR__) {
i18n.add(
LOCALE_FR,
normalizeMessages(name, {
'feedback.title': "retour d'information",
'feedback.send': 'envoyer',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANS__) {
i18n.add(
LOCALE_ZH_HANS,
normalizeMessages(name, {
'feedback.title': '问题反馈',
'feedback.send': '发送',
})
)
}
if (__UNI_FEATURE_I18N_ZH_HANT__) {
i18n.add(
LOCALE_ZH_HANT,
normalizeMessages(name, {
'feedback.title': '問題反饋',
'feedback.send': '發送',
})
)
}
}
import { initVueI18n } from '@dcloudio/uni-i18n'
const i18n = initVueI18n()
export function useI18n() {
return i18n
}
export * from './i18n'
export * from './view' export * from './view'
export * from './service' export * from './service'
export * from './helpers' export * from './helpers'
...@@ -6982,7 +6982,6 @@ function applyOptions$1(instance, options, deferredData = [], deferredWatch = [] ...@@ -6982,7 +6982,6 @@ function applyOptions$1(instance, options, deferredData = [], deferredWatch = []
} }
} }
} }
// fixed by xxxxxx
function callSyncHook(name, type, options, instance, globalMixins) { function callSyncHook(name, type, options, instance, globalMixins) {
for (let i = 0; i < globalMixins.length; i++) { for (let i = 0; i < globalMixins.length; i++) {
callHookWithMixinAndExtends(name, type, globalMixins[i], instance); callHookWithMixinAndExtends(name, type, globalMixins[i], instance);
...@@ -9491,4 +9490,4 @@ function initApp(app) { ...@@ -9491,4 +9490,4 @@ function initApp(app) {
} }
} }
export { BaseTransition, Comment, Fragment, KeepAlive, Static, Suspense, Teleport, Text, Transition, TransitionGroup, callSyncHook, callWithAsyncErrorHandling, callWithErrorHandling, cloneVNode, compile$1 as compile, computed$1 as computed, createApp, createBlock, createCommentVNode, createHook, createHydrationRenderer, createRenderer, createSSRApp, createSlots, createStaticVNode, createTextVNode, createVNode, createApp as createVueApp, createSSRApp as createVueSSRApp, customRef, defineAsyncComponent, defineComponent, defineEmit, defineProps, devtools, getCurrentInstance, getTransitionRawChildren, h, handleError, hydrate, initApp, initCustomFormatter, inject, injectHook, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, isRuntimeOnly, isVNode, markRaw, mergeProps, nextTick, onActivated, onAddToFavorites, onBackPress, onBeforeActivate, onBeforeDeactivate, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onError, onErrorCaptured, onHide, onLaunch, onMounted, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onRenderTracked, onRenderTriggered, onResize, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, onUnmounted, onUpdated, openBlock, popScopeId, provide, proxyRefs, pushScopeId, queuePostFlushCb, reactive, readonly, ref, registerRuntimeCompiler, render, renderList, renderSlot, resolveComponent, resolveDirective, resolveDynamicComponent, resolveTransitionHooks, setBlockTracking, setDevtoolsHook, setTransitionHooks, shallowReactive, shallowReadonly, shallowRef, ssrContextKey, ssrUtils, toHandlers, toRaw, toRef, toRefs, transformVNodeArgs, triggerRef, unref, useContext, useCssModule, useCssVars, useSSRContext, useTransitionState, vModelCheckbox, vModelDynamic, vModelRadio, vModelSelect, vModelText, vShow, version, warn, watch, watchEffect, withCtx, withDirectives, withKeys, withModifiers, withScopeId }; export { BaseTransition, Comment, Fragment, KeepAlive, Static, Suspense, Teleport, Text, Transition, TransitionGroup, callWithAsyncErrorHandling, callWithErrorHandling, cloneVNode, compile$1 as compile, computed$1 as computed, createApp, createBlock, createCommentVNode, createHook, createHydrationRenderer, createRenderer, createSSRApp, createSlots, createStaticVNode, createTextVNode, createVNode, createApp as createVueApp, createSSRApp as createVueSSRApp, customRef, defineAsyncComponent, defineComponent, defineEmit, defineProps, devtools, getCurrentInstance, getTransitionRawChildren, h, handleError, hydrate, initApp, initCustomFormatter, inject, injectHook, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, isRuntimeOnly, isVNode, markRaw, mergeProps, nextTick, onActivated, onAddToFavorites, onBackPress, onBeforeActivate, onBeforeDeactivate, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onError, onErrorCaptured, onHide, onLaunch, onMounted, onNavigationBarButtonTap, onNavigationBarSearchInputChanged, onNavigationBarSearchInputClicked, onNavigationBarSearchInputConfirmed, onNavigationBarSearchInputFocusChanged, onPageNotFound, onPageScroll, onPullDownRefresh, onReachBottom, onReady, onRenderTracked, onRenderTriggered, onResize, onShareAppMessage, onShareTimeline, onShow, onTabItemTap, onThemeChange, onUnhandledRejection, onUnload, onUnmounted, onUpdated, openBlock, popScopeId, provide, proxyRefs, pushScopeId, queuePostFlushCb, reactive, readonly, ref, registerRuntimeCompiler, render, renderList, renderSlot, resolveComponent, resolveDirective, resolveDynamicComponent, resolveTransitionHooks, setBlockTracking, setDevtoolsHook, setTransitionHooks, shallowReactive, shallowReadonly, shallowRef, ssrContextKey, ssrUtils, toHandlers, toRaw, toRef, toRefs, transformVNodeArgs, triggerRef, unref, useContext, useCssModule, useCssVars, useSSRContext, useTransitionState, vModelCheckbox, vModelDynamic, vModelRadio, vModelSelect, vModelText, vShow, version, warn, watch, watchEffect, withCtx, withDirectives, withKeys, withModifiers, withScopeId };
...@@ -6982,7 +6982,6 @@ function applyOptions(instance, options, deferredData = [], deferredWatch = [], ...@@ -6982,7 +6982,6 @@ function applyOptions(instance, options, deferredData = [], deferredWatch = [],
} }
} }
} }
// fixed by xxxxxx
function callSyncHook(name, type, options, instance, globalMixins) { function callSyncHook(name, type, options, instance, globalMixins) {
for (let i = 0; i < globalMixins.length; i++) { for (let i = 0; i < globalMixins.length; i++) {
callHookWithMixinAndExtends(name, type, globalMixins[i], instance); callHookWithMixinAndExtends(name, type, globalMixins[i], instance);
...@@ -9405,4 +9404,4 @@ const compile$1 = () => { ...@@ -9405,4 +9404,4 @@ const compile$1 = () => {
} }
}; };
export { BaseTransition, Comment, Fragment, KeepAlive, Static, Suspense, Teleport, Text, Transition, TransitionGroup, callSyncHook, callWithAsyncErrorHandling, callWithErrorHandling, cloneVNode, compile$1 as compile, computed$1 as computed, createApp, createBlock, createCommentVNode, createHydrationRenderer, createRenderer, createSSRApp, createSlots, createStaticVNode, createTextVNode, createVNode, createApp as createVueApp, createSSRApp as createVueSSRApp, customRef, defineAsyncComponent, defineComponent, defineEmit, defineProps, devtools, getCurrentInstance, getTransitionRawChildren, h, handleError, hydrate, initCustomFormatter, inject, injectHook, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, isRuntimeOnly, isVNode, markRaw, mergeProps, nextTick, onActivated, onBeforeActivate, onBeforeDeactivate, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, onUpdated, openBlock, popScopeId, provide, proxyRefs, pushScopeId, queuePostFlushCb, reactive, readonly, ref, registerRuntimeCompiler, render, renderList, renderSlot, resolveComponent, resolveDirective, resolveDynamicComponent, resolveTransitionHooks, setBlockTracking, setDevtoolsHook, setTransitionHooks, shallowReactive, shallowReadonly, shallowRef, ssrContextKey, ssrUtils, toHandlers, toRaw, toRef, toRefs, transformVNodeArgs, triggerRef, unref, useContext, useCssModule, useCssVars, useSSRContext, useTransitionState, vModelCheckbox, vModelDynamic, vModelRadio, vModelSelect, vModelText, vShow, version, warn, watch, watchEffect, withCtx, withDirectives, withKeys, withModifiers, withScopeId }; export { BaseTransition, Comment, Fragment, KeepAlive, Static, Suspense, Teleport, Text, Transition, TransitionGroup, callWithAsyncErrorHandling, callWithErrorHandling, cloneVNode, compile$1 as compile, computed$1 as computed, createApp, createBlock, createCommentVNode, createHydrationRenderer, createRenderer, createSSRApp, createSlots, createStaticVNode, createTextVNode, createVNode, createApp as createVueApp, createSSRApp as createVueSSRApp, customRef, defineAsyncComponent, defineComponent, defineEmit, defineProps, devtools, getCurrentInstance, getTransitionRawChildren, h, handleError, hydrate, initCustomFormatter, inject, injectHook, isInSSRComponentSetup, isProxy, isReactive, isReadonly, isRef, isRuntimeOnly, isVNode, markRaw, mergeProps, nextTick, onActivated, onBeforeActivate, onBeforeDeactivate, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onUnmounted, onUpdated, openBlock, popScopeId, provide, proxyRefs, pushScopeId, queuePostFlushCb, reactive, readonly, ref, registerRuntimeCompiler, render, renderList, renderSlot, resolveComponent, resolveDirective, resolveDynamicComponent, resolveTransitionHooks, setBlockTracking, setDevtoolsHook, setTransitionHooks, shallowReactive, shallowReadonly, shallowRef, ssrContextKey, ssrUtils, toHandlers, toRaw, toRef, toRefs, transformVNodeArgs, triggerRef, unref, useContext, useCssModule, useCssVars, useSSRContext, useTransitionState, vModelCheckbox, vModelDynamic, vModelRadio, vModelSelect, vModelText, vShow, version, warn, watch, watchEffect, withCtx, withDirectives, withKeys, withModifiers, withScopeId };
此差异已折叠。
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
</style> </style>
<script> <script>
import { import {
useI18n useI18n,
initI18nAsyncMsgs
} from '@dcloudio/uni-core' } from '@dcloudio/uni-core'
initI18nAsyncMsgs()
export default { export default {
name: 'AsyncError', name: 'AsyncError',
setup() { setup() {
......
...@@ -20,6 +20,7 @@ export * from './route/reLaunch' ...@@ -20,6 +20,7 @@ export * from './route/reLaunch'
export * from './route/switchTab' export * from './route/switchTab'
export * from './ui/navigationBar' export * from './ui/navigationBar'
export * from './ui/popup'
export * from './ui/tabBar' export * from './ui/tabBar'
export { export {
......
import {
defineAsyncApi,
API_TYPE_SHOW_MODAL,
API_SHOW_MODAL,
ShowModalProtocol,
ShowModalOptions,
API_TYPE_SHOW_TOAST,
API_SHOW_TOAST,
ShowToastProtocol,
ShowToastOptions,
API_TYPE_HIDE_TOAST,
API_HIDE_TOAST,
API_HIDE_LOADING,
API_SHOW_ACTION_SHEET,
API_SHOW_LOADING,
API_TYPE_HIDE_LOADING,
API_TYPE_SHOW_ACTION_SHEET,
API_TYPE_SHOW_LOADING,
ShowActionSheetOptions,
ShowActionSheetProtocol,
ShowLoadingOptions,
ShowLoadingProtocol,
} from '@dcloudio/uni-api'
export const showModal = defineAsyncApi<API_TYPE_SHOW_MODAL>(
API_SHOW_MODAL,
() => {},
ShowModalProtocol,
ShowModalOptions
)
export const showToast = defineAsyncApi<API_TYPE_SHOW_TOAST>(
API_SHOW_TOAST,
() => {},
ShowToastProtocol,
ShowToastOptions
)
export const hideToast = defineAsyncApi<API_TYPE_HIDE_TOAST>(
API_HIDE_TOAST,
() => {}
)
export const showLoading = defineAsyncApi<API_TYPE_SHOW_LOADING>(
API_SHOW_LOADING,
() => {},
ShowLoadingProtocol,
ShowLoadingOptions
)
export const hideLoading = defineAsyncApi<API_TYPE_HIDE_LOADING>(
API_HIDE_LOADING,
() => {}
)
export const showActionSheet = defineAsyncApi<API_TYPE_SHOW_ACTION_SHEET>(
API_SHOW_ACTION_SHEET,
() => {},
ShowActionSheetProtocol,
ShowActionSheetOptions
)
...@@ -100,6 +100,11 @@ function compile(tokens, values) { ...@@ -100,6 +100,11 @@ function compile(tokens, values) {
return compiled; return compiled;
} }
const LOCALE_ZH_HANS = 'zh-Hans';
const LOCALE_ZH_HANT = 'zh-Hant';
const LOCALE_EN = 'en';
const LOCALE_FR = 'fr';
const LOCALE_ES = 'es';
const hasOwnProperty = Object.prototype.hasOwnProperty; const hasOwnProperty = Object.prototype.hasOwnProperty;
const hasOwn = (val, key) => hasOwnProperty.call(val, key); const hasOwn = (val, key) => hasOwnProperty.call(val, key);
const defaultFormatter = new BaseFormatter(); const defaultFormatter = new BaseFormatter();
...@@ -120,25 +125,25 @@ function normalizeLocale(locale, messages) { ...@@ -120,25 +125,25 @@ function normalizeLocale(locale, messages) {
locale = locale.toLowerCase(); locale = locale.toLowerCase();
if (locale.indexOf('zh') === 0) { if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) { if (locale.indexOf('-hans') !== -1) {
return 'zh-Hans'; return LOCALE_ZH_HANS;
} }
if (locale.indexOf('-hant') !== -1) { if (locale.indexOf('-hant') !== -1) {
return 'zh-Hant'; return LOCALE_ZH_HANT;
} }
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) { if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
return 'zh-Hant'; return LOCALE_ZH_HANT;
} }
return 'zh-Hans'; return LOCALE_ZH_HANS;
} }
const lang = startsWith(locale, ['en', 'fr', 'es']); const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]);
if (lang) { if (lang) {
return lang; return lang;
} }
} }
class I18n { class I18n {
constructor({ locale, fallbackLocale, messages, watcher, formater, }) { constructor({ locale, fallbackLocale, messages, watcher, formater, }) {
this.locale = 'en'; this.locale = LOCALE_EN;
this.fallbackLocale = 'en'; this.fallbackLocale = LOCALE_EN;
this.message = {}; this.message = {};
this.messages = {}; this.messages = {};
this.watchers = []; this.watchers = [];
...@@ -146,7 +151,7 @@ class I18n { ...@@ -146,7 +151,7 @@ class I18n {
this.fallbackLocale = fallbackLocale; this.fallbackLocale = fallbackLocale;
} }
this.formater = formater || defaultFormatter; this.formater = formater || defaultFormatter;
this.messages = messages; this.messages = messages || {};
this.setLocale(locale); this.setLocale(locale);
if (watcher) { if (watcher) {
this.watchLocale(watcher); this.watchLocale(watcher);
...@@ -169,7 +174,7 @@ class I18n { ...@@ -169,7 +174,7 @@ class I18n {
this.watchers.splice(index, 1); this.watchers.splice(index, 1);
}; };
} }
mergeLocaleMessage(locale, message) { add(locale, message) {
if (this.messages[locale]) { if (this.messages[locale]) {
Object.assign(this.messages[locale], message); Object.assign(this.messages[locale], message);
} }
...@@ -212,7 +217,7 @@ function getDefaultLocale() { ...@@ -212,7 +217,7 @@ function getDefaultLocale() {
} }
return uni.getSystemInfoSync().language; return uni.getSystemInfoSync().language;
} }
function initVueI18n(messages, fallbackLocale = 'en', locale) { function initVueI18n(messages = {}, fallbackLocale = LOCALE_EN, locale) {
const i18n = new I18n({ const i18n = new I18n({
locale: locale || fallbackLocale, locale: locale || fallbackLocale,
fallbackLocale, fallbackLocale,
...@@ -260,29 +265,22 @@ function initVueI18n(messages, fallbackLocale = 'en', locale) { ...@@ -260,29 +265,22 @@ function initVueI18n(messages, fallbackLocale = 'en', locale) {
t(key, values) { t(key, values) {
return t(key, values); return t(key, values);
}, },
add(locale, message) {
return i18n.add(locale, message);
},
getLocale() { getLocale() {
return i18n.getLocale(); return i18n.getLocale();
}, },
setLocale(newLocale) { setLocale(newLocale) {
return i18n.setLocale(newLocale); return i18n.setLocale(newLocale);
}, },
mixin: {
beforeCreate() {
const unwatch = i18n.watchLocale(() => {
this.$forceUpdate();
});
this.$once('hook:beforeDestroy', function () {
unwatch();
});
},
methods: {
$$t(key, values) {
return t(key, values);
},
},
},
}; };
} }
exports.I18n = I18n; exports.I18n = I18n;
exports.LOCALE_EN = LOCALE_EN;
exports.LOCALE_ES = LOCALE_ES;
exports.LOCALE_FR = LOCALE_FR;
exports.LOCALE_ZH_HANS = LOCALE_ZH_HANS;
exports.LOCALE_ZH_HANT = LOCALE_ZH_HANT;
exports.initVueI18n = initVueI18n; exports.initVueI18n = initVueI18n;
export declare type BuiltInLocale = 'zh-Hans' | 'zh-Hant' | 'en' | 'fr' | 'es'; export declare type BuiltInLocale = typeof LOCALE_ZH_HANS | typeof LOCALE_ZH_HANT | typeof LOCALE_EN | typeof LOCALE_FR | typeof LOCALE_ES;
export declare interface Formatter { export declare interface Formatter {
interpolate: (message: string, values?: Record<string, unknown> | Array<unknown>) => Array<unknown>; interpolate: (message: string, values?: Record<string, unknown> | Array<unknown>) => Array<unknown>;
...@@ -16,7 +16,7 @@ export declare class I18n { ...@@ -16,7 +16,7 @@ export declare class I18n {
setLocale(locale: string): void; setLocale(locale: string): void;
getLocale(): BuiltInLocale; getLocale(): BuiltInLocale;
watchLocale(fn: LocaleWatcher): () => void; watchLocale(fn: LocaleWatcher): () => void;
mergeLocaleMessage(locale: BuiltInLocale, message: Record<string, string>): void; add(locale: BuiltInLocale, message: Record<string, string>): void;
t(key: string, values?: Record<string, unknown> | Array<unknown> | BuiltInLocale): string; t(key: string, values?: Record<string, unknown> | Array<unknown> | BuiltInLocale): string;
t(key: string, locale?: BuiltInLocale, values?: Record<string, unknown> | Array<unknown>): string; t(key: string, locale?: BuiltInLocale, values?: Record<string, unknown> | Array<unknown>): string;
} }
...@@ -24,24 +24,29 @@ export declare class I18n { ...@@ -24,24 +24,29 @@ export declare class I18n {
export declare interface I18nOptions { export declare interface I18nOptions {
locale: BuiltInLocale; locale: BuiltInLocale;
fallbackLocale?: BuiltInLocale; fallbackLocale?: BuiltInLocale;
messages: LocaleMessages; messages?: LocaleMessages;
formater?: Formatter; formater?: Formatter;
watcher?: LocaleWatcher; watcher?: LocaleWatcher;
} }
export declare function initVueI18n(messages: LocaleMessages, fallbackLocale?: BuiltInLocale, locale?: BuiltInLocale): { export declare function initVueI18n(messages?: LocaleMessages, fallbackLocale?: BuiltInLocale, locale?: BuiltInLocale): {
i18n: I18n; i18n: I18n;
t(key: string, values?: Record<string, unknown> | unknown[] | undefined): string; t(key: string, values?: Record<string, unknown> | unknown[] | undefined): string;
add(locale: BuiltInLocale, message: Record<string, string>): void;
getLocale(): BuiltInLocale; getLocale(): BuiltInLocale;
setLocale(newLocale: BuiltInLocale): void; setLocale(newLocale: BuiltInLocale): void;
mixin: {
beforeCreate(): void;
methods: {
$$t(key: string, values?: any): string;
};
};
}; };
export declare const LOCALE_EN = "en";
export declare const LOCALE_ES = "es";
export declare const LOCALE_FR = "fr";
export declare const LOCALE_ZH_HANS = "zh-Hans";
export declare const LOCALE_ZH_HANT = "zh-Hant";
export declare type LocaleMessages = { export declare type LocaleMessages = {
[name in BuiltInLocale]?: Record<string, string>; [name in BuiltInLocale]?: Record<string, string>;
}; };
......
...@@ -96,6 +96,11 @@ function compile(tokens, values) { ...@@ -96,6 +96,11 @@ function compile(tokens, values) {
return compiled; return compiled;
} }
const LOCALE_ZH_HANS = 'zh-Hans';
const LOCALE_ZH_HANT = 'zh-Hant';
const LOCALE_EN = 'en';
const LOCALE_FR = 'fr';
const LOCALE_ES = 'es';
const hasOwnProperty = Object.prototype.hasOwnProperty; const hasOwnProperty = Object.prototype.hasOwnProperty;
const hasOwn = (val, key) => hasOwnProperty.call(val, key); const hasOwn = (val, key) => hasOwnProperty.call(val, key);
const defaultFormatter = new BaseFormatter(); const defaultFormatter = new BaseFormatter();
...@@ -116,25 +121,25 @@ function normalizeLocale(locale, messages) { ...@@ -116,25 +121,25 @@ function normalizeLocale(locale, messages) {
locale = locale.toLowerCase(); locale = locale.toLowerCase();
if (locale.indexOf('zh') === 0) { if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) { if (locale.indexOf('-hans') !== -1) {
return 'zh-Hans'; return LOCALE_ZH_HANS;
} }
if (locale.indexOf('-hant') !== -1) { if (locale.indexOf('-hant') !== -1) {
return 'zh-Hant'; return LOCALE_ZH_HANT;
} }
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) { if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
return 'zh-Hant'; return LOCALE_ZH_HANT;
} }
return 'zh-Hans'; return LOCALE_ZH_HANS;
} }
const lang = startsWith(locale, ['en', 'fr', 'es']); const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]);
if (lang) { if (lang) {
return lang; return lang;
} }
} }
class I18n { class I18n {
constructor({ locale, fallbackLocale, messages, watcher, formater, }) { constructor({ locale, fallbackLocale, messages, watcher, formater, }) {
this.locale = 'en'; this.locale = LOCALE_EN;
this.fallbackLocale = 'en'; this.fallbackLocale = LOCALE_EN;
this.message = {}; this.message = {};
this.messages = {}; this.messages = {};
this.watchers = []; this.watchers = [];
...@@ -142,7 +147,7 @@ class I18n { ...@@ -142,7 +147,7 @@ class I18n {
this.fallbackLocale = fallbackLocale; this.fallbackLocale = fallbackLocale;
} }
this.formater = formater || defaultFormatter; this.formater = formater || defaultFormatter;
this.messages = messages; this.messages = messages || {};
this.setLocale(locale); this.setLocale(locale);
if (watcher) { if (watcher) {
this.watchLocale(watcher); this.watchLocale(watcher);
...@@ -165,7 +170,7 @@ class I18n { ...@@ -165,7 +170,7 @@ class I18n {
this.watchers.splice(index, 1); this.watchers.splice(index, 1);
}; };
} }
mergeLocaleMessage(locale, message) { add(locale, message) {
if (this.messages[locale]) { if (this.messages[locale]) {
Object.assign(this.messages[locale], message); Object.assign(this.messages[locale], message);
} }
...@@ -208,7 +213,7 @@ function getDefaultLocale() { ...@@ -208,7 +213,7 @@ function getDefaultLocale() {
} }
return uni.getSystemInfoSync().language; return uni.getSystemInfoSync().language;
} }
function initVueI18n(messages, fallbackLocale = 'en', locale) { function initVueI18n(messages = {}, fallbackLocale = LOCALE_EN, locale) {
const i18n = new I18n({ const i18n = new I18n({
locale: locale || fallbackLocale, locale: locale || fallbackLocale,
fallbackLocale, fallbackLocale,
...@@ -256,28 +261,16 @@ function initVueI18n(messages, fallbackLocale = 'en', locale) { ...@@ -256,28 +261,16 @@ function initVueI18n(messages, fallbackLocale = 'en', locale) {
t(key, values) { t(key, values) {
return t(key, values); return t(key, values);
}, },
add(locale, message) {
return i18n.add(locale, message);
},
getLocale() { getLocale() {
return i18n.getLocale(); return i18n.getLocale();
}, },
setLocale(newLocale) { setLocale(newLocale) {
return i18n.setLocale(newLocale); return i18n.setLocale(newLocale);
}, },
mixin: {
beforeCreate() {
const unwatch = i18n.watchLocale(() => {
this.$forceUpdate();
});
this.$once('hook:beforeDestroy', function () {
unwatch();
});
},
methods: {
$$t(key, values) {
return t(key, values);
},
},
},
}; };
} }
export { I18n, initVueI18n }; export { I18n, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT, initVueI18n };
import BaseFormatter from './format' import BaseFormatter from './format'
export const LOCALE_ZH_HANS = 'zh-Hans'
export const LOCALE_ZH_HANT = 'zh-Hant'
export const LOCALE_EN = 'en'
export const LOCALE_FR = 'fr'
export const LOCALE_ES = 'es'
// 中文 (简体),中文 (繁體),英语,法语,西班牙语 // 中文 (简体),中文 (繁體),英语,法语,西班牙语
export type BuiltInLocale = 'zh-Hans' | 'zh-Hant' | 'en' | 'fr' | 'es' export type BuiltInLocale =
| typeof LOCALE_ZH_HANS
| typeof LOCALE_ZH_HANT
| typeof LOCALE_EN
| typeof LOCALE_FR
| typeof LOCALE_ES
export type LocaleMessages = { export type LocaleMessages = {
[name in BuiltInLocale]?: Record<string, string> [name in BuiltInLocale]?: Record<string, string>
...@@ -22,7 +33,7 @@ export type LocaleWatcher = ( ...@@ -22,7 +33,7 @@ export type LocaleWatcher = (
export interface I18nOptions { export interface I18nOptions {
locale: BuiltInLocale locale: BuiltInLocale
fallbackLocale?: BuiltInLocale fallbackLocale?: BuiltInLocale
messages: LocaleMessages messages?: LocaleMessages
formater?: Formatter formater?: Formatter
watcher?: LocaleWatcher watcher?: LocaleWatcher
} }
...@@ -54,25 +65,25 @@ function normalizeLocale( ...@@ -54,25 +65,25 @@ function normalizeLocale(
locale = locale.toLowerCase() locale = locale.toLowerCase()
if (locale.indexOf('zh') === 0) { if (locale.indexOf('zh') === 0) {
if (locale.indexOf('-hans') !== -1) { if (locale.indexOf('-hans') !== -1) {
return 'zh-Hans' return LOCALE_ZH_HANS
} }
if (locale.indexOf('-hant') !== -1) { if (locale.indexOf('-hant') !== -1) {
return 'zh-Hant' return LOCALE_ZH_HANT
} }
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) { if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
return 'zh-Hant' return LOCALE_ZH_HANT
} }
return 'zh-Hans' return LOCALE_ZH_HANS
} }
const lang = startsWith(locale, ['en', 'fr', 'es']) const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES])
if (lang) { if (lang) {
return lang as BuiltInLocale return lang as BuiltInLocale
} }
} }
export class I18n { export class I18n {
private locale: BuiltInLocale = 'en' private locale: BuiltInLocale = LOCALE_EN
private fallbackLocale: BuiltInLocale = 'en' private fallbackLocale: BuiltInLocale = LOCALE_EN
private message: Record<string, string> = {} private message: Record<string, string> = {}
private messages: LocaleMessages = {} private messages: LocaleMessages = {}
private watchers: LocaleWatcher[] = [] private watchers: LocaleWatcher[] = []
...@@ -88,7 +99,7 @@ export class I18n { ...@@ -88,7 +99,7 @@ export class I18n {
this.fallbackLocale = fallbackLocale this.fallbackLocale = fallbackLocale
} }
this.formater = formater || defaultFormatter this.formater = formater || defaultFormatter
this.messages = messages this.messages = messages || {}
this.setLocale(locale) this.setLocale(locale)
if (watcher) { if (watcher) {
this.watchLocale(watcher) this.watchLocale(watcher)
...@@ -111,7 +122,7 @@ export class I18n { ...@@ -111,7 +122,7 @@ export class I18n {
this.watchers.splice(index, 1) this.watchers.splice(index, 1)
} }
} }
mergeLocaleMessage(locale: BuiltInLocale, message: Record<string, string>) { add(locale: BuiltInLocale, message: Record<string, string>) {
if (this.messages[locale]) { if (this.messages[locale]) {
Object.assign(this.messages[locale], message) Object.assign(this.messages[locale], message)
} else { } else {
......
import { I18n, BuiltInLocale, LocaleMessages } from './I18n' import { I18n, BuiltInLocale, LocaleMessages, LOCALE_EN } from './I18n'
type Interpolate = ( type Interpolate = (
key: string, key: string,
...@@ -30,8 +30,8 @@ function getDefaultLocale() { ...@@ -30,8 +30,8 @@ function getDefaultLocale() {
} }
export function initVueI18n( export function initVueI18n(
messages: LocaleMessages, messages: LocaleMessages = {},
fallbackLocale: BuiltInLocale = 'en', fallbackLocale: BuiltInLocale = LOCALE_EN,
locale?: BuiltInLocale locale?: BuiltInLocale
) { ) {
const i18n = new I18n({ const i18n = new I18n({
...@@ -79,26 +79,14 @@ export function initVueI18n( ...@@ -79,26 +79,14 @@ export function initVueI18n(
t(key: string, values?: Record<string, unknown> | Array<unknown>) { t(key: string, values?: Record<string, unknown> | Array<unknown>) {
return t(key, values) return t(key, values)
}, },
add(locale: BuiltInLocale, message: Record<string, string>) {
return i18n.add(locale, message)
},
getLocale() { getLocale() {
return i18n.getLocale() return i18n.getLocale()
}, },
setLocale(newLocale: BuiltInLocale) { setLocale(newLocale: BuiltInLocale) {
return i18n.setLocale(newLocale) return i18n.setLocale(newLocale)
}, },
mixin: {
beforeCreate() {
const unwatch = i18n.watchLocale(() => {
;(this as any).$forceUpdate()
})
;(this as any).$once('hook:beforeDestroy', function () {
unwatch()
})
},
methods: {
$$t(key: string, values?: any) {
return t(key, values)
},
},
},
} }
} }
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction } from '@vue/shared'; import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise } from '@vue/shared';
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
console.warn(`${name}: ${msg}`); console.warn(`${name}: ${msg}`);
...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) { ...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) {
} }
const formatArgs = options.formatArgs; const formatArgs = options.formatArgs;
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name](args[0][name], params); const formatterOrDefaultValue = formatArgs[name];
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params);
}
else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue;
}
}
}); });
return args; return args;
} }
......
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction } from '@vue/shared'; import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise } from '@vue/shared';
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
console.warn(`${name}: ${msg}`); console.warn(`${name}: ${msg}`);
...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) { ...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) {
} }
const formatArgs = options.formatArgs; const formatArgs = options.formatArgs;
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name](args[0][name], params); const formatterOrDefaultValue = formatArgs[name];
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params);
}
else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue;
}
}
}); });
return args; return args;
} }
......
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction } from '@vue/shared'; import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise } from '@vue/shared';
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
console.warn(`${name}: ${msg}`); console.warn(`${name}: ${msg}`);
...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) { ...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) {
} }
const formatArgs = options.formatArgs; const formatArgs = options.formatArgs;
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name](args[0][name], params); const formatterOrDefaultValue = formatArgs[name];
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params);
}
else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue;
}
}
}); });
return args; return args;
} }
......
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction } from '@vue/shared'; import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise } from '@vue/shared';
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
console.warn(`${name}: ${msg}`); console.warn(`${name}: ${msg}`);
...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) { ...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) {
} }
const formatArgs = options.formatArgs; const formatArgs = options.formatArgs;
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name](args[0][name], params); const formatterOrDefaultValue = formatArgs[name];
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params);
}
else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue;
}
}
}); });
return args; return args;
} }
......
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction } from '@vue/shared'; import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise } from '@vue/shared';
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
console.warn(`${name}: ${msg}`); console.warn(`${name}: ${msg}`);
...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) { ...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) {
} }
const formatArgs = options.formatArgs; const formatArgs = options.formatArgs;
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name](args[0][name], params); const formatterOrDefaultValue = formatArgs[name];
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params);
}
else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue;
}
}
}); });
return args; return args;
} }
......
import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isPromise, isFunction } from '@vue/shared'; import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawType, makeMap, isFunction, isPromise } from '@vue/shared';
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
console.warn(`${name}: ${msg}`); console.warn(`${name}: ${msg}`);
...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) { ...@@ -152,7 +152,16 @@ function formatApiArgs(args, options) {
} }
const formatArgs = options.formatArgs; const formatArgs = options.formatArgs;
Object.keys(formatArgs).forEach((name) => { Object.keys(formatArgs).forEach((name) => {
formatArgs[name](args[0][name], params); const formatterOrDefaultValue = formatArgs[name];
if (isFunction(formatterOrDefaultValue)) {
formatterOrDefaultValue(args[0][name], params);
}
else {
// defaultValue
if (!hasOwn(params, name)) {
params[name] = formatterOrDefaultValue;
}
}
}); });
return args; return args;
} }
......
...@@ -27,6 +27,11 @@ interface ManifestFeatures { ...@@ -27,6 +27,11 @@ interface ManifestFeatures {
promise: boolean promise: boolean
longpress: boolean longpress: boolean
routerMode: '"hash"' | '"history"' routerMode: '"hash"' | '"history"'
i18nEn: boolean
i18nEs: boolean
i18nFr: boolean
i18nZhHans: boolean
i18nZhHant: boolean
} }
function resolveProjectFeature( function resolveProjectFeature(
...@@ -156,6 +161,11 @@ function resolveManifestFeature( ...@@ -156,6 +161,11 @@ function resolveManifestFeature(
promise: false, promise: false,
longpress: true, longpress: true,
routerMode: '"hash"', routerMode: '"hash"',
i18nEn: true,
i18nEs: true,
i18nFr: true,
i18nZhHans: true,
i18nZhHant: true,
} }
const manifest = parse( const manifest = parse(
fs.readFileSync(path.join(options.inputDir, 'manifest.json'), 'utf8') fs.readFileSync(path.join(options.inputDir, 'manifest.json'), 'utf8')
...@@ -167,7 +177,29 @@ function resolveManifestFeature( ...@@ -167,7 +177,29 @@ function resolveManifestFeature(
) { ) {
features.routerMode = '"history"' features.routerMode = '"history"'
} }
// TODO manifest.json features const platform = manifest[options.platform] || {}
const manifestFeatures = platform.features
if (manifestFeatures) {
const { i18n } = manifestFeatures
if (isArray(i18n)) {
if (!i18n.includes('en')) {
features.i18nEn = false
}
if (!i18n.includes('es')) {
features.i18nEs = false
}
if (!i18n.includes('fr')) {
features.i18nFr = false
}
if (!i18n.includes('zh-Hans')) {
features.i18nZhHans = false
}
if (!i18n.includes('zh-Hant')) {
features.i18nZhHant = false
}
}
}
// TODO other features
return features return features
} }
...@@ -181,6 +213,11 @@ export function getFeatures( ...@@ -181,6 +213,11 @@ export function getFeatures(
wx, wx,
wxs, wxs,
nvue, nvue,
i18nEn,
i18nEs,
i18nFr,
i18nZhHans,
i18nZhHant,
pages, pages,
tabBar, tabBar,
tabBarMidButton, tabBarMidButton,
...@@ -205,6 +242,11 @@ export function getFeatures( ...@@ -205,6 +242,11 @@ export function getFeatures(
__UNI_FEATURE_WXS__: wxs, // 是否启用 wxs 支持,如:getComponentDescriptor 等(uni-core/src/view/plugin/appConfig) __UNI_FEATURE_WXS__: wxs, // 是否启用 wxs 支持,如:getComponentDescriptor 等(uni-core/src/view/plugin/appConfig)
__UNI_FEATURE_PROMISE__: promise, // 是否启用旧版本的 promise 支持(即返回[err,res]的格式),默认返回标准 __UNI_FEATURE_PROMISE__: promise, // 是否启用旧版本的 promise 支持(即返回[err,res]的格式),默认返回标准
__UNI_FEATURE_LONGPRESS__: longpress, // 是否启用longpress __UNI_FEATURE_LONGPRESS__: longpress, // 是否启用longpress
__UNI_FEATURE_I18N_EN__: i18nEn, // 是否启用en
__UNI_FEATURE_I18N_ES__: i18nEs, // 是否启用es
__UNI_FEATURE_I18N_FR__: i18nFr, // 是否启用fr
__UNI_FEATURE_I18N_ZH_HANS__: i18nZhHans, // 是否启用zh_Hans
__UNI_FEATURE_I18N_ZH_HANT__: i18nZhHant, // 是否启用zh_Hant
// 以下特性,编译器已自动识别是否需要启用 // 以下特性,编译器已自动识别是否需要启用
__UNI_FEATURE_NVUE__: nvue, // 是否启用nvue __UNI_FEATURE_NVUE__: nvue, // 是否启用nvue
__UNI_FEATURE_ROUTER_MODE__: routerMode, // 路由模式 __UNI_FEATURE_ROUTER_MODE__: routerMode, // 路由模式
......
const fs = require('fs')
const path = require('path')
const locales = ['en', 'es', 'fr', 'zh-Hans', 'zh-Hant']
function buildI18n(namespace, dir) {
const modules = {}
locales.forEach((locale) => {
const messages = buildI18nLocale(locale, namespace, dir)
Object.keys(messages).forEach((moduleName) => {
;(modules[moduleName] || (modules[moduleName] = {}))[locale] =
messages[moduleName]
})
})
// modules
// {app: {en: { quit: '' },es: { quit: '' },fr: { quit: "" },'zh-Hans': { quit: '' },'zh-Hant': { quit: '' }}}
const messagesFile = path.resolve(dir, 'messages.ts')
fs.writeFileSync(messagesFile, generateI18nCode(namespace, modules))
console.log('write:' + messagesFile)
}
function buildI18nLocale(locale, namespace, dir) {
return buildI18nModuleMessages(
require(path.resolve(dir, locale + '.json')),
namespace
)
}
function buildI18nModuleMessages(messages, namespace) {
const modules = {}
Object.keys(messages).forEach((name) => {
const [module, ...part] = name.replace(namespace + '.', '').split('.')
;(modules[module] || (modules[module] = {}))[part.join('.')] =
messages[name]
})
return modules
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
function generateI18nCode(namespace, modules) {
return (
`
// This file is created by scripts/i18n.js
// Do not modify this file!!!!!!!!!
import {
LOCALE_EN,
LOCALE_ES,
LOCALE_FR,
LOCALE_ZH_HANS,
LOCALE_ZH_HANT,
} from '@dcloudio/uni-i18n'
import { useI18n } from './useI18n'
const i18n = useI18n()
function normalizeMessages(
namespace: string,
messages: Record<string, string>
) {
return Object.keys(messages).reduce<Record<string, string>>((res, name) => {
res[namespace + name] = messages[name]
return res
}, {})
}
` +
Object.keys(modules)
.map((name) => generateI18nModuleCode(namespace, name, modules[name]))
.join('')
)
}
function generateI18nModuleCode(namespace, name, localeMessages) {
return `export function initI18n${capitalize(name)}Msgs() {
const name = '${namespace}.${name}.'
${Object.keys(localeMessages)
.map((locale) => generateI18nModuleLocaleCode(locale, localeMessages[locale]))
.join('')}
}
`
}
function generateI18nModuleLocaleCode(locale, messages) {
locale = locale.toUpperCase().replace('-', '_')
return ` if (__UNI_FEATURE_I18N_${locale}__) {
i18n.add(LOCALE_${locale}, normalizeMessages(name, ${JSON.stringify(
messages
)}))
}
`
}
buildI18n('uni', path.resolve(__dirname, '../packages/uni-core/src/i18n'))
...@@ -12,8 +12,8 @@ const priority = { ...@@ -12,8 +12,8 @@ const priority = {
'uni-mp-toutiao': 70, 'uni-mp-toutiao': 70,
'uni-mp-weixin': 70, 'uni-mp-weixin': 70,
'uni-quickapp-webview': 70, 'uni-quickapp-webview': 70,
'uni-h5-vue': 60, 'uni-h5': 60,
'uni-h5': 50, 'uni-h5-vue': 50,
'uni-cli-shared': 40, 'uni-cli-shared': 40,
'vite-plugin-uni': 30, 'vite-plugin-uni': 30,
'size-check': 1, 'size-check': 1,
......
...@@ -2192,9 +2192,9 @@ ecc-jsbn@~0.1.1: ...@@ -2192,9 +2192,9 @@ ecc-jsbn@~0.1.1:
safer-buffer "^2.1.0" safer-buffer "^2.1.0"
electron-to-chromium@^1.3.712: electron-to-chromium@^1.3.712:
version "1.3.713" version "1.3.715"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.713.tgz#4583efb17f2d1e9ec07a44c8004ea73c013ad146" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.715.tgz#8fd002e79c13d711133565600f40cd80abfe5d55"
integrity sha512-HWgkyX4xTHmxcWWlvv7a87RHSINEcpKYZmDMxkUlHcY+CJcfx7xEfBHuXVsO1rzyYs1WQJ7EgDp2CoErakBIow== integrity sha512-VCWxo9RqTYhcCsHtG+l0TEOS6H5QmO1JyVCQB9nv8fllmAzj1VcCYH3qBCXP75/En6FeoepefnogLPE+5W7OiQ==
elliptic@^6.5.3: elliptic@^6.5.3:
version "6.5.4" version "6.5.4"
...@@ -5812,9 +5812,9 @@ symbol-tree@^3.2.4: ...@@ -5812,9 +5812,9 @@ symbol-tree@^3.2.4:
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw== integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==
table@^6.0.4: table@^6.0.4:
version "6.0.9" version "6.1.0"
resolved "https://registry.yarnpkg.com/table/-/table-6.0.9.tgz#790a12bf1e09b87b30e60419bafd6a1fd85536fb" resolved "https://registry.yarnpkg.com/table/-/table-6.1.0.tgz#676a0cfb206008b59e783fcd94ef8ba7d67d966c"
integrity sha512-F3cLs9a3hL1Z7N4+EkSscsel3z55XT950AvB05bwayrNg5T1/gykXtigioTAjbltvbMSJvvhFCbnf6mX+ntnJQ== integrity sha512-T4G5KMmqIk6X87gLKWyU5exPpTjLjY5KyrFWaIjv3SvgaIUGXV7UEzGEnZJdTA38/yUS6f9PlKezQ0bYXG3iIQ==
dependencies: dependencies:
ajv "^8.0.1" ajv "^8.0.1"
is-boolean-object "^1.1.0" is-boolean-object "^1.1.0"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册