Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add dir attribute for each locale #762

Closed
HapLifeMan opened this issue Jun 10, 2020 · 17 comments
Closed

[Feature Request] Add dir attribute for each locale #762

HapLifeMan opened this issue Jun 10, 2020 · 17 comments

Comments

@HapLifeMan
Copy link

HapLifeMan commented Jun 10, 2020

Is your feature request related to a problem? Please describe.

I'm using this package to translate my app, and now I'm adding an RTL locale.
It would be great if nuxt-i18n could manage the body dir attribute.

Describe the solution you'd like

I propose to add a dir property for each locale in the module (values would be rtl, ltr or auto by default). For example:

// nuxt.config.js, inside nuxt-i18n module

locales: [
  { code: 'ar', iso: 'ar', file: 'ar/app.js', dir: 'rtl' },
  { code: 'en', iso: 'en-US', file: 'en/app.js', dir: 'ltr' },
  { code: 'fr', iso: 'fr-FR', file: 'fr/app.js', dir: 'ltr' },
],

Then this would be added in the HTML by setting a new dir attribute at a very top level of the document, for example in the <html> or <body> tag. I think it's a mandatory feature for an i18n repo 😊

Describe alternatives you've considered

As for now I'm using a custom plugin and adding the dir property as soon as I can:

// plugins/ltr-rtl.js

export default function({ app }, inject) {
  const dir = () => app.i18n.locales.find((x) => x.code === app.i18n.locale)?.dir;
  inject( 'dir', dir);
}
<!-- layouts/default.vue -->

<div id="app" :dir="$dir()">
  My app here...
</div>

What do you think? 😊

@HapLifeMan HapLifeMan changed the title Add dir attribute for each locale [Feature Request] Add dir attribute for each locale Jun 10, 2020
@majdal
Copy link

majdal commented Jun 17, 2020

RTL always has so little support, so I'm happy to see it being brought up!

I have a similar issue that I'm trying to resolve. I tried two different solutions, and both are unsatisfactory. I'm still fairly new to Nuxt, so I'm not 100% sure this is the right approach. It would be good to create a more "standard" way of solving the rtl problem.

export default function({ app }) {
  // beforeLanguageSwitch called right before setting a new locale
  app.i18n.beforeLanguageSwitch = (oldLocale, newLocale) => {
    app.head.htmlAttrs = {
      lang: newLocale,
      dir: newLocale == "ar" ? "rtl" : "ltr"
    }
  }
}

Nevertheless, it would be great to have a $dir property to use where needed.

@HapLifeMan
Copy link
Author

HapLifeMan commented Jun 24, 2020

Hey @rchl, I would really love to have your opinion 😊

@rchl
Copy link
Collaborator

rchl commented Jun 24, 2020

Great idea and should be easy to do. :)

@glenpike
Copy link

You can also target the direction CSS property as a workaround - https://developer.mozilla.org/en-US/docs/Web/CSS/direction
e.g.

html[lang="ar-SA"] {
  direction: rtl;
}

@HapLifeMan
Copy link
Author

@glenpike Using the HTML attribute is way much better than the CSS property 😊

@glenpike
Copy link

@glenpike Using the HTML attribute is way much better than the CSS property 😊

Hi, yes, sorry, should have commented this was a workaround.

@stale
Copy link

stale bot commented Sep 13, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 13, 2020
@stale stale bot closed this as completed Sep 20, 2020
@rchl rchl reopened this Sep 20, 2020
@stale stale bot removed the stale label Sep 20, 2020
@HapLifeMan
Copy link
Author

Hi @rchl, any news on this feature request? 😊

@francisceril
Copy link

francisceril commented Oct 19, 2020

Is the feature already added?

@rchl
Copy link
Collaborator

rchl commented Feb 1, 2021

Resolved through #1023 and released in https://github.com/nuxt-community/i18n-module/releases/tag/v6.19.0.

See the dir property in https://i18n.nuxtjs.org/options-reference#locales

@rchl rchl closed this as completed Feb 1, 2021
@aliwesome
Copy link

@rchl How can we get that property in pages or components?

@rchl
Copy link
Collaborator

rchl commented Jan 3, 2022

@aliwesome not sure what you mean exactly but you need to add this in the default layout or individual pages:

export default {
  head () {
    return this.$nuxtI18nHead()
  }
}

@Mo7ammedMamdou7
Copy link

I made this solution and worked fine:
First, create 'localize.js' as a plugin file in the plugins folder and add this code :

export default ({ app }) => {
  app.router.afterEach((to, from) => {
    if (app.i18n.locale === 'ar') {
      app.head.htmlAttrs.dir = 'rtl'
      app.head.htmlAttrs.lang = 'ar'
    } else {
      app.head.htmlAttrs.dir = 'ltr'
      app.head.htmlAttrs.lang = 'en'
    }
  })
}

This file will be imported to the nuxt.config.js file :

plugins: [
    '~/plugins/localize.js',
  ],

Then, Go to your nav component file and add this head function in your script to handle route changes:

head() {
    if (this.$i18n.locale === 'ar') {
      return {
        htmlAttrs: {
          lang: 'ar',
          dir: 'rtl',
        },
      }
    } else {
      return {
        htmlAttrs: {
          lang: 'en',
          dir: 'ltr',
        },
      }
    }
  },

@iifawzi
Copy link

iifawzi commented Mar 4, 2022

@Mo7ammedMamdou7
it's now supported out of the box.
https://i18n.nuxtjs.org/options-reference/#locales
Ref: #1023

@Mo7ammedMamdou7
Copy link

@Mo7ammedMamdou7 it's now supported out of the box. https://i18n.nuxtjs.org/options-reference/#locales Ref: #1023

Yes, bro thanks a lot for your support,
This is the configuration I made and worked fine:
First, I add this code to the i18n config in the nuxt.config.js file:

locales: [
      {
        code: 'ar',
        name: 'Arabic',
        iso: 'ar-UAE',
        file: 'ar.js',
        dir: 'rtl',
      },
      {
        code: 'en',
        name: 'English',
        iso: 'en-US',
        file: 'en.js',
        dir: 'ltr',
      },
    ],

Then I added this code to default.vue layout file:

head() {
    return this.$nuxtI18nHead({ addDirAttribute: true, addSeoAttributes: true })
  },

@AmirrezaJM
Copy link

AmirrezaJM commented Jan 15, 2023

I wrote it on nuxt3(vue3) but I can't switch between dir and lang for each locale
How I should fix it?
my default.vue layout file :

const head = useLocaleHead({
  addDirAttribute: true,
  addSeoAttributes: true
})

useHead({
  htmlAttrs: {
    lang: head.value.htmlAttrs!.lang,
    dir: head.value.htmlAttrs!.dir
  },
})


my i18n config in nuxt.cofing.ts file: 
i18n: {
    locales: [
        {
            code: 'fa',
            iso: 'fa-IR',
            name: 'Farsi',
            file: 'fa-IR.json',
            dir: 'rtl',
        },
        {
            code: 'en',
            iso: 'en-US',
            name: 'English',
            file: 'en-US.json',
            dir: 'ltr',
        },
    ],
    defaultLocale: 'fa',
    detectBrowserLanguage: false,
    langDir: "lang",
     // & add `vueI18n` option to `@nuxtjs/i18n` module options
    vueI18n: {
        legacy: false,
        fallbackLocale: 'fa',
    }
}

@momurad18
Copy link

useLocaleHead

you need to add this to your layout

<Html :lang="head.htmlAttrs.lang" :dir="head.htmlAttrs.dir">your template</Html>

@nuxt-modules nuxt-modules locked and limited conversation to collaborators Dec 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants