javascript - 使用 Nuxt Js i18n 在头部添加 RTL 条件
问题描述
我想添加一个条件如下
if(app.i18n.locale == 'ar')
我在这个项目中使用阿拉伯语和英语。如果当前语言是阿拉伯语,则在头部添加bootstrap-rtl.css,如果当前语言en调用bootstrap.css,我尝试了不止一种方法,没有成功。
解决方案
例如在插件文件夹中添加一个文件direction-control.js
export default ({ app }, inject) => {
const dir = () =>
app.i18n.locales.find((x) => x.code === app.i18n.locale)?.dir;
inject('dir', dir);
};
虽然您nuxt.config.js
将需要与此类似的代码
plugins: [
'~/plugins/direction-control', // your plugins file name
// other plugins
],
modules: [
[
'nuxt-i18n',
{
locales: [
{
code: 'en',
file: 'en.json',
dir: 'ltr',
name: 'English',
},
{
code: 'ar',
file: 'ar.json',
dir: 'rtl', // that will be passed to your app
name: 'عربي',
},
],
langDir: 'translations/',
defaultLocale: 'ar',
},
],
],
现在是时候获取目录了
export default {
mounted() {
console.log(this.$dir()); // logs your direction 'ltr' or 'rtl'
console.log(this.$i18n.locale);
if (this.$i18n.locale == "ar") {
// make some action
}
},
}
或像这样的内部模板
<template>
<div :dir="$dir()">
<Nuxt />
</div>
</template>
推荐阅读
- jquery - 如何将“每个”json复制到跨度id?
- python - 解析数据并呈现 PNG 图像
- javascript - 未使用日历控件触发文本框 OnTextChanged 事件
- python-3.x - 使用现有列表创建新列表并使用正则表达式和列表中的特定字母拆分字符串
- python - 我怎样才能把这个程序递归(python)?
- cucumber - Cucumber,最大的挑战,浏览器调用
- python - TypeError:“范围”对象不支持项目分配如何解决此问题
- google-chrome-extension - Google Chrome 扩展程序可以访问 Web 蓝牙 API 吗?
- javascript - 如何让 CSS 文件正确应用于我的 PHP/HTML 代码?
- html - 当我没有安装 Wordpress 时,从 wp-admin 引用的“POST / HTTP/1.1”请求如何成功?