Here is the structure of my translation file:
{
"RANDOM": [
{
"ITEM": "basdf"
},
{
"ITEM": "casdf"
},
{
"ITEM": "dasdf"
},
{
"ITEM": "easdf"
}
]
}
The goal is to display one item randomly:
getQuestion() {
const sizeOfRandomItems = 4;
const randomNumber = Math.floor(Math.random() * sizeOfRandomItems);
return this.$filter('i18n')('RANDOM.' + randomNumber + '.ITEM');
}
The issue arises when adding a new item, as sizeOfRandomItems
needs manual updating. Is there a way to dynamically get the array size of RANDOM
from the translation file?
I attempted the following:
const arraySize = this.$filter('i18n')('RANDOM');
console.log(arraySize.length);
However, I receive a warning:
i18n-translate.js:205 Missing key:
It returns 6
(which represents the number of characters in the key holding the array RANDOM
)