What are the ways to utilize vue-i18n setup within and beyond Vue components when working with Quasar?

Hello, fellow developers. I am currently working on implementing internationalization in Quasar, using Vue 3 (Composition API) and vue-i18n. My goal is to make internationalization available throughout the entire application, not just within Vue components. However, I keep encountering errors in the process.

Here's how I have configured i18n initialization for Quasar:

I came across the i18n = lang approach in a blog post

import { boot } from 'quasar/wrappers'
import VueI18n from 'vue-i18n'
const { createI18n } = VueI18n
import messages from '../config/i18n'

let i18n

export default boot(({ app, Vue }) => {
  Vue.use(VueI18n)

  const lang = createI18n({
    locale: 'en',
    legacy: false,
    messages,
  })

  i18n = lang

  app.use(lang)
})

export { i18n }

In the example below, I import the configuration variable, but I've also tried importing what the export default returns without success:

import { i18n } from './../boot/i18n'

console.log(i18n)

export default {
  required: (value) => !!value || i18n.t('errors.rules.required'),
  email: (value) => {
    return (
      /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(value) ||
      i18n.global.t('errors.rules.email')
    )
  },
  // More translation functions...
}

I have double-checked the path for correctness, so that is not the issue. But translating the object still fails.

Here is the language object for English (en):

export default {
  errors: {
    rules: {
      required: 'Required',
      email: 'Invalid email address',
      password: 'This password is weak',
      // More translations...
    },
  },
}

This is the version of dependencies I am currently using:

{
   "dependencies":{
      "@quasar/extras":"^1.16.9",
      "quasar":"^2.14.5",
      "vue":"^3.3.11",
      "vue-i18n":"^9.9.0"
   }
}

If anyone has suggestions on how to utilize internationalization outside of Vue components successfully, please share your insights. I've tried various methods but none seem to work as expected.

Answer №1

My solution in the end was to simply include the following code snippet:

import { boot } from 'quasar/wrappers'
import VueI18n from 'vue-i18n'
const { createI18n } = VueI18n
import messages from '../config/i18n'

const i18n = createI18n({
  locale: 'en',
  legacy: false,
  messages,
})

export default boot(({ app, Vue }) => {
  Vue.use(VueI18n)
  app.use(i18n)
})

export { i18n }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating between sibling components in Angular 1.5 using the component router

Is there a way to use the new component router in Angular 1.5 to display the sibling component alongside the main one within the ng-outlet directive? I am looking to showcase both the Detail View and the List View at the same time. Based on my understandin ...

Where is the ideal location to store non-component data properties in Vue.js?

Currently, I am in the midst of a Vue.js project where components are predominantly used. However, there are moments when I simply need to incorporate some Vue.js syntax without creating an entire component. Since I am not utilizing the render function as ...

Obtaining Data from CRM 2011 with the Power of jQuery and JavaScript

Currently, I am facing an issue while attempting to retrieve data from CRM 2011 using jQuery. Despite my efforts, I am unable to successfully fetch the desired data. Below is the code that I have been working on: function GetConfigurations() { var oDataP ...

Refining search outcomes using multiple Multiselect boxes in VueJS

In my Vue component, I have successfully displayed results from a data object and created multiple multiselect boxes. However, I am facing challenges with filtering. I understand how to set and compare a single value from the multiselect for displaying sp ...

Calculate the mean value of each array, and then determine which average is higher

After running the code snippet provided, I noticed that it outputs "first" instead of "second". Can you help me verify my logic here? I first calculate both averages and then compare them to return the greater one. Do you see any issues with this approac ...

Simulating a mobile device screen size using an embedded iframe

Imagine this scenario: What if instead of adjusting the browser window size to showcase a responsive web design, we could load the site into an IFRAME with the dimensions of a mobile screen. Could this concept actually work? Picture having an IFRAME like ...

Show full-screen images on click using Ionic framework

Currently, I am working on developing a mobile app using the Ionic framework. I have created a layout that resembles the one shown in this Card Layout. My question is: How can I make the card image display in full screen when clicked by the user and retur ...

Why isn't v-model functioning properly in Vue?

In my current project involving an API, I encountered a problem. I utilized axios to fetch data from the API, which returned a JSON array. Each array item had a radio value that I appended to it. Despite attempting to use v-model to track changes in the ra ...

Exploring Vue 3 through the lens of class components

Currently, our project is utilizing Vue 2.x and our components are structured as follows: @Component({ template: ` <div> some code .... <div> ` }) export default class class1 extends Vue { @Prop() data: IsomeData; } Vue-class ...

Achieving consistent text alignment in HTML and CSS without relying on tables

As I delve into the world of HTML and CSS, I've taken a traditional approach by crafting a login page complete with three input controls (two text inputs and one button). To ensure proper alignment of these elements, I initially turned to the trusty & ...

What is the most effective way to bring in "server-side only" code in Next.js?

I am currently working on my index page's getServerSideProps function and I want to utilize a function called foo, which is imported from another local file. This function relies on a specific Node library that cannot be executed in the browser becaus ...

The custom error page in NextJS is failing to display

In my custom pages/404.ts file, I have coded the following: export default function NotFound() { return <h1>404 - Page Not Found</h1> } Additionally, there is another page that displays a 404 error when the organization is null: import Error ...

Nuxt 3: Creating a Lifestyle Blog with Challenges in Loading Dynamic Content

Working on my Nuxt website, I have successfully created the homepage and blog page. However, when navigating to a specific blog post, the page is supposed to fetch the data using async from the store by passing the slug but sometimes an error occurs where ...

Why does the getter consistently generate the previous value?

Check out this code snippet: function User(fullName) { this.fullName = fullName; Object.defineProperties(this, { firstName: { get: function () { return fullName.split(" ")[0]; ...

I have a parent DIV with a child DIV, and I am looking to use jQuery to select the last child DIV. The parent DIV has an

In my HTML code, I have a parent div called "allcomments_4" which contains several child divs with unique IDs (oneEntry), each with their own children. My goal is to locate and retrieve the content inside the last child of the parent node (lastComment) and ...

What is the best way to trigger an AJAX function every 15 seconds?

As part of my web application, I have implemented a JavaScript function that is triggered by the <body onload> event. Within this function, there is a while loop that continuously iterates until it receives the desired response from a PHP page. Unfo ...

Struggling to animate the selector of a nested div in Jquery?

Despite trying numerous variations, I am struggling to identify the correct selector of a nested div. The goal is to animate this particular div, but since no animations are taking place, I suspect that I have not selected the right element. Here is the a ...

utilizing jQuery to create dynamic data changes based on JSON file

<div id="rightside"> <h1>John Doe</h1> <p>1980 - 2020 <p><a href="johnswebsite.com">Visit Website</a> <p>Lorem ipsum dolor sit amet, consectetur adi ...

The watermark feature in HTML may not appear when printed on every page

I'm facing an issue with adding a watermark style. The watermark displays only on the first page when attempting to print in Chrome. It works perfectly in Firefox and IE. <style type="text/css"> #watermark { color: #d0d0d0; font-size: 90pt; ...

Update google maps markers and infoBubbles dynamically with ajax

I am currently using asp mvc in conjunction with jquery and the google maps api to showcase and update various locations on a map. Main Objective: Utilize markers to mark multiple locations Display brief information about these locations using info ...