【uniapp】微信小程序国际化详细版

这篇具有很好参考价值的文章主要介绍了【uniapp】微信小程序国际化详细版。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、步骤

1、main.js 引入并初始化 VueI18n 

2、创建文件夹locale

二、应用

1、页面(固定数据)

2、动态数据

3、系统


官网详解:https://uniapp.dcloud.net.cn/tutorial/i18n.html

我创建项目的时候选择的模板是uni-ui项目,所以不需要npm  vue-i18n

一、步骤

1、main.js 引入并初始化 VueI18n 

import messages from './locale/index'//(1)

let i18nConfig = { //(2)
	locale: uni.getLocale(),//获取系统语言
	messages
}



// #ifndef VUE3
import Vue from 'vue'
import App from './App'
import store from './store'
import api from '@/http/vmeitime-http/index.js'
import share from '@/utils/share.js'
Vue.mixin(share)

import VueI18n from 'vue-i18n'//(3)
Vue.use(VueI18n)//(4)
const i18n = new VueI18n(i18nConfig)//(5)

Vue.config.productionTip = false
Vue.prototype.$store = store
Vue.prototype.api = api

App.mpType = 'app'


const app = new Vue({
	...App,
	store,
	share,
	i18n,//(6)
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
import App from './App.vue'
//(7)
import {
	createI18n
} from 'vue-i18n'
const i18n = createI18n(i18nConfig)//(8)
export function createApp() {
	const app = createSSRApp(App)
	app.use(i18n)//(9)
	return {
		app
	}
}
// #endif

2、创建文件夹locale

locale文件夹是与pages同级

locale/en.json(英文模板)

{
	"locale.auto": "System",
	"locale.en": "English",
	"locale.zh-hans": "简体中文",
	"index.scene": "Scene",
	"index.company": "Company Profile",
	"index.package": "Package",
	"index.details": "Details",
	"word.whole": "whole",
	"word.download": "Download Records",
	"word.preservation": "Transfer to my mobile phone",
	"word.forward": "Forward to friends",
	"me.WeChat": "WeChat name",
	"me.message": "Message feedback",
	"me.myDownloads": "My Downloads",
	"me.contact": "contact us",
	"me.logout": "Log out"
}

locale/ zh-Hans(简体中文)

{
	"locale.auto": "系统",
	"locale.en": "English",
	"locale.zh-hans": "简体中文",
	"index.scene": "场景",
	"index.company": "公司介绍",
	"index.package": "产品包",
	"index.details": "详情",
	"word.whole": "全部",
	"word.download": "下载记录",
	"word.preservation": "转存到我的手机",
	"word.forward": "转发给朋友",
	"me.WeChat": "微信名",
	"me.message": "留言反馈",
	"me.myDownloads": "我的下载",
	"me.contact": "联系我们",
	"me.logout": "退出登录"
}

locale/index.js

import en from './en.json'
import zhHans from './zh-Hans.json'

export default {
	en,
	'zh-Hans': zhHans,
}

二、应用

1、页面(固定数据)

在首页pages/index/index.vue

在首页必须这样写,其他页面就只要写(5)即可

<template>
	<view class="container">
      //(5)
      <view>{{$t('index.scene')}}</view> 
    </view>
</template>

<script>
	export default {
		computed: { //(1)
			locales() {
				return [{
						text: this.$t('locale.auto'),
						code: 'auto'
					},{
						text: this.$t('locale.en'),
						code: 'en'
					}, {
						text: this.$t('locale.zh-hans'),
						code: 'zh-Hans'
					}
				]
			}
		},
		data() {
			return {
				applicationLocale: '', //语言//(2)
                systemLocale:''
			};
		},
		onLoad() {
            //(3)
			let systemInfo = uni.getSystemInfoSync();
			this.systemLocale = systemInfo.language;
			this.applicationLocale = uni.getLocale();
			this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
			uni.onLocaleChange((e) => {
				this.applicationLocale = e.locale;
			})
            //h5页面设置成英文
            /*#ifdef H5*/
			this.applicationLocale = 'en';
			this.$i18n.locale = 'en'
			/*#endif*/
		},
		methods: {
            //(4)
			onLocaleChange(e) {
				if (this.isAndroid) {
					uni.showModal({
						content: this.$t('index.language-change-confirm'),
						success: (res) => {
							if (res.confirm) {
								uni.setLocale(e.code);
							}
						}
					})
				} else {
					uni.setLocale(e.code);
					this.$i18n.locale = e.code;
				}
			},
		}
	}
</script>

注意:

(1)、在template中使用的格式:{{$t('index.scene')}}

(2)、在data里数值的替换方法:this.$t('index.scene')

<template>
	<view class="me-container">
			<uni-list v-for="(item,index) in list" :key='index'>
				<uni-list-item :title="item.title" clickable="true" thumb-size="medium" showArrow
					:rightText="item.rightText?item.rightText:''" @click="pages(item,index)">
					<template v-slot:header>
						<view class="slot-box">
							<image class="slot-image" :src="item.thumb" mode="widthFix"></image>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
			};
		},
		onLoad() {
            //template正常写,不变,数组使用push的方法,对象同理
            //必须使用push的方式,若使用list:[me.message,me.myDownloads]无效
			this.list.push( {
				 	title: this.$t('me.message'),
				 	thumb: '../../static/lyfkicon.png'
				 },{
					title: this.$t('me.myDownloads'),
					thumb: '../../static/wdxzicon.png'
				}, {
					title: this.$t('me.contact'),
					rightText: '400-123-1234',
					thumb: '../../static/lxwmicon.png'
				}, {
					title: this.$t('me.logout'),
					thumb: '../../static/tcdlicon.png'
				})
		},
	}
