Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

javascript - How to use Vue i18n translation in .js file?

I'm working on VueCLI. I've created .js file that contains rules for inputs:

import Vue from 'vue'

export const $t = (sign) => Vue.prototype.$t(sign)

function checkMail(email) {
  const re = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/ // eslint-disable-line no-eval
  return re.test(String(email).toLowerCase())
}

const rules = {
  gdprRequired: (v) => !!v || $t('registerPage.gdprNotChecked'),
  emailRules: [(v) => checkMail(v) || $t('registerPage.wrongMail')],
}

export default rules

I want to declare rules in external js file and then import it in other vue files. When I do this rule check value properly, but It don't show translated error messeage. I tried export const $t = (sign) => Vue.prototype.$t(sign), bcs similar code worked for nuxt, but here, in VueCLI I got error:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in beforeMount hook: "ReferenceError: $t is not defined"

found in

---> <VTextField>
       <VForm>
         <Register>
           <VMain>
             <VApp>
               <Session> at src/layouts/Session/Session.vue
                 <App> at src/App.vue
                   <Root>

and this:

vue.runtime.esm.js?2b0e:1888 ReferenceError: $t is not defined
    at rules.emailRules (rules.js?d86d:21)
    at VueComponent.validate (index.ts?91bf:254)
    at VueComponent.beforeMount (index.ts?91bf:217)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
    at callHook (vue.runtime.esm.js?2b0e:4219)
    at mountComponent (vue.runtime.esm.js?2b0e:4043)
    at VueComponent.Vue.$mount (vue.runtime.esm.js?2b0e:8415)
    at init (vue.runtime.esm.js?2b0e:3118)
    at createComponent (vue.runtime.esm.js?2b0e:5978)
    at createElm (vue.runtime.esm.js?2b0e:5925)

Here importing code in 'register.vue':

import rulesConstant from "@/constants/rules";

Computing exported rules to vue variable:

computed: {
    rules() {
      return rulesConstant;
    },
  },

Example use:

<v-checkbox
          class="custom-checkbox"
          :rules="[rules.gdprRequired]"
          v-model="gdprCheck"
        ></v-checkbox>

Can You help me?

question from:https://stackoverflow.com/questions/65831314/how-to-use-vue-i18n-translation-in-js-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most common way of using vue-i18n outside of Vue components is by importing the singleton instance of VueI18n class and using it directly:

@/i18n/index.js

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import locales from './locales'

Vue.use(VueI18n);

export default new VueI18n({
  locale: 'en',
  messages: locales,
});

@/constants/rules

import i18n from '@/i18n/index.js'

const rules = {
  gdprRequired: (v) => !!v || i18n.t('registerPage.gdprNotChecked'),
  emailRules: [(v) => checkMail(v) || i18n.t('registerPage.wrongMail')],
}

export default rules

This way you have all the instance methods at your disposal, not just t


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...