From 900ca99e11a2ef251d2cee8e074845987184fbbf Mon Sep 17 00:00:00 2001 From: linju-json Date: Sat, 2 Oct 2021 20:21:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=98=AF=E5=90=A6=E5=BC=80=E5=90=AFi18n?= =?UTF-8?q?=E5=A4=9A=E8=AF=AD=E8=A8=80=E5=9B=BD=E9=99=85=E5=8C=96=E3=80=82?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 25 ---- README.md | 48 +++++-- changelog.md | 8 ++ lang/i18n.js | 135 ++++++++++++------ package.json | 2 +- pages/list/list.nvue | 2 +- pages/ucenter/settings/settings.vue | 7 +- uni-starter.config.js | 4 + .../pages/opendb-feedback/opendb-feedback.vue | 1 - 9 files changed, 144 insertions(+), 88 deletions(-) diff --git a/App.vue b/App.vue index 83f846e3..418f9dd3 100644 --- a/App.vue +++ b/App.vue @@ -43,31 +43,6 @@ }); }*/ // #endif - - let initLanguageAfter = () => { - console.log('作用于多语言国际化功能,这里获取语言包lang/en.js、lang/zh-Hans.js的tabbar中配置的值更新当前应用的底部tabbar,如果你不需要多语言国际化。直接删除App.vue页面的47-55行即可'); - //底部tabbar更新 - this.$t('tabbar').split(',').forEach((text, index) => { - uni.setTabBarItem({ - index, - text, - complete: e => { - // console.log("e: " + JSON.stringify(e)); - } - }) - }) - //更新 uni-starter.config - //agreements - let agreementsTitle = this.$t('agreementsTitle').split(',') - let agreements = getApp().globalData.config.about.agreements agreements[0].title = agreementsTitle[0] agreements[1].title = agreementsTitle[1] getApp().globalData.config.about.agreements = agreements - } - setTimeout(()=>{ - initLanguageAfter() - },1000) - uni.$on('changeLanguage', e => { - console.log('changeLanguage',e); - initLanguageAfter(e) - }) }, onShow: function() { console.log('App Show') diff --git a/README.md b/README.md index 6589eae5..0877cde8 100644 --- a/README.md +++ b/README.md @@ -61,19 +61,36 @@ uni-starter + uniCloud admin,提供了用户端和管理端的基础模板, 你只需要在项目根目录的框架配置文件:`uni-starter.config.js`的`router`->`needLogin`和`login`中配置完整的路由名称即可: ```js "router": { - //needLogin:配置强制需要登陆的页面,在打开这些页面之前会自动检查(前端校验)uni_id_token的值是否有效,如果无效会自动跳转到登陆页面 - "needLogin": [ - "/pages/ucenter/userinfo/userinfo", - "/uni_modules/uni-news-favorite/pages/uni-news-favorite/list", - "/uni_modules/uni-feedback/pages/uni-feedback/add" - ], - /* - login:配置登陆类型与优先级 - 未列举到的,或设备环境不支持的选项,将被隐藏。如果你需要在不同平台有不同的配置,直接用条件编译即可 - 根据数组的第0项,决定登录方式的第一优先级。 - 详情下文:2.登录配置详解 - */ - "login": ["username","smsCode","univerify", "weixin", "apple"], +/*名词解释:“强制登陆页” + 在打开定义的需强制登陆的页面之前会自动检查(前端校验)uni_id_token的值是否有效, + 如果无效会自动跳转到登陆页面 +两种模式: + 1.needLogin:黑名单模式。枚举游客不可访问的页面。 + 2.visitor:白名单模式。枚举游客可访问的页面。 +* 注意:黑名单与白名单模式二选一 */ + //needLogin:配置强制需要登陆的页面,在打开这些页面之前会自动检查(前端校验)uni_id_token的值是否有效,如果无效会自动跳转到登陆页面 + // "needLogin" : [ + // {pattern:/^\/pages\/list.*/}, //支持正则表达式 + // "/uni_modules/uni-news-favorite/pages/uni-news-favorite/list", + // "/uni_modules/uni-feedback/pages/uni-feedback/add" + // ], + "visitor" : [ + "/",//注意入口页必须直接写 "/" + {"pattern":/^\/pages\/list.*/}, //支持正则表达式 + {"pattern":/^\/pages\/ucenter\/login-page.*/}, + "/pages/common/webview/webview", + "/pages/grid/grid", + "/pages/ucenter/ucenter", + "/pages/ucenter/guestbook/guestbook", + "/pages/ucenter/about/about", + "/pages/ucenter/settings/settings" + ], + /* + login:配置登陆类型与优先级 + 未列举到的,或设备环境不支持的选项,将被隐藏。如果你需要在不同平台有不同的配置,直接用条件编译即可 + 根据数组的第0项,决定登录方式的第一优先级。 + */ + "login": ["username","univerify","weixin", "smsCode", "apple"] } ``` @@ -366,6 +383,10 @@ module.exports = { "marketId":{ "ios":"id1417078253", "android":"123456" + }, + //配置多语言国际化。i18n为英文单词 internationalization的首末字符i和n,18为中间的字符数 是“国际化”的简称 + "i18n":{ + "enable":false //默认启用,国际化。如果你不想使用国际化相关功能,请改为false } } ``` @@ -462,7 +483,6 @@ uni-starter 1. 判断当前用户是否拥有某角色`uniIDHasRole` 演示页面:`/pages/grid/grid` [API文档详情:](https://uniapp.dcloud.io/api/global?id=uniidhasrole) 2. 指纹解锁、人脸解锁 演示页面:`/pages/ucenter/settings/settings` [API文档详情:](https://uniapp.dcloud.io/api/system/authentication) - ### 注意事项 1. 真机运行需要制作自定义基座,制作后选择运行到自定义基座 2. 苹果登录的图标,需要满足苹果应用市场的审核规范请勿随便修改;如需修改请先阅读:[Sign in with Apple Button](https://appleid.apple.com/signinwithapple/button) diff --git a/changelog.md b/changelog.md index 062162ad..94057126 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,11 @@ +## 1.1.15(2021-10-02) +新增,支持配置是否开启i18n多语言国际化。 +配置文件:`uni-starter.config.js` +` +"i18n":{ + "enable":true //默认启用,国际化。如果你不想使用国际化相关功能,请改为false +} +` ## 1.1.14(2021-09-30) 1. 通过微信小程序登陆自动保存`sessionKey`到`uni-id-users`表 2. 我的-设置-个人资料 点击绑定手机号码,完善账号信息支持以下三种策略: diff --git a/lang/i18n.js b/lang/i18n.js index 5a3e2211..51260d80 100644 --- a/lang/i18n.js +++ b/lang/i18n.js @@ -1,51 +1,98 @@ -import langEn from './en' -import zhHans from './zh-Hans' -const messages = { - 'en': langEn, - 'zh-Hans': zhHans +import langEn from './en' +import zhHans from './zh-Hans' +import uniStarterConfig from '../uni-starter.config.js' +const {i18n:{enable:i18nEnable} }= uniStarterConfig +const messages = { + 'en': langEn, + 'zh-Hans': zhHans } -let currentLang = uni.getStorageSync('CURRENT_LANG') +let currentLang +if(i18nEnable){ + currentLang = uni.getStorageSync('CURRENT_LANG') +}else{ + currentLang = "zh-Hans" +} +console.log(uni.getStorageSync('CURRENT_LANG'),currentLang); if (!currentLang) { - if(uni.getLocale){ - console.log('获取应用语言:',uni.getLocale() ); - let language = 'zh-Hans' - if(uni.getLocale() != 'zh-Hans'){ - language = 'en' - } - uni.setStorageSync('CURRENT_LANG', language) - currentLang = language - }else{ - uni.getSystemInfo({ - success: function (res) { - console.log('获取设备信息:',res); - let language = 'zh-Hans' - if(res.language == 'en'){ - language = 'en' - } - uni.setStorageSync('CURRENT_LANG', language) - currentLang = language - }, - fail: (err) => { - console.error(err) - } - }) - } -} -let i18nConfig = { - locale: currentLang, // set locale - messages // set locale messages -} - -// #ifdef VUE2 + if (uni.getLocale) { + console.log('获取应用语言:', uni.getLocale()); + let language = 'en' + if (uni.getLocale() != 'en') { + language = 'zh-Hans' + } + uni.setStorageSync('CURRENT_LANG', language) + currentLang = language + } else { + uni.getSystemInfo({ + success: function(res) { + console.log('获取设备信息:', res); + let language = 'zh-Hans' + if (res.language == 'en') { + language = 'en' + } + uni.setStorageSync('CURRENT_LANG', language) + currentLang = language + }, + fail: (err) => { + console.error(err) + } + }) + } +} +let i18nConfig = { + locale: currentLang, // set locale + messages // set locale messages +} + +// #ifdef VUE2 import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n) -const i18n = new VueI18n(i18nConfig) -// #endif +const i18n = new VueI18n(i18nConfig) +// #endif + +// #ifdef VUE3 +import { + createI18n +} from 'vue-i18n' +const i18n = createI18n(i18nConfig) +// #endif + +export default i18n -// #ifdef VUE3 -import {createI18n} from 'vue-i18n' -const i18n = createI18n(i18nConfig) -// #endif -export default i18n \ No newline at end of file +if(i18nEnable){ + console.log(1); + let initLanguageAfter = () => { + //获得定义的文字 + console.log($i18n('tabbar').split(',')); + function $i18n(e){ + return i18n.messages[i18n.locale][e] + } + setTimeout(function(){ + //底部tabbar更新 + $i18n('tabbar').split(',').forEach((text, index) => { + console.log(text); + uni.setTabBarItem({ + index, + text, + complete: e => { + console.log("e: " + JSON.stringify(e)); + } + }) + }) + },1) + //更新 uni-starter.config + //agreements + let agreementsTitle = $i18n('agreementsTitle').split(',') + let agreements = uniStarterConfig.about.agreements + agreements[0].title = agreementsTitle[0] + agreements[1].title = agreementsTitle[1] + uniStarterConfig.about.agreements = agreements + } + initLanguageAfter() + uni.$on('changeLanguage', e => { + console.log('changeLanguage', e); + initLanguageAfter(e) + }) +} \ No newline at end of file diff --git a/package.json b/package.json index 2462f87f..27c26d63 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "id": "uni-starter", "displayName": "uni-starter", - "version": "1.1.14", + "version": "1.1.15", "description": "云端一体应用快速开发基本项目模版", "keywords": [ "login", diff --git a/pages/list/list.nvue b/pages/list/list.nvue index 7cfea1b8..e184fec8 100644 --- a/pages/list/list.nvue +++ b/pages/list/list.nvue @@ -138,7 +138,7 @@ console.log('refresh'); }, onqueryerror(e) { - console.log(e); + console.error(e); } }, onPullDownRefresh() { diff --git a/pages/ucenter/settings/settings.vue b/pages/ucenter/settings/settings.vue index 34213445..198fcf65 100644 --- a/pages/ucenter/settings/settings.vue +++ b/pages/ucenter/settings/settings.vue @@ -15,7 +15,7 @@ - + @@ -50,7 +50,10 @@ ...mapGetters({ 'userInfo': 'user/info', 'hasLogin': 'user/hasLogin', - }) + }), + i18nEnable(){ + return getApp().globalData.config.i18n.enable + } }, onLoad() { this.uniToken = uni.getStorageSync('uni_id_token') diff --git a/uni-starter.config.js b/uni-starter.config.js index 48d43012..c2967698 100644 --- a/uni-starter.config.js +++ b/uni-starter.config.js @@ -86,5 +86,9 @@ export default { "marketId":{ "ios":"id1417078253", "android":"123456" + }, + //配置多语言国际化。i18n为英文单词 internationalization的首末字符i和n,18为中间的字符数 是“国际化”的简称 + "i18n":{ + "enable":true //默认启用,国际化。如果你不想使用国际化相关功能,请改为false } } diff --git a/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback.vue b/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback.vue index 40db16bf..9c30b673 100644 --- a/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback.vue +++ b/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback.vue @@ -26,7 +26,6 @@ import { validator } from '../../js_sdk/validator/opendb-feedback.js'; - console.log(validator); const db = uniCloud.database(); const dbCollectionName = 'opendb-feedback'; -- GitLab