Skip to content

Commit

Permalink
feat(i18n): init
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Feb 23, 2021
1 parent 7e02674 commit b6abfa4
Show file tree
Hide file tree
Showing 20 changed files with 485 additions and 284 deletions.
4 changes: 3 additions & 1 deletion build/rollup.config.app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path')
const json = require('@rollup/plugin-json')
const alias = require('@rollup/plugin-alias')
const replace = require('@rollup/plugin-replace')
const nodeResolve = require('@rollup/plugin-node-resolve')
Expand Down Expand Up @@ -88,7 +89,8 @@ module.exports = {
find: 'uni-api-protocol',
replacement: resolve('src/core/helpers/protocol')
}]
}),
}),
json(),
nodeResolve(),
requireContext(),
commonjs(),
Expand Down
5 changes: 3 additions & 2 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const provides = {
}
if (process.env.UNI_VIEW) { // 方便调试
delete provides.console
}
}

if (process.env.UNI_VIEW === 'true') {
alias.vue$ = resolve('packages/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js')
Expand All @@ -66,7 +66,8 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(pkg.version),
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM)
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM),
__VIEW__: JSON.stringify(!!process.env.UNI_VIEW)
}),
new webpack.ProvidePlugin(provides)
]
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"release:v3": "npm run lint:cli && lerna publish --no-git-tag-version --force-publish=* --npm-tag=v3"
},
"dependencies": {
"@dcloudio/uni-i18n": "^0.0.1",
"base64-arraybuffer": "^0.2.0",
"intersection-observer": "^0.7.0",
"pako": "^1.0.11",
Expand All @@ -43,6 +44,7 @@
"devDependencies": {
"@rollup/plugin-alias": "^3.1.0",
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.1",
"@types/html5plus": "^1.0.0",
Expand Down Expand Up @@ -118,6 +120,8 @@
"__registerPage": true,
"UniViewJSBridge": true,
"UniServiceJSBridge": true,
"__DEV__": true,
"__VIEW__": true,
"__PLATFORM__": true,
"__VERSION__": true,
"__GLOBAL__": true,
Expand All @@ -144,4 +148,4 @@
"main": "index.js",
"description": "",
"author": ""
}
}
9 changes: 9 additions & 0 deletions src/core/helpers/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"uni.showActionSheet.cancel": "cancel",
"uni.showToast.unpaired": "Please note showToast must be paired with hideToast",
"uni.showLoading.unpaired": "Please note showLoading must be paired with hideLoading",
"uni.showModal.cancel": "cancel",
"uni.showModal.confirm": "confirm",
"uni.button.feedback.title": "feedback",
"uni.button.feedback.send": "send"
}
9 changes: 9 additions & 0 deletions src/core/helpers/i18n/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"uni.showActionSheet.cancel": "cancelar",
"uni.showToast.unpaired": "Tenga en cuenta que showToast debe estar emparejado con hideToast",
"uni.showLoading.unpaired": "Tenga en cuenta que showLoading debe estar emparejado con hideLoading",
"uni.showModal.cancel": "cancelar",
"uni.showModal.confirm": "confirmar",
"uni.button.feedback.title": "realimentación",
"uni.button.feedback.send": "enviar"
}
9 changes: 9 additions & 0 deletions src/core/helpers/i18n/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"uni.showActionSheet.cancel": "Annuler",
"uni.showToast.unpaired": "Veuillez noter que showToast doit être associé à hideToast",
"uni.showLoading.unpaired": "Veuillez noter que showLoading doit être associé à hideLoading",
"uni.showModal.cancel": "Annuler",
"uni.showModal.confirm": "confirmer",
"uni.button.feedback.title": "retour d'information",
"uni.button.feedback.send": "envoyer"
}
66 changes: 66 additions & 0 deletions src/core/helpers/i18n/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import i18n 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'

export function initI18n (locale, onChange) {
i18n.init({
locale,
fallbackLocale,
messages
})
if (onChange) {
i18n.watchLocale((newLocale, oldLocale) => {
onChange(newLocale, oldLocale)
})
}
}

