javascript - Add RTL condition in head with Nuxt Js i18n - Stack Overflow

I would like to add a condition as followsif(app.i18n.locale == 'ar')I use Arabic with Engli

I would like to add a condition as follows

if(app.i18n.locale == 'ar')

I use Arabic with English in this project. If the current language is Arabic, bootstrap-rtl.css is added in the head, and if the current language en is called bootstrap.css, I have tried more than one method and did not succeed.

I would like to add a condition as follows

if(app.i18n.locale == 'ar')

I use Arabic with English in this project. If the current language is Arabic, bootstrap-rtl.css is added in the head, and if the current language en is called bootstrap.css, I have tried more than one method and did not succeed.

Share Improve this question edited May 14, 2021 at 12:56 mdfst13 9108 silver badges21 bronze badges asked Dec 3, 2020 at 18:45 Ali MosbahAli Mosbah 812 silver badges6 bronze badges 1
  • see if this is helpful stackoverflow./questions/55880059/… – Shimi Commented Dec 3, 2020 at 18:54
Add a ment  | 

1 Answer 1

Reset to default 7

Add a file in plugins folder for example direction-control.js

    export default ({ app }, inject) => {
    const dir = () =>
      app.i18n.locales.find((x) => x.code === app.i18n.locale)?.dir;
      inject('dir', dir);
    };

While your nuxt.config.js will need similar code to this

  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',
      },
    ],
  ],

Now it's time to get dir

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
    }
  },
}

or inside template like this

<template>
  <div :dir="$dir()">
    <Nuxt />
  </div>
</template>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796103a4594212.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信