</script>

2、动态数据

 先获取系统语言,然后把获取的语言存储起来,方便之后判断。

后端传给我的是以下格式:

{
    Name:'名字'//中文
    Name_EN:'name'//英文
}

app.vue

onLaunch: function() {
		this.$store.commit('SET_LOCALE', uni.getLocale())
},

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		getLocale: 'zh-Hans'
	},
	getters: {
		getLocale: state => state.getLocale,
	},
	mutations: {
		SET_LOCALE: (state, getLocale) => {
			state.getLocale = getLocale
		},
	}
})

export default store

页面使用

<template>
	<view class="container">
      //第一种
      <text>{{applicationLocale==='zh-Hans'?list.Title2:list.Title2_EN}}</text>
      //第二种
      <text>{{list.Name}}</text>
    </view>
</template>

<script>
	export default {
		data() {
			return {
				list: [], //资源包的数据
                SceneData: {}, //上一页携带的参数
				applicationLocale: '', //语言
			};
		},
		onLoad(e) {
            this.applicationLocale = this.$store.state.getLocale

            //第二种,直接在函数中判断赋值,template中正常写
			this.GetDataGramsList()
			
            //第一种,获取到上一页参数,然后直接在template中使用三元表达式判断即可
            //这里上一页参数就是后端传过来的数据
            this.SceneData = JSON.parse(e.Scene)
			uni.setNavigationBarTitle({
				title: this.SceneData.Name,
			})
		},
        methods: {
			GetDataGramsList() {
				let that = this
				this.api.GetDataGramsList({
					sceneid: that.SceneData.Id
				}).then(res => {
					this.list = res.data.Data
					this.list.forEach(i => {
						if (this.applicationLocale !== 'zh-Hans') {
							i.Name = i.Name_EN
							i.Img = i.Img_EN
							i.Id = i.Id
							i.Introduction = i.Introduction_EN
							i.Scene = i.Scene_EN
						}
					})
				})
			},
		}
    }
</script>

3、系统

(1)页面标题替换

 pages.json固定数据

{
	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/homr/index",
			"style": {
				"navigationBarTitleText": "%index.title%"
			}
		}, {
			"path": "pages/word/index",
			"style": {
				"navigationBarTitleText": "%index.word%",
				"enablePullDownRefresh": false
			}
		}
	],
}

动态参数--由后端返回的数据作为标题

 pages.json

{
	"pages": [
		{
			"path": "pages/word/index",
			"style": {
				"navigationBarTitleText": "",
				"enablePullDownRefresh": false
			}
		}
	],
}

页面

onLoad(e) {
			let that = this
			this.content = JSON.parse(e.item)
            
			if (this.$store.state.getLocale === 'zh-Hans') {
				uni.setNavigationBarTitle({
					title: that.content.Name,
				})
			} else {
				uni.setNavigationBarTitle({
					title: that.content.Name_EN,
				})
			}
		},

(2)导航栏替换

 pages.json固定数据

{
	"tabBar": {
		"color": "#7A7E83",
		"selectedColor": "#007AFF",
		"borderStyle": "black",
		"backgroundColor": "#F8F8F8",
		"list": [{
				"pagePath": "pages/home/index",
				"text": "%index.home%"
			},
			{
				"pagePath": "pages/word/index",
				"text": "%index.word%"
			}
		]
	},
}

若是不生效那就只能在tab页面中使用uni.setTabBarItem

我是把所有的都写了首页的onShow里面 文章来源地址https://www.toymoban.com/news/detail-501705.html

onShow() {
			uni.setTabBarItem({
				index: 0,
				pagePath: "pages/index/index",
				iconPath: "static/syicon1.png",
				selectedIconPath: "static/syicon2.png",
				text: this.$t('pages.home')
			})
			uni.setTabBarItem({
				index: 1,
				pagePath: "pages/word/index",
				iconPath: "static/chanpin.png",
				selectedIconPath: "static/wjicon.png",
				text: this.$t('pages.word')
			})
			uni.setTabBarItem({
				index: 2,
				pagePath: "pages/me/index",
				iconPath: "static/wdicon1.png",
				selectedIconPath: "static/wdicon2.png",
				text: this.$t('pages.me')
			})
		},