function initLocaleWatcher (app) {
app.$i18n.vm.$watch('locale', (newLocale) => {
i18n.setLocale(newLocale)
}, {
immediate: true
})
}

export function t (key, values) {
if (__VIEW__) {
return i18n.t(key, values)
}
const app = getApp()
if (!app.$t) {
/* eslint-disable no-func-assign */
t = function (key, values) {
return i18n.t(key, values)
}
} else {
initLocaleWatcher(app)
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = app.$i18n
const silentTranslationWarn = $i18n.silentTranslationWarn
$i18n.silentTranslationWarn = true
const msg = app.$t(key, values)
$i18n.silentTranslationWarn = silentTranslationWarn
if (msg !== key) {
return msg
}
return i18n.t(key, values)
}
}
return t(key, values)
}
9 changes: 9 additions & 0 deletions src/core/helpers/i18n/zh-Hans.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"uni.showActionSheet.cancel": "取消",
"uni.showToast.unpaired": "请注意 showToast 与 hideToast 必须配对使用",
"uni.showLoading.unpaired": "请注意 showLoading 与 hideLoading 必须配对使用",
"uni.showModal.cancel": "取消",
"uni.showModal.confirm": "确认",
"uni.button.feedback.title": "问题反馈",
"uni.button.feedback.send": "发送"
}
9 changes: 9 additions & 0 deletions src/core/helpers/i18n/zh-Hant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"uni.showActionSheet.cancel": "取消",
"uni.showToast.unpaired": "請注意 showToast 與 hideToast 必須配對使用",
"uni.showLoading.unpaired": "請注意 showLoading 與 hideLoading 必須配對使用",
"uni.showModal.cancel": "取消",
"uni.showModal.confirm": "確認",
"uni.button.feedback.title": "問題反饋",
"uni.button.feedback.send": "發送"
}
14 changes: 11 additions & 3 deletions src/core/helpers/protocol/ui/popup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
t
} from 'uni-core/helpers/i18n'

import getRealPath from 'uni-platform/helpers/get-real-path'

export const showModal = {
Expand All @@ -15,15 +19,19 @@ export const showModal = {
},
cancelText: {
type: String,
default: '取消'
default () {
return t('uni.showModal.cancel')
}
},
cancelColor: {
type: String,
default: '#000000'
},
confirmText: {
type: String,
default: '确定'
default () {
return t('uni.showModal.confirm')
}
},
confirmColor: {
type: String,
Expand Down Expand Up @@ -114,4 +122,4 @@ export const showActionSheet = {
popover: {
type: Object
}
}
}
13 changes: 11 additions & 2 deletions src/core/service/plugins/app/create-app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
t,
initI18n
} from 'uni-core/helpers/i18n'

import initRouterGuard from './router-guard'

let appVm = false
Expand All @@ -23,7 +28,8 @@ export function getCurrentPages (isAll = false, ignoreError = false) {
childrenVm = layoutVm
}
childrenVm.$children.forEach(vm => {
if (tabBarVm !== vm && vm.$children.length && vm.$children[0].$options.name === 'Page' && vm.$children[0].$slots.page) {
if (tabBarVm !== vm && vm.$children.length && vm.$children[0].$options.name === 'Page' && vm.$children[0].$slots
.page) {
// vm.$children[0]=Page->PageBody->RealPage
const pageBody = vm.$children[0].$children.find(vm => vm.$options.name === 'PageBody')
const pageVm = pageBody && pageBody.$children.find(vm => !!vm.$page)
Expand Down Expand Up @@ -64,8 +70,11 @@ export function getCurrentPages (isAll = false, ignoreError = false) {

export default function createApp (vm, routes) {
appVm = vm
appVm.$$t = t
appVm.globalData = appVm.$options.globalData || {}

// h5
initI18n(navigator.userLanguage || navigator.language)
// initEvents(appVm)
initRouterGuard(appVm, routes)
}
}
Loading

0 comments on commit b6abfa4

Please sign in to comment.