No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    How to access translations in static .js file?

    Help
    i18n translations
    4
    6
    745
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mYnDstrEAm
      mYnDstrEAm last edited by

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • metalsadman
        metalsadman last edited by

        @mYnDstrEAm export the instance of your i18n boot.

        something like this:

        //src/boot/i18n.js
        ...
        let i18n = null
        export default ({ app }) => {
          // Set i18n instance on app;
          // We inject it into root component by doing so;
          // new Vue({..., i18n: ... }).$mount(...)
        i18n = new VueI18n({
            locale: 'en',
            fallbackLocale: 'en',
            messages
          })
          app.i18n = i18n
        }
        
        export { i18n }
        
        // some js file
        import { i18n } from 'src/boot/i18n.js'
        ...
        i18n.t('translation.path')
        
        1 Reply Last reply Reply Quote 1
        • metalsadman
          metalsadman last edited by

          @mYnDstrEAm you didn’t need to delete your post, just marked the title as [Solved] *if it was solved, for others who might stumble upon the same issue :).

          1 Reply Last reply Reply Quote 0
          • yk-davdomin
            yk-davdomin last edited by

            Hey @metalsadman How can I do this in quasar 2?

            1 Reply Last reply Reply Quote 0
            • BenceSzalai
              BenceSzalai last edited by BenceSzalai

              For Quasar v2 the default src/boot/i18n.js is:

              import { createI18n } from 'vue-i18n'
              import messages from 'src/i18n'
              
              export default ({ app }) => {
                const i18n = createI18n({
                  locale: 'en-US',
                  messages
                })
                app.use(i18n)
              }
              

              Modified version:

              import { createI18n } from 'vue-i18n'
              import messages from 'src/i18n'
              
              let i18n
              export default ({ app }) => {
                i18n = createI18n({
                  locale: 'en-US',
                  messages
                })
                app.use(i18n)
              }
              
              export { i18n }
              

              But really you should rather use this.$t(...) whenever you can.

              1 Reply Last reply Reply Quote 1
              • yk-davdomin
                yk-davdomin last edited by

                I was talking about using it in single js file, I found a solution:

                import { i18n } from 'src/boot/i18n'
                const { t } = i18n.global
                
                .
                .
                .
                export const eStatusList = [
                  { code: '0', label: t('documents.inProgress') },
                  { code: '1', label: t('documents.sent') },
                  { code: '2', label: t('documents.accepted') },
                  { code: '3', label: t('documents.rejected') },
                  { code: '4', label: t('documents.voiding') },
                  { code: '5', label: t('documents.voided') }
                ]
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post