(3)弹窗文字替换

uni.showModal({
	title: "提示",
	content: "请输入正确的信息!",
	success: res => {
		if (res.confirm) {
			this.popupShow = true
			} else {}
		}
	});


uni.showModal({
	title: this.$t('uniapp.title'),
	content: this.$t('uniapp.content'),
	success: res => {
		if (res.confirm) {
			this.popupShow = true
			} else {}
		}
	});

到了这里,关于【uniapp】微信小程序国际化详细版的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序-切换语言(国际化i18n)的方法封装

    最近做的一个小程序的项目, 涉及到了 多语言的切换 , 就想到了之前vue用的多语言插件i18n, 就尝试着在微信开放社区搜了一下, 没有具体的实现, 但是提供了大致的实现思路, 如下: 又结合了很多大佬的分享经验, 试着去封装了一个微信的i18n方法 首先, 我们需要明确一下需要实

    2024年02月05日
    浏览(36)
  • uniapp配置了pages.json 的 tabbar 国际化,小程序切换语言没有实时切换

    如上图,按照uniapp官方文档配置了tabbar的国际化 但是微信小程序实时切换语言没有实时刷新 解决方案: 在App.vue中加入以下代码:   在onLaunch中执行方法即可

    2024年04月28日
    浏览(23)
  • next.js app目录 i18n国际化简单实现

    最近在用next写一个多语言的项目,找了好久没找到简单实现的教程,实践起来感觉都比较复杂,最后终于是在官方文档找到了,结合网上找到的代码demo,终于实现了,在这里简单总结一下。 此教程适用于比较简单的项目实现,如果你是刚入门next,并且不想用太复杂的方式去

    2024年04月22日
    浏览(29)
  • 使用uniapp开发国际化---app,vue,nvue

    hello-i18n 示例工程 - DCloud 插件市场  en.json----\\\"自定义key\\\":\\\"英文\\\"  zh-Hans.json----\\\"自定义key\\\":\\\"中文\\\"      注意:json文件中的名称需要中英文对应。 index.js vue页面模板使用---- $t(\\\'\\\') nvue页面模板使用---- t(\\\'\\\') pages.json 使用 ---- %index.title% data中使用--- this.$t(\\\'\\\') 启动项目,就可以成功切

    2024年02月06日
    浏览(31)
  • uniapp国际化npm install vue-i18n报错

    在vue2环境下,默认安装 npm install vue-i18n 的版本是 vue-i18n@9.1.9,所以报错。 用以上命令查看版本:  vue2建议5.0版本  

    2024年02月12日
    浏览(31)
  • 用i18n 实现vue2+element UI的国际化多语言切换详细步骤及代码

    这个地方要注意自己的vue版本和i1n8的匹配程度,如果是vue2点几,记得安装i18n的@8版本,不然会自动安装的最新版本,后面会报错哦,查询了下资料,好像最新版本是适配的vue3。 在src下面新建i18n文件夹,然后在里面新建index.js,里面的内容如下 新建i18n文件夹里面新建config文

    2024年02月14日
    浏览(36)
  • 【国际化Intl】Flutter 国际化多语言实践

    提示:这里参考一下几个链接 例如: https://github.com/ThinkerWing/language https://juejin.cn/post/6844903823119482888 这篇也很详细,还有包括兼容中文的繁体简体… 可以看看 该分支对应的提交是使用Android Studio 和 Flutter Intl插件 并根据掘金这篇文章的实践,兼容汉字简体和繁体字 https://g

    2023年04月23日
    浏览(46)
  • 48、springboot 的国际化之让用户在程序界面上弄个下拉框,进行动态选择语言

    上一篇是直接改浏览器的支持语言。 在浏览器上面直接改国际化语言 这次要实现的功能是直接在程序界面动态选择语言。 Locale 代表语言、国家。 应用之所以能动态呈现不同的语言界面,其实关键在于如何确定客户端的Locale(代表语言、国家信息) ——Spring Boot应用使用L

    2024年02月09日
    浏览(25)
  • hyperf 十四 国际化

    官方网址:Hyperf 文件结构:         /storage/languages/en/messages.php         /storage/languages/zh_CH/messages.php 创建文件 /config/autoload/translation.php。 多语言的调用从注入开始,即HyperfTranslationTranslator::__construct(TranslatorLoaderInterface $loader, string $locale)方法。根据配置文件Translato

    2024年02月11日
    浏览(45)
  • SpringBoot——国际化

    优质博文:IT-BLOG-CN 【1】编写国际化配置文件; 【2】使用 ResourceBundleMessageSource 管理国际化资源文件; 【3】在页面使用 ftp:message 取出国际化内容; 【1】创建 i18n 目录,并创建 login.properties 国际化默认配置文件,同时创建 login_zh_CN.properties 系统就会自动识别到是配置国际化

    2024年02月05日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包