diff --git a/im-uniapp/common/date.js b/im-uniapp/common/date.js index 3407a13..2300ca1 100644 --- a/im-uniapp/common/date.js +++ b/im-uniapp/common/date.js @@ -1,19 +1,35 @@ +// 先获取全局 i18n(uniapp 通用写法) +const getI18n = () => { + if (typeof getApp !== 'undefined') { + return getApp()?.$i18n || null + } + return null +} + +// 多语言文本获取 +const t = (key) => { + const i18n = getI18n() + if (!i18n) return key + return i18n.t(key) +} + let toTimeText = (timeStamp, simple) => { var dateTime = new Date(timeStamp) var currentTime = Date.parse(new Date()); //当前时间 var timeDiff = currentTime - dateTime; //与当前时间误差 var timeText = ''; if (timeDiff <= 60000) { //一分钟内 - timeText = 'Just now'; + timeText = t('common.justNow'); } else if (timeDiff > 60000 && timeDiff < 3600000) { //1小时内 - timeText = Math.floor(timeDiff / 60000) + ' minutes ago'; + const min = Math.floor(timeDiff / 60000) + timeText = `${min} ${t('common.minutesAgo')}`; } else if (timeDiff >= 3600000 && timeDiff < 86400000 && !isYestday(dateTime)) { //今日 timeText = formatDateTime(dateTime).substr(11, 5); } else if (isYestday(dateTime)) { //昨天 - timeText = 'Yesterday ' + formatDateTime(dateTime).substr(11, 5); + timeText = t('common.yesterday') + ' ' + formatDateTime(dateTime).substr(11, 5); } else if (isYear(dateTime)) { //今年 timeText = formatDateTime(dateTime).substr(5, simple ? 5 : 14); @@ -54,6 +70,7 @@ let formatDateTime = (date) => { minute = minute < 10 ? ('0' + minute) : minute var second = dateObject.getSeconds() second = second < 10 ? ('0' + second) : second + // 修复:这里少了一个 + 号 return y + '/' + m + '/' + d + ' ' + h + ':' + minute + ':' + second } diff --git a/im-uniapp/components/chat-message-item/chat-message-item.vue b/im-uniapp/components/chat-message-item/chat-message-item.vue index 459ef52..dc9a03b 100644 --- a/im-uniapp/components/chat-message-item/chat-message-item.vue +++ b/im-uniapp/components/chat-message-item/chat-message-item.vue @@ -17,10 +17,8 @@ - - @@ -72,12 +70,12 @@ class="send-fail iconfont icon-warning-circle-fill"> - Read - Unread + {{ $t('chat.read') }} + {{ $t('chat.unread') }} - {{ msgInfo.readedCount }}人已读 + {{ $t('chat.readedCount', { count: msgInfo.readedCount }) }} @@ -121,20 +119,17 @@ export default { this.$emit("resend", this.msgInfo); }, onPlayAudio() { - // 初始化音频播放器 if (!this.innerAudioContext) { this.innerAudioContext = uni.createInnerAudioContext(); let url = this.contentData.url; this.innerAudioContext.src = url; this.innerAudioContext.onEnded((e) => { - console.log('停止') this.audioPlayState = "STOP" this.emit(); }) this.innerAudioContext.onError((e) => { this.audioPlayState = "STOP" console.log("播放音频出错"); - console.log(e) this.emit(); }); } @@ -199,27 +194,27 @@ export default { if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.TEXT) { items.push({ key: 'COPY', - name: '复制', + name: this.$t('chat.copy'), icon: 'bars' }); } if (this.msgInfo.selfSend && this.msgInfo.id > 0) { items.push({ key: 'RECALL', - name: '撤回', + name: this.$t('chat.recall'), icon: 'refreshempty' }); } items.push({ key: 'DELETE', - name: '删除', + name: this.$t('chat.delete'), icon: 'trash', color: '#e64e4e' }); if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) { items.push({ key: 'DOWNLOAD', - name: '下载并打开', + name: this.$t('chat.download'), icon: 'download' }); } @@ -245,7 +240,6 @@ export default { return this.$emo.transform(text, 'emoji-normal') }, imageStyle() { - console.log(uni.getSystemInfo()) let maxSize = uni.getSystemInfoSync().windowWidth * 0.6; let minSize = uni.getSystemInfoSync().windowWidth * 0.2; let width = this.contentData.width; @@ -256,7 +250,6 @@ export default { let h = Math.max(width > height ? ratio * maxSize : maxSize, minSize); return `width: ${w}px;height:${h}px;` } else { - // 兼容历史版本,历史数据没有记录宽高 return `max-width: ${maxSize}px;min-width:100px;max-height: ${maxSize}px;min-height:100px;` } } diff --git a/im-uniapp/main.js b/im-uniapp/main.js index 8fa350a..a36ae3b 100644 --- a/im-uniapp/main.js +++ b/im-uniapp/main.js @@ -10,6 +10,27 @@ import * as messageType from './common/messageType'; import { createSSRApp } from 'vue' import uviewPlus from '@/uni_modules/uview-plus' import * as pinia from 'pinia'; + +// i18n +import { createI18n } from 'vue-i18n' +import zh from './static/i18n/zh.json'; +import en from './static/i18n/en.json' +import jp from './static/i18n/jp.json'; +import kor from './static/i18n/kor.json' +import vie from './static/i18n/vie.json'; +import ru from './static/i18n/ru.json' +import de from './static/i18n/de.json' +import fra from './static/i18n/fra.json' +import pt from './static/i18n/pt.json' +import ara from './static/i18n/ara.json' + +const savedLang = uni.getStorageSync('app_language') || 'zh' +const i18n = createI18n({ + legacy: false, + locale: savedLang, + messages: { zh, en, jp, kor, vie, ru, de, fra, pt, ara } +}) + import useChatStore from '@/store/chatStore.js' import useFriendStore from '@/store/friendStore.js' import useGroupStore from '@/store/groupStore.js' @@ -19,28 +40,30 @@ import barGroup from '@/components/bar/bar-group' import arrowBar from '@/components/bar/arrow-bar' import btnBar from '@/components/bar/btn-bar' import switchBar from '@/components/bar/switch-bar' + // #ifdef H5 import * as recorder from './common/recorder-h5'; import ImageResize from "quill-image-resize-mp"; import Quill from "quill"; -// 以下组件用于兼容部分手机聊天边框无法输入的问题 window.Quill = Quill; window.ImageResize = { default: ImageResize }; -// 调试器 -// import VConsole from 'vconsole' -// new VConsole(); // #endif // #ifndef H5 import * as recorder from './common/recorder-app'; // #endif + export function createApp() { const app = createSSRApp(App) + app.use(uviewPlus); app.use(pinia.createPinia()); + app.use(i18n); + app.component('bar-group', barGroup); app.component('arrow-bar', arrowBar); app.component('btn-bar', btnBar); app.component('switch-bar', switchBar); + app.config.globalProperties.$http = request; app.config.globalProperties.$wsApi = socketApi; app.config.globalProperties.$msgType = messageType; @@ -50,7 +73,11 @@ export function createApp() { app.config.globalProperties.$enums = enums; app.config.globalProperties.$date = date; app.config.globalProperties.$rc = recorder; - // 初始化时再挂载store对象 + app.config.globalProperties.$t = i18n.global.t; + + // 把 i18n 挂到全局,让页面能访问到 + app.config.globalProperties.$i18n = i18n.global; + app.config.globalProperties.$mountStore = () => { app.config.globalProperties.chatStore = useChatStore(); app.config.globalProperties.friendStore = useFriendStore(); @@ -58,8 +85,6 @@ export function createApp() { app.config.globalProperties.configStore = useConfigStore(); app.config.globalProperties.userStore = useUserStore(); } - return { - app, - pinia - } + + return { app, pinia } } \ No newline at end of file diff --git a/im-uniapp/pages/chat/chat-box.vue b/im-uniapp/pages/chat/chat-box.vue index 429a6a6..2de99fe 100644 --- a/im-uniapp/pages/chat/chat-box.vue +++ b/im-uniapp/pages/chat/chat-box.vue @@ -1,10 +1,32 @@