Hey there, I really appreciate anyone who can assist me with this. I've been dabbling in Laravel for a few months and now I'm trying to dive into studying Nuxt. The specific type of translation I need help with is proving to be quite challenging to find information on.
What I'm attempting to do is populate an array with objects, each object containing the same string in three different languages. I have a function that properly returns the object's string, but I'm unsure about how to specify the locale for my function. This is part of my learning process, and despite searching extensively online, I haven't found much guidance. While I already understand how to perform translations using a locales file, I want to create this structure so that eventually I can retrieve information from an API.
At the moment, everything seems to be running smoothly as the computed function successfully displays a random item from the array on my screen. However, I am struggling to determine the locale and the result of the translation function before executing the math.random.
This is a snippet of my code:
LoadingBar.vue
<template lang="html">
<div class="loading-page" v-if="loading">
<h1 class="title">Preparing the ground for you</h1>
<img src="~/assets/media/photos/agronomy.png" />
<p class="text">{{tiprandom}}</p>
</div>
</template>
<script>
import randomtip from '~/components/randomtip.vue'
export default {
layout: 'blank',
components: {
randomtip
},
data: () => ({
loading: true,
tipolocal_id: 1,
dicas: [
{id:1, nome: 'Tip 1: Consider giving extra attention to the seedlings, as they are quite sensitive. It's crucial to keep the pot in a shaded area and always monitor irrigation',
nome_en: 'Tip 1: Consider giving extra attention to the seedlings, as they are quite sensitive. It's crucial to keep the pot in a shaded area and always monitor irrigation',
nome_es: 'Tip 1: Considere prestarle más atención a las plántulas, ya que son bastante sensibles. Es fundamental mantener la maceta en un área sombreada y siempre controlar el riego.'
},
{id:2, nome: 'Tip 2: Consider giving extra attention to the seedlings, as they are quite sensitive. It's crucial to keep the pot in a shaded area and always monitor irrigation',
nome_en: 'Tip 2: Consider giving extra attention to the seedlings, as they are quite sensitive. It's crucial to keep the pot in a shaded area and always monitor irrigation',
nome_es: 'Tip 2: Considere prestarle más atención a las plántulas, ya que son bastante sensibles. Es fundamental mantener la maceta en un área sombreada y siempre controlar el riego.'
},
{id:3, nome: 'Tip 3: Consider giving extra attention to the seedlings, as they are quite sensitive. It's crucial to keep the pot in a shaded area and always monitor irrigation',
nome_en: 'Tip 3: Consider giving extra attention to the seedlings, as they are quite sensitive. It's crucial to keep the pot in a shaded area and always monitor irrigation',
nome_es: 'Tip 3: Considere prestarle más atención a las plántulas, ya que son bastante sensibles. Es fundamental mantener la maceta en un área sombreada y siempre controlar el riego.'
},
],
}),
computed: {
tiprandom () {
return this.dicas[Math.floor(Math.random()*this.dicas.length)]
},
locale() {
if (this.$i18n.locale == 'pt-br') {
return 'pt_br'
} else {
return this.$i18n.locale
}
}
},
methods: {
functionDicas(dicas) {
if (this.$i18n.locale == 'pt-br') {
return dicas.nome
} else if (this.$i18n.locale == 'en') {
return dicas.nome_en
} else if (this.$i18n.locale == 'es') {
return dicas.nome_es
}
},
start () {
this.loading = true
},
finish () {
this.loading = false
},
}
}
</script>